Step 1 Step 2 Step 3 Step 4 Step 5 Step 6 Step 7

Developing a Basic JMS Application 5-13 } Create all the necessary objects for sending messages to a JMS queue. The ctx object is the JNDI initial context passed in by the main method. public void init Context ctx, String queueName throws NamingException, JMSException {

5.2.9.1 Step 1

Look up a connection factory in JNDI. qconFactory = QueueConnectionFactory ctx.lookupJMS_FACTORY;

5.2.9.2 Step 2

Create a connection using the connection factory. qcon = qconFactory.createQueueConnection;

5.2.9.3 Step 3

Create a session using the connection. The following code defines the session as non-transacted and specifies that messages will be acknowledged automatically. For more information about transacted sessions and acknowledge modes, see Section 2.4.3, Session. qsession = qcon.createQueueSessionfalse, Session.AUTO_ACKNOWLEDGE;

5.2.9.4 Step 4

Look up a destination queue in JNDI. queue = Queue ctx.lookupqueueName;

5.2.9.5 Step 5

Create a reference to a message producer queue sender using the session and destination queue. qsender = qsession.createSenderqueue;

5.2.9.6 Step 6

Create the message object. msg = qsession.createTextMessage;

5.2.9.7 Step 7

Start the connection. qcon.start; } Note: When setting up the JNDI initial context for an EJB or servlet, use the following method: Context ctx = new InitialContext; 5-14 Programming JMS for Oracle WebLogic Server The init method for the examples.jms.queue.QueueReceive example is similar to the QueueSend init method shown previously, with the one exception. Steps 5 and 6 would be replaced by the following code, respectively: qreceiver = qsession.createReceiverqueue; qreceiver.setMessageListenerthis; In the first line, instead of calling the createSender method to create a reference to the queue sender, the application calls the createReceiver method to create the queue receiver. In the second line, the message consumer registers an asynchronous message listener. When a message is delivered to the queue session, it is passed to the examples.jms.QueueReceive.onMessage method. The following code excerpt shows the onMessage interface from the QueueReceive example: public void onMessageMessage msg { try { String msgText; if msg instanceof TextMessage { msgText = TextMessagemsg.getText; } else { If it is not a TextMessage... msgText = msg.toString; } System.out.printlnMessage Received: + msgText ; if msgText.equalsIgnoreCasequit { synchronizedthis { quit = true; this.notifyAll; Notify main thread to quit } } } catch JMSException jmse { jmse.printStackTrace; } } The onMessage method processes messages received through the queue receiver. The method verifies that the message is a TextMessage and, if it is, prints the text of the message. If onMessage receives a different message type, it uses the messages toString method to display the message contents. For more information about the JMS classes used in this example, see Section 2.4, Understanding the JMS API or the javax.jms Javadoc, at http:www.java.sun.comproductsjmsdocs.html .

5.2.10 Example: Setting Up a PubSub Application