Book Image

Getting Started with Hazelcast

By : Matthew Johns
Book Image

Getting Started with Hazelcast

By: Matthew Johns

Overview of this book

Table of Contents (18 chapters)
Getting Started with Hazelcast
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Spreading the word


The final collection capability offered by Hazelcast is a broadcast messaging system. This is very much inspired by JMS topics and offers a comparable set of features, in that, we can publish events on to messaging bus to deliver to a large number of subscribed receivers.

As we can see in the following diagram, an application can publish a message onto a topic that will then be distributed across over to all instances of our application who have subscribed to the topic. This will include the instance that originally sent the message in the first place, assuming it too has a listener subscribed to the topic.

First things first, we'll need a MessageListener class to handle messages, implementing an onMessage(Message<T>) method as required.

public class TopicListener implements MessageListener<String> {

  @Override
  public void onMessage(Message<String> msg) {
    System.err.println("Received: " + msg.getMessageObject());
  }
}

Let's create a class to broadcast...