Search in sources :

Example 31 with MessageListener

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());
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) BytesMessage(javax.jms.BytesMessage) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MessageListener(javax.jms.MessageListener) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 32 with MessageListener

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();
}
Also used : Destination(javax.jms.Destination) MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) Connection(javax.jms.Connection) MessageListener(javax.jms.MessageListener) MessageProducer(javax.jms.MessageProducer) CountDownLatch(java.util.concurrent.CountDownLatch) Session(javax.jms.Session) Test(org.junit.Test)

Example 33 with MessageListener

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();
}
Also used : Destination(javax.jms.Destination) MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) Connection(javax.jms.Connection) MessageListener(javax.jms.MessageListener) MessageProducer(javax.jms.MessageProducer) CountDownLatch(java.util.concurrent.CountDownLatch) Session(javax.jms.Session) Test(org.junit.Test)

Example 34 with MessageListener

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());
}
Also used : Destination(javax.jms.Destination) MessageConsumer(javax.jms.MessageConsumer) TextMessage(javax.jms.TextMessage) Message(javax.jms.Message) MessageListener(javax.jms.MessageListener) JMSException(javax.jms.JMSException) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MessageProducer(javax.jms.MessageProducer) Vector(java.util.Vector) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Example 35 with MessageListener

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();
}
Also used : DestinationFactory(org.apache.activemq.artemis.jms.bridge.DestinationFactory) MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) JMSBridgeImpl(org.apache.activemq.artemis.jms.bridge.impl.JMSBridgeImpl) Connection(javax.jms.Connection) MessageListener(javax.jms.MessageListener) CountDownLatch(java.util.concurrent.CountDownLatch) LinkedList(java.util.LinkedList) TransactionManager(javax.transaction.TransactionManager) ConnectionFactoryFactory(org.apache.activemq.artemis.jms.bridge.ConnectionFactoryFactory) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) Test(org.junit.Test)

Aggregations

MessageListener (javax.jms.MessageListener)146 Message (javax.jms.Message)128 MessageConsumer (javax.jms.MessageConsumer)103 Session (javax.jms.Session)97 TextMessage (javax.jms.TextMessage)95 CountDownLatch (java.util.concurrent.CountDownLatch)72 Test (org.junit.Test)71 Connection (javax.jms.Connection)60 MessageProducer (javax.jms.MessageProducer)58 JMSException (javax.jms.JMSException)57 Destination (javax.jms.Destination)32 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)31 ActiveMQConnectionFactory (org.apache.activemq.ActiveMQConnectionFactory)26 BytesMessage (javax.jms.BytesMessage)22 Queue (javax.jms.Queue)21 ConnectionFactory (javax.jms.ConnectionFactory)16 ScheduledMessage (org.apache.activemq.ScheduledMessage)15 ArrayList (java.util.ArrayList)14 Topic (javax.jms.Topic)13 ActiveMQQueue (org.apache.activemq.command.ActiveMQQueue)13