Apache HazelCast – Distributed Topic

Apache HazelCast – Distributed Topic

Introduction

This post is third in the series of the Apache HazelCast blogs and deals with the HazelCast Distributed Topic mechanism where, we will discuss about publishing/subscribing messages(objects). Publishing and subscribing operations are cluster-wide. When a member subscribes to a topic, he is actually registering for messages published by other members in the cluster, including new members who’ve joined the cluster, after you’ve added the Listener. To achieve this, HazelCast provides distributed ITopic components.

Please refer to our previous blogs on “Apache HazelCast – Management Center” and “Apache HazelCast – Distributed Cache“.

Use Case

In this use case we are going to publish and subscribe the user defined POJO objects (instead of String, List, and Set) to the topics.

What we need to do:

  • Prerequisites
  • User-defined POJO
  • Publisher Objects to Topic
  • Subscriber Objects from Topic
  • Execution of programs
  • Challenges

Solution

Follow the sections given below from the previous blog “Apache HazelCast – Management Center” to download, install Hazelcast and to start HazelCast nodes, all of which are required to perform this use case.

Prerequisites

  • Install JDK 1.6+
  • Make sure HazelCast nodes are running
  • Configure below two jars into classpath
    • hazelcast-3.x.x.jar
    • hazelcast-client-3.x.x.jar

User-defined POJO

  • The TechnologyDetails POJO class includes details of technology (Cassandra, Neo4j) like technology type, license, written language, stable release and so on. This POJO must implement serializable interface since, all HazelCast components support serialized objects only. By default, Java pre-defined class such as List, String, and Map are implemented with Serializable interface.
  • TechnologyDetails.java

Publish Objects to Topic

  • This class is used for publishing objects that have details about Apache Camel and Apache Hadoop, to a Topic with the help of TechnologyDetails POJO.
  • ObjectDistributor.java

Subscriber Objects from Topic

  • This class is used to add a subscriber for Topic. In Listener classes, we just print the details of POJO field for demo purpose. We can also perform operations the way we want to perform them.

(Note: Don’t shutdown the HazelCast instance for this program. Otherwise, the Listeners get killed.)

  • ObjectCollector.java
  • TopicSubscriber.java: This MessageListener class helps to notify Topic, whenever an item Message is published in ITopic.
  • hazel.properties

Execution of programs

Create two jar files with the below given classes:

  • ObjectDistributor.jar
    • com.treselle.hazel.distributedmessaging.ObjectDistributor (Main-Class)
    • com.treselle.hazel.distributedmessaging.TechnologyDetails
  • ObjectCollector.jar
    • com.treselle.hazel.distributedmessaging.ObjectCollector (Main-Class)
    • com.treselle.hazel.distributedmessaging.TopicSubscriber
    • com.treselle.hazel.distributedmessaging.TechnologyDetails

Choose two JVM machines or open two Command prompts:

  • First start the ObjectCollector.jar in one command prompt/JVM machine
  • Then, Run the ObjectDistributor.jar in other prompts/JVM like the above command.

Note: While executing the ObjectDistributor.jar, please check the console where we run the ObjectCollector.jar. For publishing each object into Topic, the stats details of each object like source, event type, and object details have also been logged.

After running the ObjectDistributor.jar, open the HazelCast management center (http:{tomcat_server_ip}:8080/mancenter-3.2.6/main.jsp), and you can view the added distributedTopic in the list as shown below:

apache_hazelcast_distributed_topic

Note: Orange mark indicates Created Topic

Challenges

Problem: When adding user-defined Class (TechnologyDetails.java) Objects to HazelCast components IList, ISet, IMap, MultiMap, Queue, and Topic, the below exception is thrown:

exception_screenshot

Solution: HazelCast components only process serialized objects. The user-defined class(TechnologyDetails.java) must implement Serializable interface. Java predefined classes String, Map, List, and Set are serialized by default.

Conclusion

  • HazelCast also supports the storage or cache of custom objects(User defined classes) by implementing Serializable interface.
  • Hazelcast provides distribution mechanism for publishing messages that are delivered to multiple subscribers which is also known as publish/subscribe (pub/sub) messaging model.

The links to the previous two posts of this 3 part-blog series are given below:

References

Topic:
http://docs.hazelcast.org/docs/latest/manual/html-single/hazelcast-documentation.html#topic

14030 Views 2 Views Today