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) {}
}
}