Akka Java Tutorials Part 3- Distributed Publisher Subscriber

gaurav ranjan
3 min readNov 9, 2019

--

As part of akka java tutorials series in Part 2 I discussed how to build a singleton scheduler in an akka cluster. This scheduler is useful when running cron jobs at scheduled intervals without worrying about the synchronisation among the nodes in the cluster.

In this tutorial I will be covering AKKA Distributed Pub Sub. Pub/Sub model is a great way to interact within the actors of the cluster. This typically works like any other distributed queuing system like Kafka. But obviously its not so advanced and feature rich like them. It is suited for cases where you don’t want to invest in running and maintaining a generic queueing software. Rather have a mechanism to effectively solve a specific use case which needs some sort of queuing.

With this lets get into understanding the various nuances of this tool.

Mediator

Akka provides mediator that manages a registry of actor references and replicates the entries to peer actors among all cluster nodes or a group of nodes tagged with a specific role.

Mediator actor needs to be registered in actors which acts as a publisher or subscriber. It can be registered in the following way.

ActorRef mediator= DistributedPubSub.get(actorSystem).mediator();

Topic

Like any other queuing system Akka also has the concept of topics where you can publish the messages and also subscribe to the topic to listen to those messages.

Publisher

Publisher is the actor which publishes message to the topic. It uses the mediator to publish a message to a topic. In this case I have used akka-tutorials as my topic name.

this.mediator.tell(new DistributedPubSubMediator.Publish("akka-tutorials", message), getSelf());

You can see above how I am publishing a message to the topic akka-tutorials.

Subscriber

Subscriber is the one which subscribes to a specific topic and listens on them. Whenever a specific message comes on that topic it can act based on what it needs to do with that.

this.mediator.tell(new DistributedPubSubMediator.Subscribe("akka-tutorials", getSelf()), getSelf());

And below you can see how to handle the messages sent on the topic. Once you subscribe to a topic the messages would be received in the onReceive method of the actor.

@Override
public void onReceive(Object message) throws Throwable {
if (message instanceof ClusterMessages.PublishMessage) {
log.info("message received from publisher in akka cluster");
}
}

Caution

With the steps mentioned above you have your distributed publisher subscriber system up and running in no time. This might look really easy with respect to code and setup. However, you have to bear in mind that this is not a full blown queuing system.

The Message Delivery Reliability of Akka, message delivery guarantee in distributed pub sub modes is at-most-once delivery. In other words, messages can be lost over the wire.

So you need to be careful in designing the system. You can make use of akka distributed data which I explained in Part 1 to make sure even though the messages are lost it can be retried later. Akka distributed data has a config to store messages on file.

You would also need to design the Acknowledgement mechanism for the published messages.

Having said that designing for the above two problems is not so hard. I have been using this my production systems for a long time and haven’t ran into much of the issues. Best part is its easier to built this rather than going for something like Kafka if the volume of messages are reasonable.

The complete code is available here.

Conclusion

In this series we saw how akka cluster tools can be used to build a reasonable working Distributed Publisher Subscriber system.

In the next part I would be covering Slick JBDC.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response