Use this task to develop a JMS client application to use messages to communicate with enterprise applications.
This topic gives an overview of the steps needed to develop a JMS client application, based on a sample client provided with WebSphere Application Server. This topic only describes the JMS-related considerations; it does not describe general client programming, which you should already be familiar with. For detailed information about these steps, and for examples of developing JMS clients, see the Java Message Service Documentation and the WebSphere MQ Using Java book, SC34-5456.
A JMS client assumes that the JMS resources (such as a queue connection factory and queue destination) already exist. A client application can use JMS resources administered by the application server or administered by the client container regardless of whether the client application is running on the same machine as the server or remotely.
For more information about developing client applications and configuring JMS resources for them, see Developing J2EE application client code and related tasks.
To use JMS, a typical JMS client program completes the following general steps:
import javax.naming.Context; import javax.naming.InitialContext; import javax.rmi.PortableRemoteObject; import javax.jms.*;
try { ctx = new InitialContext(env); ...
public class JMSppSampleClient { public static void main(String[] args) throws JMSException, Exception { String messageID = null; String outString = null; String qcfName = "java:comp/env/jms/ConnectionFactory"; String qnameIn = "java:comp/env/jms/Q1"; String qnameOut = "java:comp/env/jms/Q2"; boolean verbose = false; QueueSession session = null; QueueConnection connection = null; Context ctx = null; QueueConnectionFactory qcf = null; Queue inQueue = null; Queue outQueue = null; ...
qcf = (QueueConnectionFactory)ctx.lookup( qcfName ); ... inQueue = (Queue)ctx.lookup( qnameIn ); outQueue = (Queue)ctx.lookup( qnameOut ); ...
connection = qcf.createQueueConnection();The JMS specification defines that connections should be created in the stopped state. Until the connection starts, MessageConsumers that are associated with the connection cannot receive any messages. To start the connection, issue the following command:
connection.start();
boolean transacted = false; session = connection.createQueueSession( transacted, Session.AUTO_ACKNOWLEDGE);
In this example, the session is not transacted, and it should automatically acknowledge received messages. With these settings, a message is backed out only after a system error or if the client application terminates unexpectedly.
QueueSender queueSender = session.createSender(inQueue);
JMS provides several message types, each of which embodies some knowledge of its content. To avoid referencing the vendor-specific class names for the message types, methods are provided on the Session object for message creation. In this example, a text message is created from the outString property, which could be provided as an input parameter on invocation of the client program or constructed in some other way:
TextMessage outMessage = session.createTextMessage(outString);
queueSender.send(outMessage);
messageID = outMessage.getJMSMessageID();The correlation ID is then used in a message selector, to select only messages that have that ID:
String selector = "JMSCorrelationID = '"+messageID+"'";
QueueReceiver queueReceiver = session.createReceiver(outQueue, selector);
Message inMessage = queueReceiver.receive(2000);
The parameter in the receive call is a timeout in milliseconds. This parameter defines how long the method should wait if there is no message available immediately. If you omit this parameter, the call blocks indefinitely. If you do not want any delay, use the receiveNoWait()method. In this example, the receive call returns when the message arrives, or after 2000ms, whichever is sooner.
In this example, the instanceof operator is used to check that the message received is of the TextMessage type. The message content is then extracted by casting to the TextMessage subclass.
if ( inMessage instanceof TextMessage ) ... String replyString = ((TextMessage) inMessage).getText();
queueReceiver.close(); ... queueSender.close(); ... session.close(); session = null; ... connection.close(); connection = null;
// Creating a TopicPublisher TopicPublisher pub = session.createPublisher(topic); ... pub.publish(outMessage); ... // Closing TopicPublisher pub.close();
Unlike normal Java exceptions, a JMSException can contain another exception embedded in it. The implementation of JMSException does not include the embedded exception in the output of its toString()method. Therefore, you need to check explicitly for an embedded exception and print it out, as shown in the following example:
catch (JMSException je) { System.out.println("JMS failed with "+je); Exception le = je.getLinkedException(); if (le != null) { System.out.println("linked exception "+le); } }
Related tasks
Developing a J2EE application to use JMS
Deploying J2EE application clients on workstation platforms
Configuring Java messaging client resources
Configuring new JMS connection factories for application clients
Configuring new Java Message Service destinations for application clients
Programming