1. cat send.java
package myjmssample;
import java.util.Properties;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
public class send {
public static void main(String[] args) {
String queueConnectionFactoryName = "JMSConnFactory";
String queueName = "JMSDestResource";
boolean transacted = false;
int acknowledgementMode = Session.AUTO_ACKNOWLEDGE;
String message="Message need to send";
Properties properties = new Properties();
properties.put( Context.PROVIDER_URL, "iiop://localhost:3700" );
properties.put( Context.INITIAL_CONTEXT_FACTORY,"com.sun.appserv.naming.S1ASCtxFactory" );
try {
Context context = new InitialContext(properties);
Object obj = context.lookup(queueConnectionFactoryName);
QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) obj;
obj = context.lookup(queueName);
Queue queue = (Queue) obj;
QueueConnection queueConnection=queueConnectionFactory.createQueueConnection();
queueConnection.start();
QueueSession queueSession = queueConnection.createQueueSession(transacted, acknowledgementMode);
TextMessage textMessage = queueSession.createTextMessage();
textMessage.clearBody();
textMessage.setText(message);
QueueSender queueSender = queueSession.createSender(queue);
queueSender.send(textMessage);
if (transacted) {
queueSession.commit();
}
if (queueSender != null) {
queueSender.close();
}
if (queueSession != null) {
queueSession.close();
}
if (queueConnection != null) {
queueConnection.close();
}
}
catch(Exception ex){
ex.printStackTrace();
}
}
}
2. cat recv.java
package myjmssample;
import java.util.Properties;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
public class recv implements MessageListener {
public void onMessage(Message message) {
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
try {
System.out.println("Message content is:" + textMessage.getText());
}
catch (JMSException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
recv msgRcvr=new recv();
String queueConnectionFactoryName = "JMSConnFactory";
String queueName = "JMSDestResource";
boolean transacted = false;
int acknowledgementMode = Session.AUTO_ACKNOWLEDGE;
Properties properties = new Properties();
properties.put( Context.PROVIDER_URL, "iiop://localhost:3700" );
properties.put( Context.INITIAL_CONTEXT_FACTORY,"com.sun.appserv.naming.S1ASCtxFactory" );
try {
Context context = new InitialContext(properties);
Object obj = context.lookup(queueConnectionFactoryName);
QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory)
obj;
obj = context.lookup(queueName);
Queue queue = (Queue) obj;
QueueConnection queueConnection = queueConnectionFactory.
createQueueConnection();
queueConnection.start();
QueueSession queueSession = queueConnection.createQueueSession(transacted,
acknowledgementMode);
QueueReceiver queueReceiver = queueSession.createReceiver(queue);
queueReceiver.setMessageListener(msgRcvr);
synchronized(msgRcvr){
msgRcvr.wait(100000);
}
if (queueReceiver != null) {
queueReceiver.close();
}
if (queueSession != null) {
queueSession.close();
}
if (queueConnection != null) {
queueConnection.close();
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
3. cat setClassPath.sh
#!/bin/ksh
export CLASSPATH=$CLASSPATH:/u/ainet/hui/openesb/glassfish-v2-ur2-b04-patch-20080616/lib/javaee.jar
export CLASSPATH=$CLASSPATH:/u/ainet/hui/openesb/glassfish-v2-ur2-b04-patch-20080616/lib/appserv-rt.jar
export CLASSPATH=$CLASSPATH:/u/ainet/hui/openesb/glassfish-v2-ur2-b04-patch-20080616/lib/appserv-admin.jar
export CLASSPATH=$CLASSPATH:/u/ainet/hui/openesb/glassfish-v2-ur2-b04-patch-20080616/lib/appserv-ext.jar
export CLASSPATH=$CLASSPATH:/u/ainet/hui/openesb/glassfish-v2-ur2-b04-patch-20080616/lib/install/applications/jmsra/imqjmsra.jar
export CLASSPATH=$CLASSPATH:./
4. compile
. setClassPath
javac myjmssample/send.java
javac myjmssample/recv.java
5. run
5.1 - Configure GlassFish JMS
create Connection Factories = " JMSConnFactory"
create Destination Resources = " JMSDestResource"
5.2 run java myjmssample/send
run java myjmssample/recv