Browse DevX
Sign up for e-mail newsletters from DevX


Master JMS Messaging with OpenJMS  : Page 4

Learn how to take advantage of Java's Messaging Service API (JMS) with the free OpenJMS implementation. You'll see how to send and receive messages using both point-to-point and publish/subscribe messaging models.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Publish/Subscribe Messaging with OpenJMS
The other major message domain supported by JMS is called publish/subscribe. In the publish-and-subscribe messaging style, a publisher sends messages to a topic, and those messages may be received by one or more subscribers.

Writing to a Topic (a.k.a. Publishing)

Writing to a topic is not much different from writing to a queue. Take a look at the code below, which places a message on a topic. In essence, you have just replaced references to a queue with a topic. This time, the code leverages a TopicConnectionFactory as opposed to a QueueConnectionFactory. Similarly, it uses a TopicConnection, a TopicSession, and a TopicPublisher (as opposed to a QueueConnection, QueueSesion, and QueueSender respectively).

Writing to a topic is often referred to as "publishing," and reading from a topic is called "subscribing." In the point-to-point messaging domain vernacular, writing to a queue is called sending and reading from a queue is called receiving. Take note that just as you wrote to queue1 in the point-to-point example, you'll now write to topic1 (again, a topic which the OpenJMS install process preconfigures. Refer to the section Creating Your Own Topics and Queues to find out how to create a different topic.

Unlike in the point-to-point to messaging domain however, messages in the publish/subscribe domain are not persistent. Thus, when you run a typical publisher, a subscriber program (presented in the next section) must be running to receive the messages. You can remove the requirement that the subscriber must be running by using durable subscriptions, which I'll cover in a future article.

public class TopicPublish { public static void main(String[] args) { // standard boilerplate code to // establish a JNDI context and connection try { // retrieve topic connection factory TopicConnectionFactory factory = (TopicConnectionFactory)context.lookup( "JmsTopicConnectionFactory"); // create a topic connection using factory TopicConnection topicConnection = factory.createTopicConnection(); topicConnection.start(); // create a topic session // set transactions to false and set auto // acknowledgement of receipt of messages TopicSession topicSession = topicConnection.createTopicSession( false,Session.AUTO_ACKNOWLEDGE); // lookup the topic, topic1 Topic topic = (Topic) context.lookup("topic1"); // create a topic publisher and associate // to the retrieved topic TopicPublisher topicPublisher = topicSession.createPublisher(topic); // publish a message to the topic System.out.println( "Sample application publishing " + "a message to a topic."); TextMessage message = topicSession.createTextMessage( "Hello world"); topicPublisher.publish(message); // clean up ... } catch (NamingException e) { e.printStackTrace(); } catch (JMSException e) { e.printStackTrace(); } } }

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date