JMS Step 3 - Using the QueueReceive.java Sample Program to Read a Message from a JMS Queue
- by John-Brown.Evans
JMS Step 3 - Using the QueueReceive.java Sample Program to Read a
Message from a JMS Queue
ol{margin:0;padding:0}
.c18_3{vertical-align:top;width:487.3pt;border-style:solid;background-color:#f3f3f3;border-color:#000000;border-width:1pt;padding:0pt 5pt 0pt 5pt}
.c20_3{vertical-align:top;width:487.3pt;border-style:solid;border-color:#ffffff;border-width:1pt;padding:5pt 5pt 5pt 5pt}
.c19_3{background-color:#ffffff}
.c17_3{list-style-type:circle;margin:0;padding:0}
.c12_3{list-style-type:disc;margin:0;padding:0}
.c6_3{font-style:italic;font-weight:bold}
.c10_3{color:inherit;text-decoration:inherit}
.c1_3{font-size:10pt;font-family:"Courier New"}
.c2_3{line-height:1.0;direction:ltr}
.c9_3{padding-left:0pt;margin-left:72pt}
.c15_3{padding-left:0pt;margin-left:36pt}
.c3_3{color:#1155cc;text-decoration:underline}
.c5_3{height:11pt}
.c14_3{border-collapse:collapse}
.c7_3{font-family:"Courier New"}
.c0_3{background-color:#ffff00}
.c16_3{font-size:18pt}
.c8_3{font-weight:bold}
.c11_3{font-size:24pt}
.c13_3{font-style:italic}
.c4_3{direction:ltr}
.title{padding-top:24pt;line-height:1.15;text-align:left;color:#000000;font-size:36pt;font-family:"Arial";font-weight:bold;padding-bottom:6pt}.subtitle{padding-top:18pt;line-height:1.15;text-align:left;color:#666666;font-style:italic;font-size:24pt;font-family:"Georgia";padding-bottom:4pt}
li{color:#000000;font-size:10pt;font-family:"Arial"}
p{color:#000000;font-size:10pt;margin:0;font-family:"Arial"}
h1{padding-top:0pt;line-height:1.15;text-align:left;color:#888;font-size:24pt;font-family:"Arial";font-weight:normal}
h2{padding-top:0pt;line-height:1.15;text-align:left;color:#888;font-size:18pt;font-family:"Arial";font-weight:normal}
h3{padding-top:0pt;line-height:1.15;text-align:left;color:#888;font-size:14pt;font-family:"Arial";font-weight:normal}
h4{padding-top:0pt;line-height:1.15;text-align:left;color:#888;font-size:12pt;font-family:"Arial";font-weight:normal}
h5{padding-top:0pt;line-height:1.15;text-align:left;color:#888;font-size:11pt;font-family:"Arial";font-weight:normal}
h6{padding-top:0pt;line-height:1.15;text-align:left;color:#888;font-size:10pt;font-family:"Arial";font-weight:normal}
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.
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.
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:
How To Install WebLogic Server and JMS Samples in WLS 10.3.x (Doc ID 1499719.1)
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 env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, url);
return new InitialContext(env);
}
}
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 queue
Check 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.
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.