Observable using defer
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 normally gets created once the code executes, Observable using defer , defers the creation of Observable. It waits till a subscription is done.
Below are main property of Observable using defer.
Below is the code showing Observable using defer
package com.codingsaint.learning.rxjava.observables;
import com.codingsaint.learning.rxjava.observer.DemoObserver;
import com.codingsaint.learning.rxjava.utils.RxUtils;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ObservableUsingDefer {
private static final Logger LOGGER = LoggerFactory.getLogger(ObservableUsingDefer.class);
public static void main(String... args) {
Observable<Integer> observableUsingDefer = Observable.defer(() -> {
return Observable.fromIterable(RxUtils.postiveNumbers(5));
});
observableUsingDefer.subscribe(new DemoObserver());
observableUsingDefer.subscribe(new DemoObserver());
}
}
Here intentionally we are subscribing 2 times to show that each time Observable is created. You can also see that defer is taking fromIterable to create the observable. This will stop creating defer until we subscribe. Once subscribed Observable.fromInterable code will be invoked