JMS Step 3 - Using the QueueReceive.java Sample Program to Read a Message from a JMS Queue
By John-Brown.Evans on Nov 21, 2012
This post continues the series of JMS articles which demonstrate how to use JMS queues in a SOA context.
In the first post,
JMS Step 1 - How to Create a Simple JMS Queue in Weblogic Server 11g
we looked at how to create a JMS queue and its dependent objects in WebLogic Server.
JMS Step 1 - How to Create a Simple JMS Queue in Weblogic Server 11g
we looked at how to create a JMS queue and its dependent objects in WebLogic Server.
In the previous post,
JMS Step 2 - Using the QueueSend.java Sample Program to Send a Message to a JMS Queue
I showed how to write a message to that JMS queue using the QueueSend.java sample program.
JMS Step 2 - Using the QueueSend.java Sample Program to Send a Message to a JMS Queue
I showed how to write a message to that JMS queue using the QueueSend.java sample program.
In this article, we will use a similar sample, the QueueReceive.java program to read the message from that queue. Please review the previous posts if you have not already done so, as they contain prerequisites for executing the sample in this article.
1. Source code
The following java code will be used to read the message(s) from the JMS queue. As with the previous example, it is based on a sample program shipped with the WebLogic Server installation. The sample is not installed by default, but needs to be installed manually using the WebLogic Server Custom Installation option, together with many, other useful samples. You can either copy-paste the following code into your editor, or install all the samples.
The knowledge base article in My Oracle Support:
describes how to install the samples.
QueueReceive.java
package examples.jms.queue; import java.util.Hashtable; import javax.jms.*; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; /** * This example shows how to establish a connection to * and receive messages from a JMS queue. The classes in this * package operate on the same JMS queue. Run the classes together to * witness messages being sent and received, and to browse the queue * for messages. This class is used to receive and remove messages * from the queue. * * @author Copyright (c) 1999-2005 by BEA Systems, Inc. All Rights Reserved. */ public class QueueReceive implements MessageListener { // Defines the JNDI context factory. public final static String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory"; // Defines the JMS connection factory for the queue. public final static String JMS_FACTORY="jms/TestConnectionFactory"; // Defines the queue. public final static String QUEUE="jms/TestJMSQueue"; private QueueConnectionFactory qconFactory; private QueueConnection qcon; private QueueSession qsession; private QueueReceiver qreceiver; private Queue queue; private boolean quit = false; /** * Message listener interface. * @param msg message */ public void onMessage(Message msg) { try { String msgText; if (msg instanceof TextMessage) { msgText = ((TextMessage)msg).getText(); } else { msgText = msg.toString(); } System.out.println("Message Received: "+ msgText ); if (msgText.equalsIgnoreCase("quit")) { synchronized(this) { quit = true; this.notifyAll(); // Notify main thread to quit } } } catch (JMSException jmse) { System.err.println("An exception occurred: "+jmse.getMessage()); } } /** * Creates all the necessary objects for receiving * messages from a JMS queue. * * @param ctx JNDI initial context * @param queueName name of queue * @exception NamingException if operation cannot be performed * @exception JMSException if JMS fails to initialize due to internal error */ public void init(Context ctx, String queueName) throws NamingException, JMSException { qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY); qcon = qconFactory.createQueueConnection(); qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); queue = (Queue) ctx.lookup(queueName); qreceiver = qsession.createReceiver(queue); qreceiver.setMessageListener(this); qcon.start(); } /** * Closes JMS objects. * @exception JMSException if JMS fails to close objects due to internal error */ public void close()throws JMSException { qreceiver.close(); qsession.close(); qcon.close(); } /** * main() method. * * @param args WebLogic Server URL * @exception Exception if execution fails */ public static void main(String[] args) throws Exception { if (args.length != 1) { System.out.println("Usage: java examples.jms.queue.QueueReceive WebLogicURL"); return; } InitialContext ic = getInitialContext(args[0]); QueueReceive qr = new QueueReceive(); qr.init(ic, QUEUE); System.out.println( "JMS Ready To Receive Messages (To quit, send a \"quit\" message)."); // Wait until a "quit" message has been received. synchronized(qr) { while (! qr.quit) { try { qr.wait(); } catch (InterruptedException ie) {} } } qr.close(); } private static InitialContext getInitialContext(String url) throws NamingException { Hashtable |
2. How to Use This Class
2.1 From the file system on Linux
This section describes how to use the class from the file system of a WebLogic Server installation.
Log in to a machine with a WebLogic Server installation and create a directory to contain the source and code matching the package name, e.g. span>$HOME/examples/jms/queue. Copy the above QueueReceive.java file to this directory.
- Set the CLASSPATH and environment to match the WebLogic server environment.
Go to $MIDDLEWARE_HOME/user_projects/domains/base_domain/bin and execute
. ./setDomainEnv.sh - Collect the following information required to run the script:
- The JNDI name of the JMS queue to use
In the WebLogic server console > Services > Messaging > JMS Modules > Module name, (e.g.TestJMSModule) > JMS queue name, (e.g. TestJMSQueue)
select the queue and note its JNDI name,
e.g. jms/TestJMSQueue - The JNDI name of the connection factory to use to connect to the queue
Follow the same path as above to get the connection factory for the above queue, e.g. TestConnectionFactory and its JNDI name
e.g. jms/TestConnectionFactory - The URL and port of the WebLogic server running the above queueCheck the JMS server for the above queue and the managed server it is targeted to, for example soa_server1. Now find the port this managed server is listening on, by looking at its entry under Environment > Servers in the WLS console,
e.g. 8001
The URL for the server to be passed to the QueueReceive program will therefore be t3://host.domain:8001
e.g. t3://jbevans-lx.de.oracle.com:8001
- Edit Queue Receive .java and enter the above queue name and connection factory respectively under
... public final static String JMS_FACTORY="jms/TestConnectionFactory"; ... public final static String QUEUE="jms/TestJMSQueue"; ... |
- Compile Queue Receive .java using
javac Queue Receive .java - Go to the source’s top-level directory and execute it using
java examples.jms.queue.Queue Receive t3://jbevans-lx.de.oracle.com:8001 - This will print a message that it is ready to receive messages or to send a “quit” message to end.
- The program will read all messages in the queue and print them to the standard output until it receives a message with the payload “quit”.
2.2 From JDeveloper
The steps from JDeveloper are the same as those used for the previous program QueueSend.java, which is used to send a message to the queue. So we won't repeat them here. Please see the previous blog post at
JMS Step 2 - Using the QueueSend.java Sample Program to Send a Message to a JMS Queue
and apply the same steps in that example to the QueueReceive.java program.
JMS Step 2 - Using the QueueSend.java Sample Program to Send a Message to a JMS Queue
and apply the same steps in that example to the QueueReceive.java program.
This concludes the example. In the following post we will create a BPEL process which writes a message based on an XML schema to the queue.
1 comment:
cheap air jordan
michael kors handbags
adidas stan smith
michael kors uk
adidas stan smith men
fitflops sale
cheap nfl jerseys
christian louboutin shoes
nike air huarache
discount oakley sunglasses
huarache shoes
cheap basketball shoes
nike zoom
oakley sunglasses
http://www.oakley-sunglass.in.net
tiffany online
jordan shoes on sale
adidas nmd for sale
nike air zoom structure 19
yeezy shoes
air jordans,cheap air jordans,air jordan shoes,air jordan 11,air jordan 13,air jordan 6,air jordan 4
Post a Comment