This section explains the different steps involved in writing normal JMS clients. We will cover only the concepts explained in this chapter. We will not cover advanced concepts like XA sessions, session pools, connection consumers, etc.
The sequence diagram shown below depicts the basic steps involved in writing a basic JMS client for sending messages:
Firstly, create an initial context for doing JNDI lookup. Depending on the service provider the constructor for initializing the initial context may take an instance of java. util. Properties holding the provider URL, name of the initial context factory, etc. Even though the code for creating the initial context is similar for different JNDI service providers, the value passed for the initial context factory and the provider URL vary. For details on provider specific lookups, see Appendix A:
Properties prop = new Properties(); prop. put (Context. INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory"); prop.put (Context.PROVIDER_URL, "file:C:\\temp"); Context ctx = new InitialContext (prop);
Next, look up the required administered objects. In the PTP model the administered objects are generally queues and queue connection factories whereas in the Pub/Sub they are topics and topic connection factories:
//PTP Queue queue = (Queue) ctx.lookup ("myQueue"); QueueConnectionFactory qcf = (QueueConnectionFactory) ctx.lookup ("myQCF"); //Pub/Sub Topic topic = (Topic) ctx.lookup ("myTopic"); TopicConnectionFactory tcf = (TopicConnectionFactory) ctx. lookup ("myTCF");
Then use the connection factory to create connections. Connections can be queue connections for PTP and topic connections for Pub/Sub:
//PTP QueueConnection qCon = qcf.createQueueConnection(); //Pub/Sub TopicConnection tCon = tcf.createTopicConnection();
Use these connections to create sessions. The PTP model uses queue sessions and the Pub/Sub model uses topic sessions. Message acknowledgement mode and transaction support for the sessions can be specified here:
//PTP QueueSession qSes = qCon.createQueueSession (false, Session.AUTO_ACKNOWLEDGE); //Pub/Sub TopicSession tSes = tCon.createTopicSession (false, Session.AUTO_ACKNOWLEDGE)
Create message producers for the session and the destination. For the PTP model queue senders are created using queue sessions that are associated to specific queues and in the Pub/Sub model topic publishers are created using topic sessions that are associated to specific topics. You can set different properties, in other words, message time-to-live, message priority, etc. for all the messages sent by this message producer using the various methods discussed in the section on message producers:
//PTP QueueSender sender = qSes.createSender (queue); //Pub/Sub TopicPublisher publisher = tSes.createPublisher(topic);
Create appropriate messages using the sessions and send them using the required methods defined in the message producer. You can use different versions of send/publish methods defined in the queue sender/topic publisher to override the global values set in the message producer for properties like message time-to-live, message priority, etc.:
//PTP TextMessage msg = qSes.createTextMessage ("Hi there"); sender.send(msg); //Pub/Sub TextMessage msg = tSes.createTextMessage ("Hi there"); publisher.publish (msg);
The sequence diagram shown below depicts the various steps involved in writing a typical JMS client for receiving messages asynchronously. For synchronous message delivery clients will be using the receive() method on the message consumer in a blocking thread:
The steps involved in writing JMS clients for receiving messages are listed below. The following steps are common to writing JMS clients for sending and receiving messages:
Creating a JNDI initial context
Looking up destinations and connection factories
Creating connections
Creating sessions
Please refer to the last section on writing JMS clients for sending messages for a detailed coverage of writing the aforementioned steps.
Create message consumers using the session for the looked up destination. For the PTP model, queue receivers are created using queue sessions that are associated to specific queues and in the Pub/Sub model topic subscribers are created using topic sessions that are associated to specific topics:
//PTP QueueReceiver receiver = qSes.createReceiver(queue); //Pub/Sub TopicSubscriber subscriber = tSes.createSubscriber(topic);
Create a message listener and register it either with the consumer or with the session. Message listeners are classes that implement javax.jms.MessageListener. The code listing below implements the interface using an anonymous inner class:
MessageListener listener = new MessageListener() { public void onMessage (Message msg) { //Process Message try{ }catch (JMSException e) { e.printStackTrace(); throw new RuntimeException (e.getMessage()); } } }; //PTP receiver.setMessageListener (listener); //Pub/Sub subscriber.setMessageListener (listener);
Connections when created afresh are created in stopped mode and connections in stopped mode can't receive messages. To enable message reception connections should be started explicitly:
//PTP qCon.start(); //Pub/Sub tCon.start();