J2EE JMS
Link http://java.sun.com/products/jms/tutorial/1_3_1-fcs/doc/client.html#1025249
Steps for JMS:
1. Set Reciver and Sender path and classpath in seperateconsoles as below
a. D:\Reciever>set J2EE_HOME=D:\j2sdkee1.3.1
D:\Reciever>setJAVA_HOME=D:\jdk1.3.1_04
D:\Reciever>setpath=%J2EE_HOME%\bin;%JAVA_HOME%\bin
D:\Reciever>setclasspath=%J2EE_HOME%\lib\j2ee.jar;%J2EE_HOME%\lib\locale;D:\Sender;D:\Reciever
2. Compile Sender and Reciver code which is attached.
3. In new consoles set the above path and start the j2eeserver D:\j2sdkee1.3.1\bin>j2ee –verbose
4. In botht the screen paste j2eeadmin-addJmsDestination MyQueue queue
5. D:\Sender>java-Djms.properties=%J2EE_HOME%\config\jms_client.properties SimpleQueueSenderMyQueue 3
6. D:\Reciever>java-Djms.properties=%J2EE_HOME%\config\jms_client.properties SimpleQueueReceiverMyQueue
JMS Ready
SimpleQueueSender.java
import javax.jms.*;
import javax.naming.*;
public class SimpleQueueSender {
/**
* Main method.
*
* @param args the queue used by the example and,
* optionally, the number of messages to send
*/
public static void main(String[] args) {
String queueName = null;
Context jndiContext = null;
QueueConnectionFactory queueConnectionFactory = null;
QueueConnection queueConnection = null;
QueueSession queueSession = null;
Queue queue = null;
QueueSender queueSender = null;
TextMessage message = null;
final int NUM_MSGS;
if ( (args.length < 1) || (args.length > 2) ) {
System.out.println("Usage: java SimpleQueueSender " +
"<queue-name> [<number-of-messages>]");
System.exit(1);
}
queueName = new String(args[0]);
System.out.println("Queue name is " + queueName);
if (args.length == 2){
NUM_MSGS = (new Integer(args[1])).intValue();
} else {
NUM_MSGS = 1;
}
/*
* Create a JNDI API InitialContext object if none exists
* yet.
*/
try {
jndiContext = new InitialContext();
} catch (NamingException e) {
System.out.println("Could not create JNDI API " +
"context: " + e.toString());
System.exit(1);
}
/*
* Look up connection factory and queue. If either does
* not exist, exit.
*/
try {
queueConnectionFactory = (QueueConnectionFactory)
jndiContext.lookup("QueueConnectionFactory");
queue = (Queue) jndiContext.lookup(queueName);
} catch (NamingException e) {
System.out.println("JNDI API lookup failed: " +
e.toString());
System.exit(1);
}
/*
* Create connection.
* Create session from connection; false means session is
* not transacted.
* Create sender and text message.
* Send messages, varying text slightly.
* Send end-of-messages message.
* Finally, close connection.
*/
try {
queueConnection =
queueConnectionFactory.createQueueConnection();
queueSession =
queueConnection.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);
queueSender = queueSession.createSender(queue);
message = queueSession.createTextMessage();
for (int i = 0; i < NUM_MSGS; i++) {
message.setText("This is message " + (i + 1));
System.out.println("Sending message: " +
message.getText());
queueSender.send(message);
}
/*
* Send a non-text control message indicating end of
* messages.
*/
queueSender.send(queueSession.createMessage());
} catch (JMSException e) {
System.out.println("Exception occurred: " +
e.toString());
} finally {
if (queueConnection != null) {
try {
queueConnection.close();
} catch (JMSException e) {}
}
}
}
}
SimpleQueueReciever
import javax.jms.*;
import javax.naming.*;
public class SimpleQueueReceiver {
/**
* Main method.
*
* @param args the queue used by the example
*/
public static void main(String[] args) {
String queueName = null;
Context jndiContext = null;
QueueConnectionFactory queueConnectionFactory = null;
QueueConnection queueConnection = null;
QueueSession queueSession = null;
Queue queue = null;
QueueReceiver queueReceiver = null;
TextMessage message = null;
/*
* Read queue name from command line and display it.
*/
if (args.length != 1) {
System.out.println("Usage: java " +
"SimpleQueueReceiver <queue-name>");
System.exit(1);
}
queueName = new String(args[0]);
System.out.println("Queue name is " + queueName);
/*
* Create a JNDI API InitialContext object if none exists
* yet.
*/
try {
jndiContext = new InitialContext();
} catch (NamingException e) {
System.out.println("Could not create JNDI API " +
"context: " + e.toString());
System.exit(1);
}
/*
* Look up connection factory and queue. If either does
* not exist, exit.
*/
try {
queueConnectionFactory = (QueueConnectionFactory)
jndiContext.lookup("QueueConnectionFactory");
queue = (Queue) jndiContext.lookup(queueName);
} catch (NamingException e) {
System.out.println("JNDI API lookup failed: " +
e.toString());
System.exit(1);
}
/*
* Create connection.
* Create session from connection; false means session is
* not transacted.
* Create receiver, then start message delivery.
* Receive all text messages from queue until
* a non-text message is received indicating end of
* message stream.
* Close connection.
*/
try {
queueConnection =
queueConnectionFactory.createQueueConnection();
queueSession =
queueConnection.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);
queueReceiver = queueSession.createReceiver(queue);
queueConnection.start();
while (true) {
Message m = queueReceiver.receive(1);
if (m != null) {
if (m instanceof TextMessage) {
message = (TextMessage) m;
System.out.println("Reading message: " +
message.getText());
} else {
break;
}
}
}
} catch (JMSException e) {
System.out.println("Exception occurred: " +
e.toString());
} finally {
if (queueConnection != null) {
try {
queueConnection.close();
} catch (JMSException e) {}
}
}
}
}
Link http://java.sun.com/products/jms/tutorial/1_3_1-fcs/doc/client.html#1025249
Steps for JMS:
1. Set Reciver and Sender path and classpath in seperateconsoles as below
a. D:\Reciever>set J2EE_HOME=D:\j2sdkee1.3.1
D:\Reciever>setJAVA_HOME=D:\jdk1.3.1_04
D:\Reciever>setpath=%J2EE_HOME%\bin;%JAVA_HOME%\bin
D:\Reciever>setclasspath=%J2EE_HOME%\lib\j2ee.jar;%J2EE_HOME%\lib\locale;D:\Sender;D:\Reciever
2. Compile Sender and Reciver code which is attached.
3. In new consoles set the above path and start the j2eeserver D:\j2sdkee1.3.1\bin>j2ee –verbose
4. In botht the screen paste j2eeadmin-addJmsDestination MyQueue queue
5. D:\Sender>java-Djms.properties=%J2EE_HOME%\config\jms_client.properties SimpleQueueSenderMyQueue 3
6. D:\Reciever>java-Djms.properties=%J2EE_HOME%\config\jms_client.properties SimpleQueueReceiverMyQueue
JMS Ready
SimpleQueueSender.java
import javax.jms.*;
import javax.naming.*;
public class SimpleQueueSender {
/**
* Main method.
*
* @param args the queue used by the example and,
* optionally, the number of messages to send
*/
public static void main(String[] args) {
String queueName = null;
Context jndiContext = null;
QueueConnectionFactory queueConnectionFactory = null;
QueueConnection queueConnection = null;
QueueSession queueSession = null;
Queue queue = null;
QueueSender queueSender = null;
TextMessage message = null;
final int NUM_MSGS;
if ( (args.length < 1) || (args.length > 2) ) {
System.out.println("Usage: java SimpleQueueSender " +
"<queue-name> [<number-of-messages>]");
System.exit(1);
}
queueName = new String(args[0]);
System.out.println("Queue name is " + queueName);
if (args.length == 2){
NUM_MSGS = (new Integer(args[1])).intValue();
} else {
NUM_MSGS = 1;
}
/*
* Create a JNDI API InitialContext object if none exists
* yet.
*/
try {
jndiContext = new InitialContext();
} catch (NamingException e) {
System.out.println("Could not create JNDI API " +
"context: " + e.toString());
System.exit(1);
}
/*
* Look up connection factory and queue. If either does
* not exist, exit.
*/
try {
queueConnectionFactory = (QueueConnectionFactory)
jndiContext.lookup("QueueConnectionFactory");
queue = (Queue) jndiContext.lookup(queueName);
} catch (NamingException e) {
System.out.println("JNDI API lookup failed: " +
e.toString());
System.exit(1);
}
/*
* Create connection.
* Create session from connection; false means session is
* not transacted.
* Create sender and text message.
* Send messages, varying text slightly.
* Send end-of-messages message.
* Finally, close connection.
*/
try {
queueConnection =
queueConnectionFactory.createQueueConnection();
queueSession =
queueConnection.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);
queueSender = queueSession.createSender(queue);
message = queueSession.createTextMessage();
for (int i = 0; i < NUM_MSGS; i++) {
message.setText("This is message " + (i + 1));
System.out.println("Sending message: " +
message.getText());
queueSender.send(message);
}
/*
* Send a non-text control message indicating end of
* messages.
*/
queueSender.send(queueSession.createMessage());
} catch (JMSException e) {
System.out.println("Exception occurred: " +
e.toString());
} finally {
if (queueConnection != null) {
try {
queueConnection.close();
} catch (JMSException e) {}
}
}
}
}
SimpleQueueReciever
import javax.jms.*;
import javax.naming.*;
public class SimpleQueueReceiver {
/**
* Main method.
*
* @param args the queue used by the example
*/
public static void main(String[] args) {
String queueName = null;
Context jndiContext = null;
QueueConnectionFactory queueConnectionFactory = null;
QueueConnection queueConnection = null;
QueueSession queueSession = null;
Queue queue = null;
QueueReceiver queueReceiver = null;
TextMessage message = null;
/*
* Read queue name from command line and display it.
*/
if (args.length != 1) {
System.out.println("Usage: java " +
"SimpleQueueReceiver <queue-name>");
System.exit(1);
}
queueName = new String(args[0]);
System.out.println("Queue name is " + queueName);
/*
* Create a JNDI API InitialContext object if none exists
* yet.
*/
try {
jndiContext = new InitialContext();
} catch (NamingException e) {
System.out.println("Could not create JNDI API " +
"context: " + e.toString());
System.exit(1);
}
/*
* Look up connection factory and queue. If either does
* not exist, exit.
*/
try {
queueConnectionFactory = (QueueConnectionFactory)
jndiContext.lookup("QueueConnectionFactory");
queue = (Queue) jndiContext.lookup(queueName);
} catch (NamingException e) {
System.out.println("JNDI API lookup failed: " +
e.toString());
System.exit(1);
}
/*
* Create connection.
* Create session from connection; false means session is
* not transacted.
* Create receiver, then start message delivery.
* Receive all text messages from queue until
* a non-text message is received indicating end of
* message stream.
* Close connection.
*/
try {
queueConnection =
queueConnectionFactory.createQueueConnection();
queueSession =
queueConnection.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);
queueReceiver = queueSession.createReceiver(queue);
queueConnection.start();
while (true) {
Message m = queueReceiver.receive(1);
if (m != null) {
if (m instanceof TextMessage) {
message = (TextMessage) m;
System.out.println("Reading message: " +
message.getText());
} else {
break;
}
}
}
} catch (JMSException e) {
System.out.println("Exception occurred: " +
e.toString());
} finally {
if (queueConnection != null) {
try {
queueConnection.close();
} catch (JMSException e) {}
}
}
}
}
No comments:
Post a Comment