Creating Observable RxJava
I am a developer who loves Java, Spring, Quarkus, Micronaut, Open source, Microservices, Cloud
Search for a command to run...
I am a developer who loves Java, Spring, Quarkus, Micronaut, Open source, Microservices, Cloud
No comments yet. Be the first to comment.
Spring AI Tool Calling: From Chatbot to AI Agent with @Tool Your AI is smart. It knows an enormous amount. But it's frozen. It doesn't know what time it is right now. It doesn't know what's on your ca
Spring AI Advisors API Explained Series: Spring AI Complete Course — Lecture 4 of 12Reading Time: 8 minutesLevel: Intermediate Most developers stop at ChatClient. That is enough for demos. It is not
Working with Multiple AI Models in Spring AI Spring AI Complete Course — Lecture 3 of 12Previous: Lecture 2 — ChatClient API | Next: Lecture 4 — Advisors API In production AI applications, you rarel
Spring AI ChatClient API: The Fluent Heart of AI Integration Introduction If you've ever tried integrating AI models into a Java application, you know the pain. HTTP clients, API keys scattered everywhere, vendor-specific SDKs that never quite fit. W...
What is Spring AI? — Why Java Developers Need This in 2026 Every AI tutorial you see is in Python. LangChain, LlamaIndex, OpenAI SDK — all Python. But here's the uncomfortable truth: 80% of enterprise backends run Java. So who's building AI into thos...
Observable being the most important block of reactive programming , it is important to understand different methods of creating Observable.
Observable can be created be created by following methods.
Observables emits events on which Observer subscribes too. So let us start by creating a DemoObserver which will be subscribing to our Observables.
The above DemoObserver will be subscribing to our Observable. This will let the code be more clear and understandable. The DemoObservable is simple and logs events as subscription , errors and completion. It also adds logs each element the observable emits with onNext method.
Let’s look at the methods of creating an Observable.
just is used to create Observable with maximum of 10 elements. It comes handy when we have events limited to maximum of 10 events.
The above code will create an Observable with list of alphabets from a to j . Below is the logs which will be published.
08-01-20 12:51:37 [main] [INFO ] [c.c.l.rxjava.observable.CsObservable]- onSubscribe 08-01-20 12:51:37 [main] [INFO ] [c.c.l.rxjava.observable.CsObservable]- onNext -> a 08-01-20 12:51:37 [main] [INFO ] [c.c.l.rxjava.observable.CsObservable]- onNext -> b 08-01-20 12:51:37 [main] [INFO ] [c.c.l.rxjava.observable.CsObservable]- onNext -> c 08-01-20 12:51:37 [main] [INFO ] [c.c.l.rxjava.observable.CsObservable]- onNext -> d 08-01-20 12:51:37 [main] [INFO ] [c.c.l.rxjava.observable.CsObservable]- onNext -> e 08-01-20 12:51:37 [main] [INFO ] [c.c.l.rxjava.observable.CsObservable]- onNext -> f 08-01-20 12:51:37 [main] [INFO ] [c.c.l.rxjava.observable.CsObservable]- onNext -> g 08-01-20 12:51:37 [main] [INFO ] [c.c.l.rxjava.observable.CsObservable]- onNext -> h 08-01-20 12:51:37 [main] [INFO ] [c.c.l.rxjava.observable.CsObservable]- onNext -> i 08-01-20 12:51:37 [main] [INFO ] [c.c.l.rxjava.observable.CsObservable]- onNext -> j 08-01-20 12:51:37 [main] [INFO ] [c.c.l.rxjava.observable.CsObservable]- onComplete
The above log confirms onSubscribe , followed by onNext with elements from “a” to “j” and completed by an onComplete.
The above code can also be written using our own DemoObserver in a simple and cleaner way.
Observable.just("a","b","c","d","e","f","g","h","i","j")
.subscribe(new DemoObserver());
For further example we’ll be using our DemoObserver for readability.
In next post we will see creating observables with other methods.