What is pattern publish subscribe (also known as Observer or Listener)?
Publishers are the entities who create/publish a message on a header. Subscribers/listeners/observers are the entities who subscribe to a messages on a header.
In a header based Publish-Subscribe pattern, Publishers tag each message with the a header instead of referencing specific Subscribers. Messaging system then sends the message to all Subscribers who have asked to receive messages on that header.
Publishers only concern themselves with creating the original message and can leave the task of servicing the Subscribers to the messaging infrastructure (this is where pattern comes into picture).
see other reactive streams projects (build with functional pipeline & lazy evaluation):
- RxJava - https://github.com/ReactiveX/RxJava
- Vert.x - https://github.com/eclipse-vertx/vert.x (wrapper over RxJava)
- Akka - https://github.com/akka/akka
- Java 9 reactive streams - https://github.com/reactive-streams/reactive-streams-jvm
- Spring reactor - https://github.com/reactor/projectreactor.io
- Kafka
public interface Publisher<T> {
public void subscribe(Subscriber<? super T> s);
}
public interface Subscriber<T> {//with 3 channels
public void onSubscribe(Subscription s);
public void onNext(T t);//data
public void onComplete();//all finished successfully
public void onError(Throwable t);//to deal with exceptions
}
public interface Subscription {
public void request(long n);//back pressure support
public void cancel();
}
public interface Processor<T, R> extends Subscriber<T>, Publisher<R> {
}