Steps to send and receive messages using JMS

However, before we start using JMS APIs, let's take a look at the generic steps involved in using them. The following steps show how to send a message to a queue and receive it. Although the steps focus on queues, the steps for topics are similar, but with appropriate topic-related classes:

  1. Look up ConnectionFactory using JNDI:
InitialContext ctx = new InitialContext(); 
QueueConnectionFactory connectionFactory = (QueueConnectionFactory)initCtx.lookup("jndi_name_of_connection_factory"); 
  1. Create a JMS connection and start it:
QueueConnection con = connectionFactory.createQueueConnection(); 
con.start(); 
  1. Create a JMS session:
QueueSession session = con.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); ...

Get Java EE 8 Development with Eclipse now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.