Commit cb6c5ffa authored by afreyssin's avatar afreyssin
Browse files

Adds a samples using JMS 2.0 API.

parent 0ded5ea1
......@@ -297,6 +297,46 @@
<arg line="topic"/>
</java>
</target>
<!-- Runs the JMS2 consumer client for queue -->
<target name="jms2_consumer_queue" depends="simple_init"
description="--> Starts a Consumer client">
<java classname="jms2.Consumer" failonerror="no" fork="yes"
dir="${run.dir}">
<classpath path="${project.class.path}"/>
<arg line="queue"/>
</java>
</target>
<!-- Runs the JMS2 producer client for queue -->
<target name="jms2_producer_queue" depends="simple_init"
description="--> Starts a Producer client">
<java classname="jms2.Producer" failonerror="no" fork="yes"
dir="${run.dir}">
<classpath path="${project.class.path}"/>
<arg line="queue"/>
</java>
</target>
<!-- Runs the JMS2 consumer client for topic -->
<target name="jms2_consumer_topic" depends="simple_init"
description="--> Starts a Consumer client">
<java classname="jms2.Consumer" failonerror="no" fork="yes"
dir="${run.dir}">
<classpath path="${project.class.path}"/>
<arg line="topic"/>
</java>
</target>
<!-- Runs the JMS2 producer client for topic -->
<target name="jms2_producer_topic" depends="simple_init"
description="--> Starts a Producer client">
<java classname="jms2.Producer" failonerror="no" fork="yes"
dir="${run.dir}">
<classpath path="${project.class.path}"/>
<arg line="topic"/>
</java>
</target>
<!-- Runs the classic browser client -->
<target name="browser" depends="simple_init"
......
package jms2;
import java.util.Enumeration;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.jms.JMSContext;
import javax.jms.CompletionListener;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSConsumer;
import javax.jms.JMSException;
import javax.jms.JMSProducer;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
public class Consumer implements MessageListener {
static Context ictx = null;
public static void main(String[] args) throws Exception {
ictx = new InitialContext();
Destination dest = (Destination) ictx.lookup(args[0]);
ConnectionFactory cf = (ConnectionFactory) ictx.lookup("cf");
ictx.close();
JMSContext context = cf.createContext();
JMSConsumer consumer = context.createConsumer(dest);
consumer.setMessageListener(new Consumer());
context.start();
System.in.read();
context.close();
System.out.println("Consumer closed.");
System.exit(0);
}
@Override
public void onMessage(Message msg) {
try {
Destination destination = msg.getJMSDestination();
Destination replyTo = msg.getJMSReplyTo();
int n = msg.getIntProperty("order");
System.out.println("Receives message #" + n + " from=" + destination);
if (msg instanceof TextMessage) {
System.out.println(" -- " + ((TextMessage) msg).getText());
}
} catch (JMSException jE) {
System.err.println("Exception in listener: " + jE);
}
}
}
package jms2;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.jms.JMSContext;
import javax.jms.CompletionListener;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSConsumer;
import javax.jms.JMSException;
import javax.jms.JMSProducer;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
public class Producer implements CompletionListener{
static Context ictx = null;
public static void main(String[] args) throws Exception {
ictx = new InitialContext();
Destination dest = (Destination) ictx.lookup(args[0]);
ConnectionFactory cf = (ConnectionFactory) ictx.lookup("cf");
ictx.close();
JMSContext context = cf.createContext();
JMSProducer producer = context.createProducer();
producer.setAsync(new Producer());
context.start();
int i = 0;
for (; i < 10; i++) {
TextMessage msg = context.createTextMessage("Test number " + i);
msg.setIntProperty("order", i);
producer.send(dest, msg);
}
Thread.sleep(1000L);
context.close();
System.exit(0);
}
@Override
public void onCompletion(Message message) {
int n = -1;
try {
n = message.getIntProperty("order");
System.out.println(n + " messages sent.");
} catch (JMSException jE) {
jE.printStackTrace();
}
}
@Override
public void onException(Message message, Exception exc) {
int n = -1;
try {
n = message.getIntProperty("order");
System.out.println(n + " messages failed.");
exc.printStackTrace();
} catch (JMSException jE) {
jE.printStackTrace();
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment