The Observer Pattern defines a one-to-many dependency where state changes in a "Subject" (Publisher) are automatically broadcast to "Observers" (Subscribers). In modern high-throughput systems, the pattern has evolved into Reactive Streams to handle asynchronous data flow and Backpressure.
Publisher, Subscriber, Subscription) that support asynchronous, non-blocking flow with flow control.In a push-only observer model, a fast publisher can overwhelm a slow subscriber, leading to buffer overflows or OutOfMemoryError.
Subscription.request(n). The Publisher only pushes what has been requested.SubmissionPublisherJava provides a built-in SubmissionPublisher that implements the Flow.Publisher interface for easy in-process event bus creation.
public class MetricService {
private final SubmissionPublisher<Double> publisher = new SubmissionPublisher<>();
public void start() {
publisher.subscribe(new Flow.Subscriber<>() {
private Flow.Subscription subscription;
@Override
public void onSubscribe(Flow.Subscription sub) {
this.subscription = sub;
subscription.request(1); // Backpressure: request only 1
}
@Override
public void onNext(Double item) {
process(item); // Simulate work
subscription.request(1); // Ask for next only after processing
}
@Override public void onError(Throwable t) { t.printStackTrace(); }
@Override public void onComplete() { System.out.println("Done"); }
});
}
public void submit(double val) { publisher.submit(val); }
}
WeakReference if manual unsubscription isn't guaranteed.CopyOnWriteArrayList for the observer list to allow concurrent modification (subscription/unsubscription) during a broadcast.See Also: