JMS Step 2 - Using the QueueSend.java Sample Program to Send a
Message to a JMS Queue
.c21_2{vertical-align:top;width:487.3pt;border-style:solid;border-color:#000000;border-width:1pt;padding:5pt 5pt 5pt 5pt}
.c15_2{vertical-align:top;width:487.3pt;border-style:solid;border-color:#ffffff;border-width:1pt;padding:5pt 5pt 5pt 5pt}
.c0_2{padding-left:0pt;direction:ltr;margin-left:36pt}
.c20_2{list-style-type:circle;margin:0;padding:0}
.c10_2{list-style-type:disc;margin:0;padding:0}
.c6_2{background-color:#ffffff}
.c17_2{padding-left:0pt;margin-left:72pt}
.c3_2{line-height:1.0;direction:ltr}
.c1_2{font-size:10pt;font-family:"Courier New"}
.c16_2{color:#1155cc;text-decoration:underline}
.c13_2{color:inherit;text-decoration:inherit}
.c7_2{background-color:#ffff00}
.c9_2{border-collapse:collapse}
.c2_2{font-family:"Courier New"}
.c18_2{font-size:18pt}
.c5_2{font-weight:bold}
.c19_2{color:#ff0000}
.c12_2{background-color:#f3f3f3;border-style:solid;border-color:#000000;border-width:1pt;}
.c14_2{font-size:24pt}
.c8_2{direction:ltr;background-color:#ffffff}
.c11_2{font-style:italic}
.c4_2{height:11pt}
.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;padding-bottom:0pt}
h2{padding-top:0pt;line-height:1.15;text-align:left;color:#888;font-size:18pt;font-family:"Arial";font-weight:normal;padding-bottom:0pt}
h3{padding-top:0pt;line-height:1.15;text-align:left;color:#888;font-size:14pt;font-family:"Arial";font-weight:normal;padding-bottom:0pt}
h4{padding-top:0pt;line-height:1.15;text-align:left;color:#888;font-size:12pt;font-family:"Arial";font-weight:normal;padding-bottom:0pt}
h5{padding-top:0pt;line-height:1.15;text-align:left;color:#888;font-size:11pt;font-family:"Arial";font-weight:normal;padding-bottom:0pt}
h6{padding-top:0pt;line-height:1.15;text-align:left;color:#888;font-size:10pt;font-family:"Arial";font-weight:normal;padding-bottom:0pt}
This post is the second in a series of JMS articles which
demonstrate how to use JMS queues in a SOA context.
In the previous post
JMS Step 1 - How to Create a Simple JMS Queue in Weblogic Server 11g
I showed you how to create a JMS queue and its dependent objects in
WebLogic Server. In this article, we will use a sample program to write a
message to that queue. Please review the previous post if you have not
created those objects yet, as they will be required later in this example.
The previous post also includes useful background information and links to
the Oracle documentation for addional research.
The following post in this series will show how to read the message
from the queue again.
1. Source code
The following java code will be used to write a message to the JMS queue.
It is based on a sample program provided 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.
QueueSend.java
package examples.jms.queue;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
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
* and send messages to the 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. The class is used to send messages to the queue.
*
* @author Copyright (c) 1999-2005 by BEA Systems, Inc. All Rights Reserved.
*/
public class QueueSend
{
// Defines the JNDI context factory.
public final static String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory";
// Defines the JMS context factory.
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 QueueSender qsender;
private Queue queue;
private TextMessage msg;
/**
* Creates all the necessary objects for sending
* messages to 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);
qsender = qsession.createSender(queue);
msg = qsession.createTextMessage();
qcon.start();
}
/**
* Sends a message to a JMS queue.
*
* @param message message to be sent
* @exception JMSException if JMS fails to send message due to internal error
*/
public void send(String message) throws JMSException {
msg.setText(message);
qsender.send(msg);
}
/**
* Closes JMS objects.
* @exception JMSException if JMS fails to close objects due to internal error
*/
public void close() throws JMSException {
qsender.close();
qsession.close();
qcon.close();
}
/** main() method.
*
* @param args WebLogic Server URL
* @exception Exception if operation fails
*/
public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.out.println("Usage: java examples.jms.queue.QueueSend WebLogicURL");
return;
}
InitialContext ic = getInitialContext(args[0]);
QueueSend qs = new QueueSend();
qs.init(ic, QUEUE);
readAndSend(qs);
qs.close();
}
private static void readAndSend(QueueSend qs)
throws IOException, JMSException
{
BufferedReader msgStream = new BufferedReader(new InputStreamReader(System.in));
String line=null;
boolean quitNow = false;
do {
System.out.print("Enter message (\"quit\" to quit): \n");
line = msgStream.readLine();
if (line != null && line.trim().length() != 0) {
qs.send(line);
System.out.println("JMS Message Sent: "+line+"\n");
quitNow = line.equalsIgnoreCase("quit");
}
} while (! quitNow);
}
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 UNIX/Linux
Log in to a machine with a WebLogic installation and create a
directory to contain the source and code matching the package name,
e.g. $HOME/examples/jms/queue. Copy the above QueueSend.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 a 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 a connection factory 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 namee.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 given to the QueueSend program in this example
will therefore be t3://host.domain:8001
e.g.
t3://jbevans-lx.de.oracle.com:8001
Edit QueueSend.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 QueueSend.java using
javac QueueSend.java
Go to the source’s top-level directory and execute it using
java examples.jms.queue.QueueSend
t3://jbevans-lx.de.oracle.com:8001
This will prompt for a text input or “quit” to end.
In the WLS console, go to the queue and select Monitoring to confirm
that a new message was written to the queue.
2.2 From JDeveloper
Create a new application in JDeveloper, called, for example
JMSTests.
When prompted for a project name, enter QueueSend and select Java
as the technology
Default Package = examples.jms.queue (but you can enter anything here as
you will overwrite it in the code later).
Leave the other values at their defaults.
Press Finish
Create a new Java class called QueueSend and use the default values
This will create a file called QueueSend.java.
Open QueueSend.java, if it is not already open and replace all its
contents with the QueueSend java code listed above
Some lines might have warnings due to unfound objects.
These are due to missing libraries in the JDeveloper project.
Add the following libraries to the JDeveloper project:
right-click the
QueueSend
project in the navigation menu and select
Libraries and Classpath
, then
Add JAR/Directory
Go to the folder containing the JDeveloper installation and
find/choose the file
javax.jms_1.1.1.jar
, e.g. at
D:\oracle\jdev11116\modules\javax.jms_1.1.1.jar
Do the same for the weblogic.jar file located, for example in
D:\oracle\jdev11116\wlserver_10.3\server\lib\weblogic.jar
Now you should be able to compile the project, for example by
selecting the Make or Rebuild icons
If you try to execute the project, you will get a usage message,
as it requires a parameter pointing to the WLS installation
containing the JMS queue, for example
t3://jbevans-lx.de.oracle.com:8001
. You can automatically pass this parameter to the program from
JDeveloper by editing the project’s Run/Debug/Profile.
Select the project properties, select Run/Debug/Profile and
edit the Default run configuration
and add the connection parameter to the Program
Arguments field
If you execute it again, you will see that it has
passed the parameter to the start command
If you get a
ClassNotFoundException
for the class
weblogic.jndi.WLInitialContextFactory
, then check that the weblogic.jar file was correctly added to the
project in one of the earlier steps above.
Set the values of JMS_FACTORY and QUEUE the same way as described
above in the description of how to use this from a Linux file
system, i.e.
...public final static String JMS_FACTORY="
jms/TestConnectionFactory
";
...
public final static String QUEUE="
jms/TestJMSQueue
";
...
You need to make one more change to the project. If you execute it
now, it will prompt for the payload for the JMS message, but you
won’t be able to enter it by default in JDeveloper. You need
to enable program input for the project first.
Select the project’s properties, then Tool Settings, then
check the Allow Program Input checkbox at the bottom and Save.
Now when you execute the project, you will get a text entry field
at the bottom into which you can enter the payload. You can enter multiple
messages until you enter “quit”, which will cause the
program to stop.
The following screen shot shows the TestJMSQueue’s
Monitoring page, after a message was sent to the queue:
This concludes the sample. In the following post I will show you how to
read the message from the queue again.