use of javax.jms.MessageListener in project activemq-artemis by apache.
the class JmsConsumerResetActiveListenerTest method testSetListenerFromListener.
/**
* verify the (undefined by spec) behaviour of setting a listener while receiving a message.
*
* @throws Exception
*/
public void testSetListenerFromListener() throws Exception {
Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
Destination dest = session.createQueue("Queue-" + getName());
final MessageConsumer consumer = session.createConsumer(dest);
final CountDownLatch latch = new CountDownLatch(2);
final AtomicBoolean first = new AtomicBoolean(true);
final Vector<Object> results = new Vector<>();
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
if (first.compareAndSet(true, false)) {
try {
consumer.setMessageListener(this);
results.add(message);
} catch (JMSException e) {
results.add(e);
}
} else {
results.add(message);
}
latch.countDown();
}
});
connection.start();
MessageProducer producer = session.createProducer(dest);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
producer.send(session.createTextMessage("First"));
producer.send(session.createTextMessage("Second"));
assertTrue("we did not timeout", latch.await(5, TimeUnit.SECONDS));
assertEquals("we have a result", 2, results.size());
Object result = results.get(0);
assertTrue(result instanceof TextMessage);
assertEquals("result is first", "First", ((TextMessage) result).getText());
result = results.get(1);
assertTrue(result instanceof TextMessage);
assertEquals("result is first", "Second", ((TextMessage) result).getText());
}
use of javax.jms.MessageListener in project activemq-artemis by apache.
the class JMSConsumerTest method testMessageListenerWithConsumerWithPrefetch1.
public void testMessageListenerWithConsumerWithPrefetch1() throws Exception {
final AtomicInteger counter = new AtomicInteger(0);
final CountDownLatch done = new CountDownLatch(1);
// Receive a message with the JMS API
connection.getPrefetchPolicy().setAll(1);
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());
}
use of javax.jms.MessageListener in project activemq-artemis by apache.
the class JMSConsumerTest method testMessageListenerAutoAckOnCloseWithPrefetch1.
public void testMessageListenerAutoAckOnCloseWithPrefetch1() 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();
m.acknowledge();
if (counter.get() == 2) {
sendDone.await();
connection.close();
got2Done.countDown();
}
} 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());
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);
// close from onMessage with Auto_ack will ack
// Make sure only 4 messages were delivered.
assertEquals(4, counter.get());
}
use of javax.jms.MessageListener in project activemq-artemis by apache.
the class JMSDurableConsumerTest method testDurableConsumerAsync.
@Test(timeout = 30000)
public void testDurableConsumerAsync() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<Message> received = new AtomicReference<>();
String durableClientId = getTopicName() + "-ClientId";
Connection connection = createConnection(durableClientId);
try {
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic = session.createTopic(getTopicName());
MessageConsumer consumer = session.createDurableSubscriber(topic, "DurbaleTopic");
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
received.set(message);
latch.countDown();
}
});
MessageProducer producer = session.createProducer(topic);
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
connection.start();
TextMessage message = session.createTextMessage();
message.setText("hello");
producer.send(message);
assertTrue(latch.await(10, TimeUnit.SECONDS));
assertNotNull("Should have received a message by now.", received.get());
assertTrue("Should be an instance of TextMessage", received.get() instanceof TextMessage);
} finally {
connection.close();
}
}
use of javax.jms.MessageListener in project activemq-artemis by apache.
the class MQTTNetworkOfBrokersFailoverTest method listenForConsumersOn.
private CountDownLatch listenForConsumersOn(BrokerService broker) throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
URI brokerUri = broker.getVmConnectorURI();
final ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory(brokerUri.toASCIIString());
final Connection connection = cf.createConnection();
connection.start();
final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination dest = session.createTopic("ActiveMQ.Advisory.Consumer.Queue.Consumer.foo:AT_LEAST_ONCE.VirtualTopic.foo.bar");
MessageConsumer consumer = session.createConsumer(dest);
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
latch.countDown();
// shutdown this connection
Dispatch.getGlobalQueue().execute(new Runnable() {
@Override
public void run() {
try {
session.close();
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
});
}
});
return latch;
}
Aggregations