Sending a message

Messages are sent using a MessageProducer. For point-to-point this is a QueueSender that is created using the createSender() method on QueueSession. A QueueSender is normally created for a specific Queue, so that all messages sent using that sender are sent to the same destination. Queue objects can be either created at runtime, or built and stored in a JNDI namespace. Refer to Using Java Naming and Directory Interface (JNDI), for details on how to store and retrieve objects using JNDI.

JMS provides a mechanism to create a Queue at runtime that minimizes the implementation-specific code in the application. This mechanism uses the QueueSession.createQueue() method, which takes a string parameter describing the destination. The string itself is still in an implementation-specific format, but this is a more flexible approach than directly referencing the implementation classes.

For MQe JMS the string is the name of the MQe queue. This can optionally contain the queue manager name. If the queue manager name is included, the queue name is separated from it by a plus sign '+', for example:
ioQueue = session.createQueue("myQM+myQueue"); 
This will create a JMS Queue representing the MQe queue "myQueue" on queue manager "myQM". If no queue manager name is specified the local queue manager is used, i.e. the one that JMS is connected to. For example:
String queueName = "SYSTEM.DEFAULT.LOCAL.QUEUE";

...

ioQueue = session.createQueue(queueName);  
This will create a JMS Queue representing the MQe queue SYSTEM.DEFAULT.LOCAL.QUEUE on the queue manager that the JMS Connection is using.

Message types

JMS provides several message types, each of which embodies some knowledge of its content. To avoid referencing the implementation-specific class names for the message types, methods are provided on the Session object for message creation. In the sample program, a text message is created in the following manner:
System.out.println("Creating a TextMessage");
TextMessage outMessage = session.createTextMessage();
System.out.println("Adding Text");
outMessage.setText(outString);
The message types that can be used are:
  • BytesMessage
  • ObjectMessage
  • TextMessage

Terms of use | WebSphere software

(c) Copyright IBM Corporation 2004, 2005. All rights reserved.