{
conn1 = cf.createConnection();
Session sess1 = conn1.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer1 = sess1.createProducer(topic);
producer1.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
MessageConsumer consumer1 = sess1.createConsumer(topic, null, true);
conn2 = cf.createConnection();
Session sess2 = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
assertEquals(Session.AUTO_ACKNOWLEDGE, sess2.getAcknowledgeMode());
MessageConsumer consumer2 = sess2.createConsumer(topic, null, true);
// we need different session because we cannot access the same session from different
// threads
Session sess3 = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
assertEquals(Session.AUTO_ACKNOWLEDGE, sess3.getAcknowledgeMode());
MessageConsumer consumer3 = sess3.createConsumer(topic, null, true);
//Consumer 1 should not get the message but consumers 2 and 3 should
conn1.start();
conn2.start();
class TestRunnable implements Runnable
{
boolean exceptionThrown;
public Message m;
MessageConsumer consumer;
TestRunnable(MessageConsumer consumer)
{
this.consumer = consumer;
}
public void run()
{
try
{
m = consumer.receive(1500);
}
catch (Exception e)
{
exceptionThrown = true;
}
}
}
TestRunnable tr1 = new TestRunnable(consumer1);
TestRunnable tr2 = new TestRunnable(consumer2);
TestRunnable tr3 = new TestRunnable(consumer3);
Thread t1 = new Thread(tr1);
Thread t2 = new Thread(tr2);
Thread t3 = new Thread(tr3);
t1.start();
t2.start();
t3.start();
Message m2 = sess1.createTextMessage("Hello");
producer1.send(m2);
t1.join();
t2.join();
t3.join();