JMS Client Application
Setting Your Environment for Running Applications
Platform Variable Name Values
Microsoft Windows %JAVA_HOME% Directory in which the JDK installed
%J2EE_HOME% Directory in which the J2EE is installed. %CLASSPATH% Include the following:.;%J2EE_HOME%\lib \j2ee.jar;
%J2EE_HOME%\lib\locale
%PATH% Include %J2EE_HOME%\bin
UNIX $JAVA_HOME Directory in which the Java 2 SDK, Standard
Edition, version 1.3.1, is installed
$J2EE_HOME Directory in which the J2EE SDK 1.3.1 is installed, usually $HOME/j2sdkee1.3.1
$CLASSPATH Include the following: .:$J2EE_HOME/lib/j2ee.jar:
$J2EE_HOME/lib/locale
$PATH Include $J2EE_HOME/bin
A Simple Point-to-Point Example:
Writing the PTP Client Programs:
The sending program, Sender.java, performs the following steps:
1. Performs a Java Naming and Directory Interface (JNDI) API lookup of the
QueueConnectionFactory and queue
2. Creates a connection and a session
3. Creates a QueueSender
4. Creates a TextMessage
5. Sends one or more messages to the queue
6. Sends a control message to indicate the end of the message stream
7. Closes the connection in a finally block, automatically closing the session
and QueueSender
The receiving program, Receiver.java, performs the following steps:
1. Performs a JNDI API lookup of the QueueConnectionFactory and queue
2. Creates a connection and a session
3. Creates a QueueReceiver
4. Starts the connection, causing message delivery to begin
5. Receives the messages sent to the queue until the end-of-message-stream control
message is received
6. Closes the connection in a finally block, automatically closing the session
and QueueReceiver.
Sender.java
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import com.sun.messaging.QueueConnectionFactory;
import com.sun.messaging.jms.JMSException;
public class Sender {
public void sendData(){
try{
String imqAddressList = "mq://localhost:7676";
String queueName = "QueueTest";
System.out.println("sender sending data ...........");
String data = " welcome to jms";
QueueConnectionFactory queueFactory = new QueueConnectionFactory();
queueFactory.setProperty("imqAddressList", imqAddressList);
QueueConnection connection = queueFactory.createQueueConnection();
QueueSession session = connection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
Queue dest = session.createQueue(queueName);
QueueSender sender = session.createSender(dest);
sender.setDeliveryMode(DeliveryMode.PERSISTENT);
TextMessage msg = session.createTextMessage();
msg.setText(data);
sender.send(msg);
session.close();
connection.close();
}catch(JMSException je){
je.printStackTrace();
}catch(javax.jms.JMSException je){
je.printStackTrace();
}
}
}
Platform Variable Name Values
Microsoft Windows %JAVA_HOME% Directory in which the JDK installed
%J2EE_HOME% Directory in which the J2EE is installed. %CLASSPATH% Include the following:.;%J2EE_HOME%\lib \j2ee.jar;
%J2EE_HOME%\lib\locale
%PATH% Include %J2EE_HOME%\bin
UNIX $JAVA_HOME Directory in which the Java 2 SDK, Standard
Edition, version 1.3.1, is installed
$J2EE_HOME Directory in which the J2EE SDK 1.3.1 is installed, usually $HOME/j2sdkee1.3.1
$CLASSPATH Include the following: .:$J2EE_HOME/lib/j2ee.jar:
$J2EE_HOME/lib/locale
$PATH Include $J2EE_HOME/bin
A Simple Point-to-Point Example:
Writing the PTP Client Programs:
The sending program, Sender.java, performs the following steps:
1. Performs a Java Naming and Directory Interface (JNDI) API lookup of the
QueueConnectionFactory and queue
2. Creates a connection and a session
3. Creates a QueueSender
4. Creates a TextMessage
5. Sends one or more messages to the queue
6. Sends a control message to indicate the end of the message stream
7. Closes the connection in a finally block, automatically closing the session
and QueueSender
The receiving program, Receiver.java, performs the following steps:
1. Performs a JNDI API lookup of the QueueConnectionFactory and queue
2. Creates a connection and a session
3. Creates a QueueReceiver
4. Starts the connection, causing message delivery to begin
5. Receives the messages sent to the queue until the end-of-message-stream control
message is received
6. Closes the connection in a finally block, automatically closing the session
and QueueReceiver.
Sender.java
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import com.sun.messaging.QueueConnectionFactory;
import com.sun.messaging.jms.JMSException;
public class Sender {
public void sendData(){
try{
String imqAddressList = "mq://localhost:7676";
String queueName = "QueueTest";
System.out.println("sender sending data ...........");
String data = " welcome to jms";
QueueConnectionFactory queueFactory = new QueueConnectionFactory();
queueFactory.setProperty("imqAddressList", imqAddressList);
QueueConnection connection = queueFactory.createQueueConnection();
QueueSession session = connection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
Queue dest = session.createQueue(queueName);
QueueSender sender = session.createSender(dest);
sender.setDeliveryMode(DeliveryMode.PERSISTENT);
TextMessage msg = session.createTextMessage();
msg.setText(data);
sender.send(msg);
session.close();
connection.close();
}catch(JMSException je){
je.printStackTrace();
}catch(javax.jms.JMSException je){
je.printStackTrace();
}
}
}