Demo HelloWorldMessage z adresáře
IMQ_HOME/demo/helloworld/helloworldmessage
má tyto
hlavní prvky (viz tutoriál):
Import the interfaces and Message Queue implementation classes for the JMS API.
The javax.jms package defines all the JMS interfaces necessary to develop a JMS client.
import javax.jms.*;
Instantiate a Message Queue QueueConnectionFactory administered object. A QueueConnectionFactory object encapsulates all the Message Queue-specific configuration properties for creating QueueConnection connections to a Message Queue server.
QueueConnectionFactory myQConnFactory =
new com.sun.messaging.QueueConnectionFactory();
ConnectionFactory administered objects can also be accessed through a JNDI lookup (see Looking Up ConnectionFactory Objects). This approach makes the client code JMS-provider independent and also allows for a centrally administered messaging system.
Create a connection to the message server. A QueueConnection
object is the active connection to the message server in the
Point-To-Point programming domain.
QueueConnection myQConn =
myQConnFactory.createQueueConnection();
Create a session within the connection. A QueueSession object
is a single-threaded context for producing and consuming messages.
It enables clients to create producers and consumers of messages for
a queue destination.
QueueSession myQSess = myQConn.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);
The myQSess object created above is non-transacted and automatically acknowledges messages upon consumption by a consumer.
Instantiate a Message Queue queue administered object that corresponds to a queue destination in the message server. Destination administered objects encapsulate provider-specific destination naming syntax and behavior. The code below instantiates a queue administered object for a physical queue destination named “world”.
Queue myQueue = new.com.sun.messaging.Queue("world");
Destination administered objects can also be accessed through a JNDI lookup (see Looking Up Destination Objects). This approach makes the client code JMS-provider independent and also allows for a centrally administered messaging system.
Create a QueueSender message producer. This message producer, associated with myQueue, is used to send messages to the queue destination named “world”.
QueueSender myQueueSender = myQSess.createSender(myQueue);
Create and send a message to the queue. You create a
TextMessage object using the QueueSession object and populate it
with a string representing the data of the message. Then you use the
QueueSender object to send the message to the “world” queue
destination.
TextMessage myTextMsg = myQSess.createTextMessage();
myTextMsg.setText("Hello World");
System.out.println(“Sending Message: “ + myTextMsg.getText());
myQueueSender.send(myTextMsg);
Create a QueueReceiver message consumer. This message consumer, associated with myQueue, is used to receive messages from the queue destination named “world”.
QueueReceiver myQueueReceiver =
myQSess.createReceiver(myQueue);
Start the QueueConnection you created in Step 3. Messages for consumption by a client can only be delivered over a connection that has been started (while messages produced by a client can be delivered to a destination without starting a connection, as in Step 7.
myQConn.start();
Receive a message from the queue. You receive a message from the “world” queue destination using the QueueReceiver object. The code, below, is an example of a synchronous consumption of messages (see Message Consumption: Synchronous and Asynchronous).
Message msg = myQueueReceiver.receive();
Retrieve the contents of the message. Once the message is received successfully, its contents can be retrieved.
if (msg instanceof TextMessage) {
TextMessage txtMsg = (TextMessage) msg;
System.out.println("Read Message: " + txtMsg.getText());
}
Close the session and connection resources.
myQSess.close();
myQConn.close();