use of javax.jms.MessageListener in project activemq-artemis by apache.
the class JMSConsumerTest method testMessageListenerWithConsumerCanBeStopped.
public void testMessageListenerWithConsumerCanBeStopped() throws Exception {
final AtomicInteger counter = new AtomicInteger(0);
final CountDownLatch done1 = new CountDownLatch(1);
final CountDownLatch done2 = new CountDownLatch(1);
// Receive a message with the JMS API
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
destination = createDestination(session, destinationType);
ActiveMQMessageConsumer consumer = (ActiveMQMessageConsumer) session.createConsumer(destination);
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message m) {
counter.incrementAndGet();
if (counter.get() == 1) {
done1.countDown();
}
if (counter.get() == 2) {
done2.countDown();
}
}
});
// Send a first message to make sure that the consumer dispatcher is
// running
sendMessages(session, destination, 1);
assertTrue(done1.await(1, TimeUnit.SECONDS));
assertEquals(1, counter.get());
// Stop the consumer.
consumer.stop();
// Send a message, but should not get delivered.
sendMessages(session, destination, 1);
assertFalse(done2.await(1, TimeUnit.SECONDS));
assertEquals(1, counter.get());
// Start the consumer, and the message should now get delivered.
consumer.start();
assertTrue(done2.await(1, TimeUnit.SECONDS));
assertEquals(2, counter.get());
}
use of javax.jms.MessageListener in project activemq-artemis by apache.
the class JMSConsumerTest method testMessageListenerOnMessageCloseUnackedWithPrefetch1StayInQueue.
public void testMessageListenerOnMessageCloseUnackedWithPrefetch1StayInQueue() throws Exception {
final AtomicInteger counter = new AtomicInteger(0);
final CountDownLatch sendDone = new CountDownLatch(1);
final CountDownLatch got2Done = new CountDownLatch(1);
// Set prefetch to 1
connection.getPrefetchPolicy().setAll(1);
// This test case does not work if optimized message dispatch is used as
// the main thread send block until the consumer receives the
// message. This test depends on thread decoupling so that the main
// thread can stop the consumer thread.
connection.setOptimizedMessageDispatch(false);
connection.start();
// Use all the ack modes
Session session = connection.createSession(false, ackMode);
destination = createDestination(session, destinationType);
MessageConsumer consumer = session.createConsumer(destination);
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message m) {
try {
TextMessage tm = (TextMessage) m;
LOG.info("Got in first listener: " + tm.getText());
assertEquals("" + counter.get(), tm.getText());
counter.incrementAndGet();
if (counter.get() == 2) {
sendDone.await();
connection.close();
got2Done.countDown();
}
tm.acknowledge();
} catch (Throwable e) {
e.printStackTrace();
}
}
});
// Send the messages
sendMessages(session, destination, 4);
sendDone.countDown();
// Wait for first 2 messages to arrive.
assertTrue(got2Done.await(100000, TimeUnit.MILLISECONDS));
// Re-start connection.
connection = (ActiveMQConnection) factory.createConnection();
connections.add(connection);
connection.getPrefetchPolicy().setAll(1);
connection.start();
// Pickup the remaining messages.
final CountDownLatch done2 = new CountDownLatch(1);
session = connection.createSession(false, ackMode);
consumer = session.createConsumer(destination);
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message m) {
try {
TextMessage tm = (TextMessage) m;
LOG.info("Got in second listener: " + tm.getText());
// order is not guaranteed as the connection is started before the listener is set.
// assertEquals("" + counter.get(), tm.getText());
counter.incrementAndGet();
if (counter.get() == 4) {
done2.countDown();
}
} catch (Throwable e) {
LOG.error("unexpected ex onMessage: ", e);
}
}
});
assertTrue(done2.await(1000, TimeUnit.MILLISECONDS));
Thread.sleep(200);
// assert msg 2 was redelivered as close() from onMessages() will only ack in auto_ack and dups_ok mode
assertEquals(5, counter.get());
}
use of javax.jms.MessageListener in project activemq-artemis by apache.
the class JMSConsumerTest method testMessageListenerWithConsumerCanBeStoppedConcurently.
public void testMessageListenerWithConsumerCanBeStoppedConcurently() throws Exception {
final AtomicInteger counter = new AtomicInteger(0);
final CountDownLatch closeDone = new CountDownLatch(1);
connection.start();
Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
destination = createDestination(session, ActiveMQDestination.QUEUE_TYPE);
// preload the queue
sendMessages(session, destination, 2000);
final ActiveMQMessageConsumer consumer = (ActiveMQMessageConsumer) session.createConsumer(destination);
final Map<Thread, Throwable> exceptions = Collections.synchronizedMap(new HashMap<Thread, Throwable>());
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
LOG.error("Uncaught exception:", e);
exceptions.put(t, e);
}
});
final class AckAndClose implements Runnable {
private final Message message;
public AckAndClose(Message m) {
this.message = m;
}
@Override
public void run() {
try {
int count = counter.incrementAndGet();
if (count == 590) {
// close in a separate thread is ok by jms
consumer.close();
closeDone.countDown();
}
if (count % 200 == 0) {
// ensure there are some outstanding messages
// ack every 200
message.acknowledge();
}
} catch (Exception e) {
LOG.error("Exception on close or ack:", e);
exceptions.put(Thread.currentThread(), e);
}
}
}
final ExecutorService executor = Executors.newCachedThreadPool();
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message m) {
// ack and close eventually in separate thread
executor.execute(new AckAndClose(m));
}
});
assertTrue(closeDone.await(20, TimeUnit.SECONDS));
// await possible exceptions
Thread.sleep(1000);
assertTrue("no exceptions: " + exceptions, exceptions.isEmpty());
}
use of javax.jms.MessageListener in project activemq-artemis by apache.
the class JMSConsumerTest method testSetMessageListenerAfterStart.
public void testSetMessageListenerAfterStart() throws Exception {
final AtomicInteger counter = new AtomicInteger(0);
final CountDownLatch done = new CountDownLatch(1);
// Receive a message with the JMS API
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
destination = createDestination(session, destinationType);
MessageConsumer consumer = session.createConsumer(destination);
// Send the messages
sendMessages(session, destination, 4);
// See if the message get sent to the listener
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message m) {
counter.incrementAndGet();
if (counter.get() == 4) {
done.countDown();
}
}
});
assertTrue(done.await(1000, TimeUnit.MILLISECONDS));
Thread.sleep(200);
// Make sure only 4 messages were delivered.
assertEquals(4, counter.get());
}
use of javax.jms.MessageListener in project activemq-artemis by apache.
the class JMSConsumerTest method testMessageListenerWithConsumer.
public void testMessageListenerWithConsumer() throws Exception {
final AtomicInteger counter = new AtomicInteger(0);
final CountDownLatch done = new CountDownLatch(1);
// Receive a message with the JMS API
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
destination = createDestination(session, destinationType);
MessageConsumer consumer = session.createConsumer(destination);
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message m) {
counter.incrementAndGet();
if (counter.get() == 4) {
done.countDown();
}
}
});
// Send the messages
sendMessages(session, destination, 4);
assertTrue(done.await(1000, TimeUnit.MILLISECONDS));
Thread.sleep(200);
// Make sure only 4 messages were delivered.
assertEquals(4, counter.get());
}
Aggregations