package org.mom4j.jms;
import javax.jms.DeliveryMode;
import javax.jms.Message;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.jms.TemporaryTopic;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSubscriber;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;
import javax.naming.Context;
import javax.naming.InitialContext;
import junit.framework.Assert;
/**
* Test various aspects of publish/subscribe messaging.
*
* @author Stefan
*/
public class TestPubSub extends JmsTestCase {
/** Creates new TestPubSub */
public TestPubSub(String name) {
super(name);
}
public void testSelector()
throws Exception
{
/* Create Naming-Context */
Context ctx = new InitialContext(namingProps);
/* Lookup 'administerable' Objects */
TopicConnectionFactory tcf = (TopicConnectionFactory)ctx.lookup("TopicConnectionFactory");
Topic t = (Topic)ctx.lookup("testTopic");
TopicConnection tc = null;
TopicSession s_send = null;
TopicSession s_rec1 = null;
TopicSession s_rec2 = null;
try {
/* Create a connection to the topic */
tc = tcf.createTopicConnection("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 = tc.createTopicSession(true, Session.AUTO_ACKNOWLEDGE);
/* Create sessions on top of the connection which will be used only for
receiving messages, transacted and with auto-acknowledge-mode */
s_rec1 = tc.createTopicSession(true, Session.AUTO_ACKNOWLEDGE);
s_rec2 = tc.createTopicSession(true, Session.AUTO_ACKNOWLEDGE);
/* start the connection */
tc.start();
/* Create the receivers */
TopicSubscriber ts1 = s_rec1.createSubscriber(t, "size >= 5 OR (size < 5 AND color ='green')", false);
TopicSubscriber ts2 = s_rec1.createSubscriber(t);
TopicSubscriber ts3 = s_rec2.createSubscriber(t);
/* Create a sender for sending messages */
TopicPublisher tpub = s_send.createPublisher(t);
/* create a message for sending */
Message msg = s_send.createObjectMessage(new Integer(1));
msg.setIntProperty("size", 2);
msg.setStringProperty("color", "black");
//Publish the message
tpub.publish(msg);
s_send.commit();
Message msg1 = ts1.receiveNoWait();
Message msg2 = ts2.receiveNoWait();
Message msg3 = ts3.receiveNoWait();
s_rec1.commit();
s_rec2.commit();
//message-properties do not match the selector
Assert.assertNull(msg1);
//the other subsrcibers do not use selectors
Assert.assertNotNull(msg2);
Assert.assertEquals("msg2 - object", 1, ((Integer)((ObjectMessage)msg2).getObject()).intValue());
Assert.assertNotNull(msg3);
Assert.assertEquals("msg3 - object", 1, ((Integer)((ObjectMessage)msg3).getObject()).intValue());
/* create another message for sending */
msg = s_send.createObjectMessage(new Integer(88));
msg.setIntProperty("size", 2);
msg.setStringProperty("color", "green");
//Publish the message
tpub.publish(msg);
s_send.commit();
msg1 = ts1.receiveNoWait();
msg2 = ts2.receiveNoWait();
msg3 = ts3.receiveNoWait();
s_rec1.commit();
s_rec2.commit();
Assert.assertNotNull(msg1);
Assert.assertEquals("msg1 - object", 88, ((Integer)((ObjectMessage)msg1).getObject()).intValue());
Assert.assertNotNull(msg2);
Assert.assertEquals("msg2 - object", 88, ((Integer)((ObjectMessage)msg2).getObject()).intValue());
Assert.assertNotNull(msg3);
Assert.assertEquals("msg3 - object", 88, ((Integer)((ObjectMessage)msg3).getObject()).intValue());
} finally {
try { s_rec1.close(); } catch(Exception ex) {}
try { s_rec2.close(); } catch(Exception ex) {}
try { s_send.close(); } catch(Exception ex) {}
try { tc.close(); } catch(Exception ex) {}
}
}
public void testMessageHeaders()
throws Exception
{
/* Create Naming-Context */
Context ctx = new InitialContext(namingProps);
/* Lookup 'administerable' Objects */
TopicConnectionFactory tcf = (TopicConnectionFactory)ctx.lookup("TopicConnectionFactory");
Topic t = (Topic)ctx.lookup("testTopic");
TopicConnection tc = null;
TopicSession s_send = null;
TopicSession s_rec1 = null;
TopicSession s_rec2 = null;
try {
final long start = System.currentTimeMillis();
final String type = Long.toString(start);
/* Create a connection to the topic */
tc = tcf.createTopicConnection("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 = tc.createTopicSession(true, Session.AUTO_ACKNOWLEDGE);
/* Create sessions on top of the connection which will be used only for
receiving messages, transacted and with auto-acknowledge-mode */
s_rec1 = tc.createTopicSession(true, Session.AUTO_ACKNOWLEDGE);
s_rec2 = tc.createTopicSession(true, Session.AUTO_ACKNOWLEDGE);
/* start the connection */
tc.start();
/* Create the receivers */
TopicSubscriber ts1 = s_rec1.createSubscriber(t);
TopicSubscriber ts2 = s_rec1.createSubscriber(t);
TopicSubscriber ts3 = s_rec2.createSubscriber(t);
/* Create a sender for sending messages */
TopicPublisher tpub = s_send.createPublisher(t);
tpub.setDisableMessageTimestamp(false);
/* create a message for sending */
Message msg = s_send.createObjectMessage(new Integer(123));
msg.setJMSType(type);
/* Send the message */
tpub.publish(msg);
Assert.assertNotNull("sender-JMSMessageID", msg.getJMSMessageID());
/* Commit the sending session */
s_send.commit();
/* Receive the message */
Message msg1 = ts1.receiveNoWait();
Message msg2 = ts2.receiveNoWait();
Message msg3 = ts3.receiveNoWait();
Assert.assertNotNull("subscriber 1 - message recieved", msg1);
Assert.assertNotNull("subscriber 2 - message recieved", msg2);
Assert.assertNotNull("subscriber 3 - message recieved", msg3);
/* Commit the session 1 to clear the topic */
s_rec1.commit();
/* Rollback the session 2 */
s_rec2.rollback();
msg3 = ts3.receiveNoWait();
Assert.assertNotNull("subscriber 3 - message redelivered after rollback", msg3);
/* Commit the session 2 to clear the topic */
s_rec2.commit();
/* Check, if the headers are set for msg1 */
this.checkHeaders(msg1, t, start, type, false, msg.getJMSMessageID());
/* Check, if the headers are set for msg2 */
this.checkHeaders(msg2, t, start, type, false, msg.getJMSMessageID());
/* Check, if the headers are set for msg3 */
this.checkHeaders(msg3, t, start, type, true, msg.getJMSMessageID());
} finally {
try { s_rec1.close(); } catch(Exception ex) {}
try { s_rec2.close(); } catch(Exception ex) {}
try { s_send.close(); } catch(Exception ex) {}
try { tc.close(); } catch(Exception ex) {}
}
}
private void checkHeaders(Message msg, Topic t, long start, String type, boolean reDel, String msgId)
throws Exception
{
Assert.assertNull("JMSCorrelationID", msg.getJMSCorrelationID());
Assert.assertEquals("JMSDestination",
t.getTopicName(),
((Topic)msg.getJMSDestination()).getTopicName());
Assert.assertEquals("JMSDeliveryMode",
msg.getJMSDeliveryMode(),
DeliveryMode.PERSISTENT);
Assert.assertEquals("JMSExpiration",
Message.DEFAULT_TIME_TO_LIVE,
msg.getJMSExpiration());
Assert.assertEquals("JMSMessageID", msgId, msg.getJMSMessageID());
Assert.assertEquals("JMSPriority",
Message.DEFAULT_PRIORITY,
msg.getJMSPriority());
Assert.assertEquals("JMSRedelivered", reDel, msg.getJMSRedelivered());
Assert.assertNull("JMSReplyTo", msg.getJMSReplyTo());
Assert.assertTrue("JMSTimestamp",
start <= msg.getJMSTimestamp() &&
msg.getJMSTimestamp() <= System.currentTimeMillis());
Assert.assertEquals("JMSType", type, msg.getJMSType());
}
public void testCreateTopic()
throws Exception
{
/* Create Naming-Context */
Context ctx = new InitialContext(namingProps);
/* Lookup 'administerable' Objects */
TopicConnectionFactory tcf = (TopicConnectionFactory)ctx.lookup("TopicConnectionFactory");
TopicConnection tc = null;
TopicSession ts = null;
try {
tc = tcf.createTopicConnection("system", "system");
ts = tc.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
String tn = "topic-" + System.currentTimeMillis();
Topic topic = ts.createTopic(tn);
Topic t = (Topic)ctx.lookup(tn);
Assert.assertNotNull("topic-handle", t);
Assert.assertEquals("topic-handle-name", tn, t.getTopicName());
TemporaryTopic tt = ts.createTemporaryTopic();
try {
ctx.lookup(tt.getTopicName());
Assert.fail("temporary topic must not be exposed to jndi");
} catch(javax.naming.NamingException ex) {
//as expected
}
TopicPublisher pub = ts.createPublisher(topic);
ObjectMessage om = ts.createObjectMessage(new Integer(99));
pub.publish(om);
pub.close();
pub = ts.createPublisher(tt);
om = ts.createObjectMessage(new Integer(99));
pub.publish(om);
pub.close();
} finally {
try { ts.close(); } catch(Exception ex) {}
try { tc.close(); } catch(Exception ex) {}
}
}
public void testDurableSubscriptions()
throws Exception
{
final String name = "mysubscriber";
/* Create Naming-Context */
Context ctx = new InitialContext(namingProps);
/* Lookup 'administerable' Objects */
TopicConnectionFactory tcf = (TopicConnectionFactory)ctx.lookup("TopicConnectionFactory");
Topic topic = (Topic)ctx.lookup("testTopic");
TopicConnection tc = null;
TopicSession ts = null;
try {
tc = tcf.createTopicConnection("system", "system");
ts = tc.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
int num = 1000;
TopicPublisher tp = ts.createPublisher(topic);
ObjectMessage om = ts.createObjectMessage(new Integer(num++));
tp.publish(om);
om = ts.createObjectMessage(new Integer(num++));
tp.publish(om);
TopicSubscriber tsub = ts.createDurableSubscriber(topic, name);
Message msg = tsub.receiveNoWait();
Assert.assertNull("initial message", msg);
om = ts.createObjectMessage(new Integer(num++));
tp.publish(om);
om = ts.createObjectMessage(new Integer(num++));
tp.publish(om);
msg = tsub.receiveNoWait();
Assert.assertNotNull("fist message", msg);
Assert.assertEquals("fist message", new Integer(1002),
((ObjectMessage)msg).getObject());
msg = tsub.receiveNoWait();
Assert.assertNotNull("second message", msg);
Assert.assertEquals("second message", new Integer(1003),
((ObjectMessage)msg).getObject());
tsub.close();
om = ts.createObjectMessage(new Integer(num++));
tp.publish(om);
om = ts.createObjectMessage(new Integer(num++));
tp.publish(om);
tsub = ts.createDurableSubscriber(topic, name);
msg = tsub.receiveNoWait();
Assert.assertNotNull("third message", msg);
Assert.assertEquals("third message", new Integer(1004),
((ObjectMessage)msg).getObject());
msg = tsub.receiveNoWait();
Assert.assertNotNull("fourth message", msg);
Assert.assertEquals("fourth message", new Integer(1005),
((ObjectMessage)msg).getObject());
ts.unsubscribe(name);
om = ts.createObjectMessage(new Integer(num++));
tp.publish(om);
om = ts.createObjectMessage(new Integer(num++));
tp.publish(om);
tsub = ts.createDurableSubscriber(topic, name);
msg = tsub.receiveNoWait();
Assert.assertNull("after unsubscription", msg);
om = ts.createObjectMessage(new Integer(num++));
tp.publish(om);
msg = tsub.receiveNoWait();
Assert.assertNotNull("fith message", msg);
Assert.assertEquals("fith message", new Integer(1008),
((ObjectMessage)msg).getObject());
ts.unsubscribe(name);
tsub.close();
tp.close();
} finally {
try { ts.close(); } catch(Exception ex) {}
try { tc.close(); } catch(Exception ex) {}
}
}
}