use of javax.jms.MessageListener in project activemq-artemis by apache.
the class JMSConsumerTest method testPassMessageListenerIntoCreateConsumer.
public void testPassMessageListenerIntoCreateConsumer() throws Exception {
final AtomicInteger counter = new AtomicInteger(0);
final CountDownLatch done = new CountDownLatch(1);
// Receive a message with the JMS API
connection.start();
ActiveMQSession session = (ActiveMQSession) connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
destination = createDestination(session, destinationType);
MessageConsumer consumer = session.createConsumer(destination, new MessageListener() {
@Override
public void onMessage(Message m) {
counter.incrementAndGet();
if (counter.get() == 4) {
done.countDown();
}
}
});
assertNotNull(consumer);
// 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 JmsMessageConsumerTest method testAsyncReceiveWithExpirationChecks.
@Test
public void testAsyncReceiveWithExpirationChecks() throws Exception {
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(brokerURI);
final CountDownLatch received = new CountDownLatch(1);
Connection connection = factory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue(name.getMethodName());
MessageConsumer consumer = session.createConsumer(destination);
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
received.countDown();
}
});
MessageProducer producer = session.createProducer(destination);
producer.setTimeToLive(TimeUnit.SECONDS.toMillis(2));
producer.send(session.createTextMessage("test"));
// Allow message to expire in the prefetch buffer
TimeUnit.SECONDS.sleep(4);
connection.start();
assertFalse(received.await(1, TimeUnit.SECONDS));
connection.close();
}
use of javax.jms.MessageListener in project activemq-artemis by apache.
the class JmsMessageConsumerTest method testAsyncReceiveWithoutExpirationChecks.
@Test
public void testAsyncReceiveWithoutExpirationChecks() throws Exception {
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(brokerURI);
factory.setConsumerExpiryCheckEnabled(false);
final CountDownLatch received = new CountDownLatch(1);
Connection connection = factory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue(name.getMethodName());
MessageConsumer consumer = session.createConsumer(destination);
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
received.countDown();
}
});
MessageProducer producer = session.createProducer(destination);
producer.setTimeToLive(TimeUnit.SECONDS.toMillis(2));
producer.send(session.createTextMessage("test"));
// Allow message to expire in the prefetch buffer
TimeUnit.SECONDS.sleep(4);
connection.start();
assertTrue(received.await(5, TimeUnit.SECONDS));
connection.close();
}
use of javax.jms.MessageListener in project activemq-artemis by apache.
the class JmsConsumerResetActiveListenerTest method testNewConsumerSetListenerFromListener.
/**
* and a listener on a new consumer, just in case.
*
* @throws Exception
*/
public void testNewConsumerSetListenerFromListener() throws Exception {
final Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
final 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 {
MessageConsumer anotherConsumer = session.createConsumer(dest);
anotherConsumer.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 JMSBridgeImplTest method testSendMessagesWithMaxBatchSize.
@Test
public void testSendMessagesWithMaxBatchSize() throws Exception {
final int numMessages = 10;
ConnectionFactoryFactory sourceCFF = JMSBridgeImplTest.newConnectionFactoryFactory(JMSBridgeImplTest.createConnectionFactory());
ConnectionFactoryFactory targetCFF = JMSBridgeImplTest.newConnectionFactoryFactory(JMSBridgeImplTest.createConnectionFactory());
DestinationFactory sourceDF = JMSBridgeImplTest.newDestinationFactory(ActiveMQJMSClient.createQueue(JMSBridgeImplTest.SOURCE));
DestinationFactory targetDF = JMSBridgeImplTest.newDestinationFactory(ActiveMQJMSClient.createQueue(JMSBridgeImplTest.TARGET));
TransactionManager tm = JMSBridgeImplTest.newTransactionManager();
JMSBridgeImpl bridge = new JMSBridgeImpl();
Assert.assertNotNull(bridge);
bridge.setSourceConnectionFactoryFactory(sourceCFF);
bridge.setSourceDestinationFactory(sourceDF);
bridge.setTargetConnectionFactoryFactory(targetCFF);
bridge.setTargetDestinationFactory(targetDF);
bridge.setFailureRetryInterval(10);
bridge.setMaxRetries(-1);
bridge.setMaxBatchSize(numMessages);
bridge.setMaxBatchTime(-1);
bridge.setTransactionManager(tm);
bridge.setQualityOfServiceMode(QualityOfServiceMode.AT_MOST_ONCE);
Assert.assertFalse(bridge.isStarted());
bridge.start();
Assert.assertTrue(bridge.isStarted());
Connection targetConn = JMSBridgeImplTest.createConnectionFactory().createConnection();
Session targetSess = targetConn.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer consumer = targetSess.createConsumer(targetDF.createDestination());
final List<Message> messages = new LinkedList<>();
final CountDownLatch latch = new CountDownLatch(numMessages);
MessageListener listener = new MessageListener() {
@Override
public void onMessage(final Message message) {
messages.add(message);
latch.countDown();
}
};
consumer.setMessageListener(listener);
targetConn.start();
Connection sourceConn = JMSBridgeImplTest.createConnectionFactory().createConnection();
Session sourceSess = sourceConn.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = sourceSess.createProducer(sourceDF.createDestination());
for (int i = 0; i < numMessages - 1; i++) {
TextMessage msg = sourceSess.createTextMessage();
producer.send(msg);
JMSBridgeImplTest.log.info("sent message " + i);
}
Thread.sleep(1000);
Assert.assertEquals(0, messages.size());
TextMessage msg = sourceSess.createTextMessage();
producer.send(msg);
Assert.assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
sourceConn.close();
Assert.assertEquals(numMessages, messages.size());
bridge.stop();
Assert.assertFalse(bridge.isStarted());
targetConn.close();
}
Aggregations