package org.mom4j.jms;
import junit.framework.Assert;
import java.util.ArrayList;
import javax.jms.DeliveryMode;
import javax.jms.Message;
import javax.jms.MapMessage;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueRequestor;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TemporaryQueue;
import javax.naming.Context;
import javax.naming.InitialContext;
/**
* Test various aspects of point to point messaging.
*
* @author Stefan
*/
public class TestP2P extends JmsTestCase {
private static int workerCount = 0;
//What happens when you call Message.acknowledge and then Session.rollback?
/** Creates new TestP2P */
public TestP2P(String name) {
super(name);
}
public void testMessageHeaders()
throws Exception
{
/* Create Naming-Context */
Context ctx = new InitialContext(namingProps);
/* Lookup 'administerable' Objects */
QueueConnectionFactory qcf = (QueueConnectionFactory)ctx.lookup("QueueConnectionFactory");
Queue q = (Queue)ctx.lookup("testQueue");
QueueConnection qc = null;
QueueSession s_send = null;
QueueSession s_rec = null;
try {
final long start = System.currentTimeMillis();
final String type = Long.toString(start);
/* Create a connection to the queue */
qc = qcf.createQueueConnection("system", "system");
/* Create a session on top of the connection which will be used only for
sending messages, transacted and with auto-acknowledge-mode*/
s_send = qc.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
/* Create a session on top of the connection which will be used only for
receiving messages, transacted and with auto-acknowledge-mode */
s_rec = qc.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
/* start the connection */
qc.start();
/* Create a receiver */
QueueReceiver qr = s_rec.createReceiver(q);
/* Create a sender for sending messages */
QueueSender qsender = s_send.createSender(q);
qsender.setDisableMessageTimestamp(false);
/* create a message for sending */
Message msg = s_send.createObjectMessage(new Integer(0));
msg.setJMSType(type);
/* Send the message */
qsender.send(msg);
Assert.assertNotNull("sender-JMSMessageID", msg.getJMSMessageID());
/* Commit the sending session */
s_send.commit();
/* Receive the message */
msg = qr.receiveNoWait();
Assert.assertNotNull("message recieved", msg);
/* Commit the session to clear the queue */
s_rec.commit();
/* Check, if the headers are set */
Assert.assertNull("JMSCorrelationID", msg.getJMSCorrelationID());
Assert.assertEquals("JMSDestination", q.getQueueName(), ((Queue)msg.getJMSDestination()).getQueueName());
Assert.assertEquals("JMSDeliveryMode", msg.getJMSDeliveryMode(), DeliveryMode.PERSISTENT);
Assert.assertEquals("JMSExpiration", Message.DEFAULT_TIME_TO_LIVE, msg.getJMSExpiration());
Assert.assertNotNull("JMSMessageID", msg.getJMSMessageID());
Assert.assertEquals("JMSPriority", Message.DEFAULT_PRIORITY, msg.getJMSPriority());
Assert.assertTrue("JMSRedelivered", !msg.getJMSRedelivered());
Assert.assertNull("JMSReplyTo", msg.getJMSReplyTo());
Assert.assertTrue("JMSTimestamp", start <= msg.getJMSTimestamp() &&
msg.getJMSTimestamp() <= System.currentTimeMillis());
Assert.assertEquals("JMSType", type, msg.getJMSType());
} finally {
try { s_rec.close(); } catch(Exception ex) {}
try { s_send.close(); } catch(Exception ex) {}
try { qc.close(); } catch(Exception ex) {}
}
}
public void testCreateQueue()
throws Exception
{
/* Create Naming-Context */
Context ctx = new InitialContext(namingProps);
/* Lookup 'administerable' Objects */
QueueConnectionFactory qcf = (QueueConnectionFactory)ctx.lookup("QueueConnectionFactory");
QueueConnection qc = null;
QueueSession qs = null;
try {
qc = qcf.createQueueConnection("system", "system");
qs = qc.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
String qn = "queue-" + System.currentTimeMillis();
Queue queue = qs.createQueue(qn);
Queue q = (Queue)ctx.lookup(qn);
Assert.assertNotNull("queue-handle", q);
Assert.assertEquals("queue-handle-name", qn, q.getQueueName());
TemporaryQueue tq = qs.createTemporaryQueue();
try {
ctx.lookup(tq.getQueueName());
Assert.fail("temporary queue must not be exposed to jndi");
} catch(javax.naming.NamingException ex) {
//as expected
}
QueueSender sender = qs.createSender(queue);
ObjectMessage om = qs.createObjectMessage(new Integer(99));
sender.send(om);
sender.close();
sender = qs.createSender(tq);
om = qs.createObjectMessage(new Integer(99));
sender.send(om);
sender.close();
} finally {
try { qs.close(); } catch(Exception ex) {}
try { qc.close(); } catch(Exception ex) {}
}
}
public void testMessageProperties()
throws Exception
{
/* Create Naming-Context */
Context ctx = new InitialContext(namingProps);
/* Lookup 'administerable' Objects */
QueueConnectionFactory qcf = (QueueConnectionFactory)ctx.lookup("QueueConnectionFactory");
Queue q = (Queue)ctx.lookup("testQueue");
QueueConnection qc = null;
QueueSession s_send = null;
QueueSession s_rec = null;
try {
final String PROP1 = "prop1";
final String PROP2 = "prop2";
final String PROP3 = "prop3";
final String PROP4 = "prop4";
final String PROP5 = "prop5";
final String PROP6 = "prop6";
final String PROP7 = "prop7";
final String PROP8 = "prop8";
final String PROP9 = "prop9";
final String VAL1 = "val1";
final String VAL2 = "val2";
final String VAL3 = "val3";
final long VAL4 = 102;
final float VAL5 = 100f;
final short VAL6 = 4;
final boolean VAL7 = true;
final int VAL8 = 15;
final double VAL9 = 512.23;
/* Create a connection to the queue */
qc = qcf.createQueueConnection("system", "system");
/* Create a session on top of the connection which will be used only for
sending messages, transacted and with auto-acknowledge-mode*/
s_send = qc.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
/* Create a session on top of the connection which will be used only for
receiving messages, transacted and with auto-acknowledge-mode */
s_rec = qc.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
/* start the connection */
qc.start();
/* Create a receiver */
QueueReceiver qr = s_rec.createReceiver(q);
/* Create a sender for sending messages */
QueueSender qsender = s_send.createSender(q);
/* create a message for sending */
Message msg = s_send.createObjectMessage(new Integer(0));
msg.setStringProperty(PROP1, VAL1);
msg.setStringProperty(PROP2, VAL2);
msg.setStringProperty(PROP3, VAL3);
msg.setLongProperty(PROP4, VAL4);
msg.setFloatProperty(PROP5, VAL5);
msg.setShortProperty(PROP6, VAL6);
msg.setBooleanProperty(PROP7, VAL7);
msg.setIntProperty(PROP8, VAL8);
msg.setDoubleProperty(PROP9, VAL9);
/* Send the message */
qsender.send(msg);
/* Commit the sending session */
s_send.commit();
/* Receive the message */
msg = qr.receiveNoWait();
/* Commit the session to clear the queue */
s_rec.commit();
/* Check, if the headers are set */
Assert.assertEquals(PROP1, VAL1, msg.getStringProperty(PROP1));
Assert.assertEquals(PROP2, VAL2, msg.getStringProperty(PROP2));
Assert.assertEquals(PROP3, VAL3, msg.getStringProperty(PROP3));
Assert.assertEquals(PROP4, VAL4, msg.getLongProperty(PROP4));
Assert.assertTrue(PROP5, VAL5 == msg.getFloatProperty(PROP5));
Assert.assertEquals(PROP6, VAL6, msg.getShortProperty(PROP6));
Assert.assertEquals(PROP7, VAL7, msg.getBooleanProperty(PROP7));
Assert.assertEquals(PROP8, VAL8, msg.getIntProperty(PROP8));
Assert.assertTrue(PROP9, VAL9 == msg.getDoubleProperty(PROP9));
} finally {
try { s_rec.close(); } catch(Exception ex) {}
try { s_send.close(); } catch(Exception ex) {}
try { qc.close(); } catch(Exception ex) {}
}
}
public void testSendTx()
throws Exception
{
int count = 0;
/* Create Naming-Context */
Context ctx = new InitialContext(namingProps);
/* Lookup 'administerable' Objects */
QueueConnectionFactory qcf = (QueueConnectionFactory)ctx.lookup("QueueConnectionFactory");
Queue q = (Queue)ctx.lookup("testQueue");
QueueConnection qc = null;
QueueSession s_send = null;
QueueSession s_rec = null;
try {
/* Create a connection to the queue */
qc = qcf.createQueueConnection("system", "system");
/* Create a session on top of the connection which will be used only for
sending messages, transacted and with auto-acknowledge-mode*/
s_send = qc.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
/* Create a session on top of the connection which will be used only for
receiving messages, transacted and with auto-acknowledge-mode */
s_rec = qc.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
/* start the connection */
qc.start();
/* Create a receiver and set a message-listener. This will be used to see,
whether messages are delivered before the session is commited */
QueueReceiver qr = s_rec.createReceiver(q);
ML ml = new ML();
qr.setMessageListener(ml);
/* Create a sender for sending messages */
QueueSender qsender = s_send.createSender(q);
/* create a couple of messages for sending */
final int num = 5;
Message[] msgs = new Message[num];
for(int i = 0; i < num; i++) {
msgs[i] = s_send.createObjectMessage(new Integer(i));
}
/* Send the messages */
for(int i = 0; i < num; i++) {
qsender.send(msgs[i]);
}
/* Wait 3 seconds, give the provider a bit of time ... */
Thread.sleep(3000);
/* None of the messages should be delivered so far, because the session
is not yet commited */
Assert.assertEquals((count++) + " - no of messages recieved", 0, ml.noOfMessagesReceived());
/* Discard messages sent */
s_send.rollback();
/* Wait 3 seconds, give the provider a bit of time ... */
Thread.sleep(3000);
/* None of the messages should be delivered so far, because the session
has been rollded back */
Assert.assertEquals((count++) + " - no of messages recieved", 0, ml.noOfMessagesReceived());
/* Resend the messages */
for(int i = 0; i < num; i++) {
qsender.send(msgs[i]);
}
/* Commit the sending session */
s_send.commit();
/* Wait 3 seconds, give the provider a bit of time ... */
Thread.sleep(3000);
/* The messages should be delivered so far, because the session
has been commited */
Assert.assertEquals((count++) + " - no of messages recieved", num, ml.noOfMessagesReceived());
/* Assure that the messages arrived in the right sequence */
for(int i = 0; i < num; i++) {
Integer I = (Integer)((ObjectMessage)ml.getReceivedMessage(i)).getObject();
Assert.assertEquals("message sequence", i, I.intValue());
}
/* Commit the receiving session, so all messages are finally cleared */
s_rec.commit();
} finally {
try { s_rec.close(); } catch(Exception ex) {}
try { s_send.close(); } catch(Exception ex) {}
try { qc.close(); } catch(Exception ex) {}
}
}
public void testAcknowledge()
throws Exception
{
int count = 0;
/* Create Naming-Context */
Context ctx = new InitialContext(namingProps);
/* Lookup 'administerable' Objects */
QueueConnectionFactory qcf = (QueueConnectionFactory)ctx.lookup("QueueConnectionFactory");
Queue q = (Queue)ctx.lookup("testQueue");
QueueConnection qc = null;
QueueSession s_send = null;
QueueSession s_rec = null;
try {
/* Create a connection to the queue */
qc = qcf.createQueueConnection("system", "system");
/* Create a session on top of the connection which will be used only for
sending messages, transacted and with auto-acknowledge-mode*/
s_send = qc.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
/* Create a session on top of the connection which will be used only for
receiving messages, transacted and with auto-acknowledge-mode */
s_rec = qc.createQueueSession(false, Session.CLIENT_ACKNOWLEDGE);
/* start the connection */
qc.start();
/* Create a receiver */
QueueReceiver qr = s_rec.createReceiver(q);
/* Create a sender for sending messages */
QueueSender qsender = s_send.createSender(q);
/* create a couple of messages for sending */
final int num = 5;
Message[] msgs = new Message[num];
for(int i = 0; i < num; i++) {
msgs[i] = s_send.createObjectMessage(new Integer(i));
}
/* Send the messages */
for(int i = 0; i < num; i++) {
qsender.send(msgs[i]);
}
/* Commit the sending session */
s_send.commit();
ObjectMessage msg = (ObjectMessage)qr.receiveNoWait();
Assert.assertNotNull("message", msg);
Assert.assertEquals("object value", new Integer(0), msg.getObject());
msg = (ObjectMessage)qr.receiveNoWait();
Assert.assertNotNull("message", msg);
Assert.assertEquals("object value", new Integer(1), msg.getObject());
msg = (ObjectMessage)qr.receiveNoWait();
Assert.assertNotNull("message", msg);
Assert.assertEquals("object value", new Integer(2), msg.getObject());
s_rec.recover();
msg = (ObjectMessage)qr.receiveNoWait();
Assert.assertNotNull("message", msg);
Assert.assertEquals("object value", new Integer(0), msg.getObject());
msg = (ObjectMessage)qr.receiveNoWait();
Assert.assertNotNull("message", msg);
Assert.assertEquals("object value", new Integer(1), msg.getObject());
msg.acknowledge();
msg = (ObjectMessage)qr.receiveNoWait();
Assert.assertNotNull("message", msg);
Assert.assertEquals("object value", new Integer(2), msg.getObject());
msg.acknowledge();
msg = (ObjectMessage)qr.receiveNoWait();
Assert.assertNotNull("message", msg);
Assert.assertEquals("object value", new Integer(3), msg.getObject());
s_rec.recover();
msg = (ObjectMessage)qr.receiveNoWait();
Assert.assertNotNull("message", msg);
Assert.assertEquals("object value", new Integer(3), msg.getObject());
msg.acknowledge();
msg = (ObjectMessage)qr.receiveNoWait();
Assert.assertNotNull("message", msg);
Assert.assertEquals("object value", new Integer(4), msg.getObject());
msg.acknowledge();
msg = (ObjectMessage)qr.receiveNoWait();
Assert.assertNull("message", msg);
} finally {
try { s_rec.close(); } catch(Exception ex) {}
try { s_send.close(); } catch(Exception ex) {}
try { qc.close(); } catch(Exception ex) {}
}
}
public void testSendTxMultiThreaded()
throws Exception
{
/* Create Naming-Context */
Context ctx = new InitialContext(namingProps);
/* Lookup 'administerable' Objects */
QueueConnectionFactory qcf = (QueueConnectionFactory)ctx.lookup("QueueConnectionFactory");
Queue q = (Queue)ctx.lookup("testQueue");
QueueConnection qc = null;
QueueSession s_rec = null;
try {
/* Create a connection to the queue */
qc = qcf.createQueueConnection("system", "system");
/* Create a session on top of the connection which will be used only for
receiving messages, transacted and with auto-acknowledge-mode */
s_rec = qc.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
/* start the connection */
qc.start();
/* Create a receiver and set a message-listener */
QueueReceiver qr = s_rec.createReceiver(q);
ML ml = new ML();
qr.setMessageListener(ml);
/* Create 'num' Worker-Threads, each of them sending 'num' messages */
final int num = 5;
Worker[] workers = new Worker[num];
for(int i = 0; i < num; i++) {
workers[i] = new Worker(num, qc, q);
}
/* Wait for the Threads to finish */
for(int i = 0; i < num; i++) {
workers[i].join();
}
/* give the provider a bit of time ... */
Thread.sleep(3000);
/* Assure that all messages arrived */
Assert.assertEquals("no of messages recieved", (num * num), ml.noOfMessagesReceived());
/* Assure that every thread has send the appropriate messages */
int result = 0;
int sum = 0;
for(int i = 0; i < num; i++) {
sum += i;
}
for(int i = 0; i < (num * num); i++) {
Integer I = (Integer)((ObjectMessage)ml.getReceivedMessage(i)).getObject();
result += I.intValue();
}
Assert.assertEquals("sum of message-values", (sum * num), result);
/* Commit the receiving session, so all messages are finally cleared */
s_rec.commit();
} finally {
try { s_rec.close(); } catch(Exception ex) {}
try { qc.close(); } catch(Exception ex) {}
}
}
public void testReceiveSyncTx()
throws Exception
{
Context ctx = new InitialContext(namingProps);
/* Lookup 'administerable' Objects */
QueueConnectionFactory qcf = (QueueConnectionFactory)ctx.lookup("QueueConnectionFactory");
Queue q = (Queue)ctx.lookup("testQueue");
QueueConnection qc = null;
QueueSession s_send = null;
QueueSession s_rec = null;
try {
/* Create a connection to the queue */
qc = qcf.createQueueConnection("system", "system");
/* Create a session on top of the connection which will be used only for
sending messages, transacted and with auto-acknowledge-mode*/
s_send = qc.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
/* Create a session on top of the connection which will be used only for
receiving messages, transacted and with auto-acknowledge-mode */
s_rec = qc.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
/* start the connection */
qc.start();
/* Create a receiver */
QueueReceiver qr = s_rec.createReceiver(q);
/* Create a sender for sending messages */
QueueSender qsender = s_send.createSender(q);
/* create a couple of messages for sending */
final int num = 5;
Message[] msgs = new Message[num];
for(int i = 0; i < num; i++) {
msgs[i] = s_send.createObjectMessage(new Integer(i));
}
/* Send the messages */
for(int i = 0; i < num; i++) {
qsender.send(msgs[i]);
}
/* Commit the sending session */
s_send.commit();
/* Receive the messages in the queue */
for(int i = 0; i < num; i++) {
ObjectMessage omsg = (ObjectMessage)qr.receiveNoWait();
Assert.assertNotNull(i + ":message", omsg);
Integer I = (Integer)omsg.getObject();
Assert.assertNotNull(i + ":integer", I);
Assert.assertEquals("Integer-Value", i, I.intValue());
Assert.assertTrue(i + ":redelivered", !omsg.getJMSRedelivered());
}
Message msg = qr.receiveNoWait();
Assert.assertNull("null-message", msg);
/* rollback session and receive message again */
s_rec.rollback();
/* AGAIN:Receive the 5 messages in the queue */
for(int i = 0; i < num; i++) {
ObjectMessage omsg = (ObjectMessage)qr.receiveNoWait();
Assert.assertNotNull(i + ":message-redeliver", omsg);
Integer I = (Integer)omsg.getObject();
Assert.assertNotNull(i + ":integer-redeliver", I);
Assert.assertEquals("Integer-Value-redeliver", i, I.intValue());
Assert.assertTrue(i + ":redelivered", omsg.getJMSRedelivered());
}
/* commit the session and clear messages */
s_rec.commit();
/* Check that there is no more message to receive */
msg = qr.receiveNoWait();
Assert.assertNull("null-message", msg);
} finally {
try { s_rec.close(); } catch(Exception ex) {}
try { s_send.close(); } catch(Exception ex) {}
try { qc.close(); } catch(Exception ex) {}
}
}
public void testReceiveAsyncTx()
throws Exception
{
Context ctx = new InitialContext(namingProps);
/* Lookup 'administerable' Objects */
QueueConnectionFactory qcf = (QueueConnectionFactory)ctx.lookup("QueueConnectionFactory");
Queue q = (Queue)ctx.lookup("testQueue");
QueueConnection qc = null;
QueueSession s_send = null;
QueueSession s_rec = null;
try {
/* Create a connection to the queue */
qc = qcf.createQueueConnection("system", "system");
/* Create a session on top of the connection which will be used only for
sending messages, transacted and with auto-acknowledge-mode*/
s_send = qc.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
/* Create a session on top of the connection which will be used only for
receiving messages, transacted and with auto-acknowledge-mode */
s_rec = qc.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
/* start the connection */
qc.start();
/* Create a receiver */
QueueReceiver qr = s_rec.createReceiver(q);
/* Create a sender for sending messages */
QueueSender qsender = s_send.createSender(q);
/* create a couple of messages for sending */
final int num = 5;
Message[] msgs = new Message[num];
for(int i = 0; i < num; i++) {
msgs[i] = s_send.createObjectMessage(new Integer(i));
}
/* Send the messages */
for(int i = 0; i < num; i++) {
qsender.send(msgs[i]);
}
/* Commit the sending session */
s_send.commit();
/* set the queue-receiver to receive the messages */
ML ml = new ML();
qr.setMessageListener(ml);
/* give the provider a bit of time to deliver the messages */
Thread.sleep(3000);
Assert.assertEquals("No of messages received", num, ml.noOfMessagesReceived());
/* Check the content of the messages */
for(int i = 0; i < num; i++) {
ObjectMessage omsg = (ObjectMessage)ml.getReceivedMessage(i);
Assert.assertNotNull(i + ":message", omsg);
Integer I = (Integer)omsg.getObject();
Assert.assertNotNull(i + ":integer", I);
Assert.assertEquals("Integer-Value", i, I.intValue());
Assert.assertTrue(i + ":redelivered", !omsg.getJMSRedelivered());
}
/* rollback session and receive message again */
ml.clearMessagesReceived();
s_rec.rollback();
/* AGAIN:Receive the messages in the queue */
/* give the provider a bit of time to deliver the messages */
Thread.sleep(3000);
Assert.assertEquals("No of messages received", num, ml.noOfMessagesReceived());
/* Check the content of the messages */
for(int i = 0; i < num; i++) {
ObjectMessage omsg = (ObjectMessage)ml.getReceivedMessage(i);
Assert.assertNotNull(i + ":message-redeliver", omsg);
Integer I = (Integer)omsg.getObject();
Assert.assertNotNull(i + ":integer-redeliver", I);
Assert.assertEquals("Integer-Value-redeliver", i, I.intValue());
Assert.assertTrue(i + ":redelivered", omsg.getJMSRedelivered());
}
/* commit the session and clear messages */
s_rec.commit();
} finally {
try { s_rec.close(); } catch(Exception ex) {}
try { s_send.close(); } catch(Exception ex) {}
try { qc.close(); } catch(Exception ex) {}
}
}
public void testTimeToLive()
throws Exception
{
/* Create Naming-Context */
Context ctx = new InitialContext(namingProps);
/* Lookup 'administerable' Objects */
QueueConnectionFactory qcf = (QueueConnectionFactory)ctx.lookup("QueueConnectionFactory");
Queue q = (Queue)ctx.lookup("testQueue");
QueueConnection qc = null;
QueueSession s_send = null;
QueueSession s_rec = null;
try {
/* Create a connection to the queue */
qc = qcf.createQueueConnection("system", "system");
/* Create a session on top of the connection which will be used only for
sending messages, transacted and with auto-acknowledge-mode*/
s_send = qc.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
/* Create a session on top of the connection which will be used only for
receiving messages, transacted and with auto-acknowledge-mode */
s_rec = qc.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
/* start the connection */
qc.start();
/* Create a receiver */
QueueReceiver qr = s_rec.createReceiver(q);
/* Create a sender for sending messages */
QueueSender qsender = s_send.createSender(q);
/* create a message for sending */
Message msg = s_send.createObjectMessage(new Integer(0));
/* Send the message with TTL of 20 seconds */
qsender.send(msg, DeliveryMode.PERSISTENT, Message.DEFAULT_PRIORITY, 20000);
/* Commit the sending session */
s_send.commit();
/* wait ... */
Thread.sleep(5000);
/* Receive the message */
msg = qr.receiveNoWait();
/* commit the receiving-session, in case we got something */
s_rec.commit();
Assert.assertNotNull("Expected to receive a message", msg);
Assert.assertTrue("TTL must be greater then current-time",
msg.getJMSExpiration() > System.currentTimeMillis());
/* create another message for sending */
msg = s_send.createObjectMessage(new Integer(0));
/* Send the message with TTL of 1 second */
qsender.send(msg, DeliveryMode.PERSISTENT, Message.DEFAULT_PRIORITY, 1000);
/* Commit the sending session */
s_send.commit();
/* wait, to make sure TTL takes effect */
Thread.sleep(10000);
/* Receive the message. Well, we do not expect to get one */
msg = qr.receiveNoWait();
/* commit the receiving-session, in case we got something */
s_rec.commit();
Assert.assertNull("Did not expect to receive a message", msg);
} finally {
try { s_rec.close(); } catch(Exception ex) {}
try { s_send.close(); } catch(Exception ex) {}
try { qc.close(); } catch(Exception ex) {}
}
}
public void testRequestReply()
throws Exception
{
/* Create Naming-Context */
Context ctx = new InitialContext(namingProps);
/* Lookup 'administerable' Objects */
QueueConnectionFactory qcf = (QueueConnectionFactory)ctx.lookup("QueueConnectionFactory");
Queue q = (Queue)ctx.lookup("testQueue");
QueueConnection qc = null;
QueueSession s_send = null;
QueueSession s_rec = null;
try {
/* Create a connection to the queue */
qc = qcf.createQueueConnection("system", "system");
/* Create a session for the requestor */
s_send = qc.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
/* Create a session for the receiver */
s_rec = qc.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
new ReplyThread(s_rec, q);
QueueRequestor requestor = new QueueRequestor(s_send, q);
Message msg = s_send.createObjectMessage("hello");
Message rec = requestor.request(msg);
Assert.assertEquals("requested message", "hello", ((ObjectMessage)rec).getObject());
requestor.close();
} finally {
try { s_rec.close(); } catch(Exception ex) {}
try { s_send.close(); } catch(Exception ex) {}
try { qc.close(); } catch(Exception ex) {}
}
}
public void testPriority()
throws Exception
{
/* Create Naming-Context */
Context ctx = new InitialContext(namingProps);
/* Lookup 'administerable' Objects */
QueueConnectionFactory qcf = (QueueConnectionFactory)ctx.lookup("QueueConnectionFactory");
Queue q = (Queue)ctx.lookup("testQueue");
QueueConnection qc = null;
QueueSession s_send = null;
QueueSession s_rec = null;
try {
/* Create a connection to the queue */
qc = qcf.createQueueConnection("system", "system");
/* Create a session on top of the connection which will be used only for
sending messages, transacted and with auto-acknowledge-mode*/
s_send = qc.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
/* Create a session on top of the connection which will be used only for
receiving messages, transacted and with auto-acknowledge-mode */
s_rec = qc.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
/* start the connection */
qc.start();
/* Create a receiver */
QueueReceiver qr = s_rec.createReceiver(q);
/* Create a sender for sending messages */
QueueSender qsender = s_send.createSender(q);
/* send messages with priority */
final int num = 10;
for(int i = 0; i < num; i++) {
Message msg = s_send.createObjectMessage(new Integer(i));
qsender.send(msg, DeliveryMode.PERSISTENT, i, 0);
}
/* Commit the sending session */
s_send.commit();
/* wait ... */
Thread.sleep(1000);
/* Receive the message */
for(int i = (num - 1); i >= 0; i--) {
ObjectMessage msg = (ObjectMessage)qr.receiveNoWait();
Integer I = (Integer)msg.getObject();
Assert.assertEquals("Message-Value", i, I.intValue());
}
/* commit the receiving-session, in case we got something */
s_rec.commit();
} finally {
try { s_rec.close(); } catch(Exception ex) {}
try { s_send.close(); } catch(Exception ex) {}
try { qc.close(); } catch(Exception ex) {}
}
}
public void testSelector()
throws Exception
{
/* Create Naming-Context */
Context ctx = new InitialContext(namingProps);
/* Lookup 'administerable' Objects */
QueueConnectionFactory qcf = (QueueConnectionFactory)ctx.lookup("QueueConnectionFactory");
Queue q = (Queue)ctx.lookup("testQueue");
QueueConnection qc = null;
QueueSession s_send = null;
QueueSession s_rec = null;
try {
/* Create a connection to the queue */
qc = qcf.createQueueConnection("system", "system");
/* Create a session on top of the connection which will be used only for
sending messages, transacted and with auto-acknowledge-mode*/
s_send = qc.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
/* Create a session on top of the connection which will be used only for
receiving messages, transacted and with auto-acknowledge-mode*/
s_rec = qc.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
/* start the connection */
qc.start();
/* Create some receivers, each with a different selector */
QueueReceiver qr_plain = s_rec.createReceiver(q);
QueueReceiver qr_aaa = s_rec.createReceiver(q, "myprop='aaa'");
QueueReceiver qr_bbb = s_rec.createReceiver(q, "myprop='bbb'");
/* Create a sender for sending messages */
QueueSender qsender = s_send.createSender(q);
/* send messages with priority */
int i = 0;
//send message 1
Message msg = s_send.createObjectMessage(new Integer(i++));
msg.setStringProperty("myprop", "aaa");
qsender.send(msg);
s_send.commit();
//consume message 1
msg = qr_bbb.receiveNoWait();
Assert.assertNull(msg);
s_rec.commit();
msg = qr_aaa.receiveNoWait();
Assert.assertNotNull(msg);
Assert.assertEquals("myprop", "aaa", msg.getStringProperty("myprop"));
s_rec.commit();
//send message 2
msg = s_send.createObjectMessage(new Integer(i++));
msg.setStringProperty("myprop", "bbb");
qsender.send(msg);
s_send.commit();
//consume message 2
msg = qr_aaa.receiveNoWait();
Assert.assertNull(msg);
s_rec.commit();
msg = qr_bbb.receiveNoWait();
Assert.assertNotNull(msg);
Assert.assertEquals("myprop", "bbb", msg.getStringProperty("myprop"));
s_rec.commit();
//send message 3
msg = s_send.createObjectMessage(new Integer(i++));
msg.setStringProperty("myprop", "ccc");
qsender.send(msg);
s_send.commit();
//consume message 3
msg = qr_aaa.receiveNoWait();
Assert.assertNull(msg);
msg = qr_bbb.receiveNoWait();
Assert.assertNull(msg);
s_rec.commit();
msg = qr_plain.receiveNoWait();
Assert.assertNotNull(msg);
Assert.assertEquals("myprop", "ccc", msg.getStringProperty("myprop"));
s_rec.commit();
//send message 4
msg = s_send.createObjectMessage(new Integer(i++));
qsender.send(msg);
s_send.commit();
//consume message 4
msg = qr_aaa.receiveNoWait();
Assert.assertNull(msg);
s_rec.commit();
msg = qr_bbb.receiveNoWait();
Assert.assertNull(msg);
s_rec.commit();
msg = qr_plain.receiveNoWait();
Assert.assertNotNull(msg);
Assert.assertNull("myprop", msg.getStringProperty("myprop"));
s_rec.commit();
//send message 5
msg = s_send.createObjectMessage(new Integer(i++));
msg.setStringProperty("myprop", "aaa");
qsender.send(msg);
s_send.commit();
//consume message 5
msg = qr_bbb.receiveNoWait();
Assert.assertNull(msg);
s_rec.commit();
msg = qr_aaa.receiveNoWait();
Assert.assertNotNull(msg);
Assert.assertEquals("myprop", "aaa", msg.getStringProperty("myprop"));
s_rec.commit();
//send message 6
msg = s_send.createObjectMessage(new Integer(i++));
msg.setStringProperty("myprop", "ccc");
qsender.send(msg);
s_send.commit();
//consume message 6
msg = qr_aaa.receiveNoWait();
Assert.assertNull(msg);
s_rec.commit();
msg = qr_bbb.receiveNoWait();
Assert.assertNull(msg);
s_rec.commit();
msg = qr_plain.receiveNoWait();
Assert.assertNotNull(msg);
Assert.assertEquals("myprop", "ccc", msg.getStringProperty("myprop"));
s_rec.commit();
} finally {
try { s_rec.close(); } catch(Exception ex) {}
try { s_send.close(); } catch(Exception ex) {}
try { qc.close(); } catch(Exception ex) {}
}
}
public void testMapMessage() throws Exception {
int count = 0;
/* Create Naming-Context */
Context ctx = new InitialContext(namingProps);
/* Lookup 'administerable' Objects */
QueueConnectionFactory qcf = (QueueConnectionFactory)ctx.lookup("QueueConnectionFactory");
Queue q = (Queue)ctx.lookup("testQueue");
QueueConnection qc = null;
QueueSession s_send = null;
QueueSession s_rec = null;
try {
/* Create a connection to the queue */
qc = qcf.createQueueConnection("system", "system");
/* Create a session on top of the connection which will be used only for
sending messages, transacted and with auto-acknowledge-mode*/
s_send = qc.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
/* Create a session on top of the connection which will be used only for
receiving messages, transacted and with auto-acknowledge-mode */
s_rec = qc.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
/* start the connection */
qc.start();
/* Create a receiver and set a message-listener. This will be used to see,
whether messages are delivered before the session is commited */
QueueReceiver qr = s_rec.createReceiver(q);
/* Create a sender for sending messages */
QueueSender qsender = s_send.createSender(q);
MapMessage msg = s_send.createMapMessage();
msg.setBoolean("boolean", true);
msg.setByte("byte", (byte)8);
msg.setBytes("bytes", new byte[] {1,2,3,4});
/* Send the messages */
qsender.send(msg);
/* Wait 3 seconds, give the provider a bit of time ... */
Thread.sleep(3000);
/* Commit the sending session */
s_send.commit();
/* Wait 3 seconds, give the provider a bit of time ... */
Thread.sleep(3000);
/* Receive the message */
Message message = qr.receiveNoWait();
/* Commit the session to clear the queue */
s_rec.commit();
//map message?
Assert.assertTrue(message instanceof MapMessage);
msg = (MapMessage)message;
Assert.assertNotNull(msg.getObject("boolean"));
Assert.assertNotNull(msg.getObject("byte"));
} finally {
try { s_rec.close(); } catch(Exception ex) {}
try { s_send.close(); } catch(Exception ex) {}
try { qc.close(); } catch(Exception ex) {}
}
}
class ML implements MessageListener {
private ArrayList messagesReceived;
private int msgCount;
public ML() {
messagesReceived = new ArrayList();
msgCount = 0;
}
public void onMessage(Message msg) {
msgCount++;
messagesReceived.add(msg);
}
public int noOfMessagesReceived() {
return messagesReceived.size();
}
public void clearMessagesReceived() {
messagesReceived.clear();
if(messagesReceived.size() != 0) {
throw new IllegalStateException("ArrayList-size:"
+ messagesReceived.size());
}
}
public Message getReceivedMessage(int index) {
return (Message)messagesReceived.get(index);
}
}
class ReplyThread extends Thread {
private QueueSession session;
private Queue queue;
public ReplyThread(QueueSession session, Queue queue) {
this.session = session;
this.queue = queue;
this.start();
}
public void run() {
QueueReceiver rec = null;
QueueSender sender = null;
try {
rec = this.session.createReceiver(this.queue);
ObjectMessage msg = (ObjectMessage)rec.receive();
ObjectMessage ret = this.session.createObjectMessage(msg.getObject());
Queue q = (Queue)msg.getJMSReplyTo();
sender = this.session.createSender(q);
sender.send(ret);
this.session.commit();
} catch(Exception ex) {
ex.printStackTrace();
} finally {
try { rec.close(); } catch(Exception ex) {}
try { sender.close(); } catch(Exception ex) {}
}
}
}
class Worker extends Thread {
private QueueConnection queueConnection;
private Queue queue;
private int num;
public Worker(int n, QueueConnection con, Queue q) {
super("Worker#" + (workerCount++));
this.queueConnection = con;
this.queue = q;
this.num = n;
start();
}
public void run() {
QueueSession qs = null;
try {
/* Create a session on top of the connection which will be used only for
sending messages, transacted and with auto-acknowledge-mode*/
qs = queueConnection.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
/* Create a sender for sending messages */
QueueSender qsender = qs.createSender(queue);
/* create a couple of messages for sending */
Message[] msgs = new Message[num];
for(int i = 0; i < num; i++) {
msgs[i] = qs.createObjectMessage(new Integer(i));
}
/* Send the messages */
for(int i = 0; i < num; i++) {
qsender.send(msgs[i]);
sleep(100);
}
/* Wait 3 seconds, give the provider a bit of time ... */
sleep(3000);
/* Discard messages sent */
qs.rollback();
/* Wait 3 seconds, give the provider a bit of time ... */
Thread.sleep(3000);
/* Resend the messages */
for(int i = 0; i < num; i++) {
qsender.send(msgs[i]);
sleep(100);
}
/* Commit the sending session */
qs.commit();
} catch(Exception ex) {
ex.printStackTrace();
} finally {
try { qs.close(); } catch(Exception ex) {}
}
}
}
}