Sample JMS Queue Producer/Consumer
This section demonstrates simple JMS PTP and pub/sub message producers and consumers.
Configuring JMS
First, create a folder to use as a JMS file store in the file system. Create a folder called jms_store at the root level of your WebLogic Server installation.
Then, configure the JMS file store using the WebLogic Server Administration Console. Figure 71 shows a JMS store named "MyJMSFileStore" that is mapped to the directory \jms_store.
Figure 71 Creating the JMS Store
Creating the JMS Server
Next, the WebLogic Server administrator configures a JMS Server with the Administration Console. All JMS-administered objects such as destinations or connection factories exist within a JMS Server. Each WebLogic Server instance can include multiple JMS Server instances. Because these examples use persistent messages, the server administrator has specified a backing store, the file-based storage previously created (see Figure 72).
Figure 72 Creating the JMS Server
The server administrator can also create JMS connection factories at this time. Because these examples do not require any special configuration parameters, we can use the standard connection factories.
Administrator Creates JMS Destinations
After configuring the JMS Server, the server administrator creates the JMS destinations. These examples require a JMS queue named MessageQueue and a JMS topic named MessageTopic (see Figure 73).
Figure 73 Creating a JMS Queue
Sample JMS Queue Producer/Consumer
This example demonstrates a message producer, a synchronous message consumer, and an asynchronous message consumer for a JMS queue.
Message Producer
The message producer begins by creating a JNDI InitialContext and looking up the JMS QueueConnectionFactory. Because our examples use the standard JMS connection factories, it looks up "weblogic.jms.ConnectionFactory".
Context ctx = getInitialContext(); QueueConnectionFactory qConFactory = (QueueConnectionFactory) ctx.lookup("weblogic.jms.ConnectionFactory");
Now the MessageProducer looks up the JMS destination in JNDI. The server administrator created the JMS queue with the JNDI name "MessageQueue". The WebLogic Server binds the queue into the JNDI tree, making it available to clients.
Queue messageQueue = (Queue) ctx.lookup("MessageQueue");
The message producer now creates the QueueConnection and a QueueSession. The parameters to the createQueueSession call specify that it is nontransacted and uses automatic message acknowledgment. Using transactions with JMS and message acknowledgment are covered later in this chapter.
QueueConnection qCon = qConFactory.createQueueConnection(); QueueSession session = qCon.createQueueSession( false, /* not a transacted session */ Session.AUTO_ACKNOWLEDGE );
Finally, the message producer creates a sender and a JMS message. This message is a TextMessage and will include only a string.
sender = session.createSender(messageQueue); msg = session.createTextMessage();
The message producer is now prepared to send messages. The TextMessage's setText method attaches our Hello string to the message. The QueueSender's send method sends our message to the persistent queue. When the send call returns, the message producer may send another message.
msg.setText("Hello"); sender.send(msg);
Synchronous Message Consumer
The message consumer begins with an initialization code that is nearly identical to the message producer. The QueueConnectionFactory and queue are found in JNDI. Next, a QueueConnection and QueueSession are created.
Context ctx = getInitialContext(); QueueConnectionFactory qConFactory = (QueueConnectionFactory) ctx.lookup("weblogic.jms.ConnectionFactory"); Queue messageQueue = (Queue) ctx.lookup("MessageQueue"); QueueConnection qCon = qConFactory.createQueueConnection(); QueueSession session = qCon.createQueueSession( false, /* not a transacted session */ Session.AUTO_ACKNOWLEDGE );
The message consumer next creates a QueueReceiver from the QueueSession. Finally, the start method is called on the QueueConnection. Message delivery is inhibited until the start method is invoked. This allows a message consumer to finish initialization before messages become available.
receiver = session.createReceiver(messageQueue); qCon.start();
A synchronous message consumer uses the receive() method to ask JMS for the next message on this destination. The receive method blocks until a message is available on this destination. This client then uses the TextMessage's getText method to retrieve the message string and prints it to the screen.
msg = (TextMessage) receiver.receive(); System.err.println("Received: "+msg.getText());
Asynchronous Message Consumer
The asynchronous message consumer begins with the same set of initialization code as a synchronous message consumer.
Context ctx = getInitialContext(); QueueConnectionFactory qConFactory = (QueueConnectionFactory) ctx.lookup("weblogic.jms.ConnectionFactory"); Queue messageQueue = (Queue) ctx.lookup("MessageQueue"); QueueConnection qCon = qConFactory.createQueueConnection(); QueueSession session = qCon.createQueueSession( false, /* not a transacted session */ Session.AUTO_ACKNOWLEDGE ); receiver = session.createReceiver(messageQueue);
An asynchronous message consumer must use the receiver's setMessageListener method and pass an object that implements the javax.jms.MessageListener interface. Finally, the QueueConnection's start method begins message delivery.
receiver.setMessageListener(this); qCon.start();
Because this is an asynchronous consumer, the JMS implementation delivers messages by calling the onMessage method. This simple implementation just prints out the message's text and returns.
public void onMessage(Message m) { TextMessage msg = (TextMessage) m; System.err.println("Received: "+msg.getText()); }
Because message delivery is asynchronous, the client program is not blocked, waiting for messages to arrive. And, because our simple example program has no other work to perform, it exits before the messages can be delivered. To prevent this, we introduce an artificial wait to ensure that the program does not exit before the producer's messages are delivered.
Running the Queue Example
This example resides in the examples/ch7/queue directory on the accompanying CD-ROM. You can run the example from that directory.
Figure 74 Listing of the Queue Examples Directory
Before running the queue example, the WebLogic Server's JMS implementation must be configured. This example requires a JMS Server to be created with the Administration Console. Next, a JMS queue with the JNDI name of "MessageQueue" must be created. See the preceding examples or the WebLogic Server documentation for more information on creating and configuring JMS Servers.
The example can be built with the supplied build.cmd script for Windows NT or Windows 2000. UNIX users will need to modify the script to fit their environment. There also are runProducer.cmd, runSyncConsumer.cmd, and runAsyncConsumer.cmd scripts to run the message producer, synchronous consumer, and asynchronous consumer examples.
Open a command window and execute the runProducer.cmd script.
Figure 75 Running the runProducer Script
In another command window, run the runAsyncConsumer script. You can see in Figure 76 that the messages were received successfully.
Figure 76 Results of Running the runAsyncConsumer Script