Search in sources :

Example 36 with MessageListener

use of javax.jms.MessageListener in project activemq-artemis by apache.

the class JMSBridgeImplTest method testSendMessagesWhenMaxBatchTimeExpires.

@Test
public void testSendMessagesWhenMaxBatchTimeExpires() throws Exception {
    int maxBatchSize = 2;
    long maxBatchTime = 500;
    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(maxBatchSize);
    bridge.setMaxBatchTime(maxBatchTime);
    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<>();
    MessageListener listener = new MessageListener() {

        @Override
        public void onMessage(final Message message) {
            messages.add(message);
        }
    };
    consumer.setMessageListener(listener);
    targetConn.start();
    Connection sourceConn = JMSBridgeImplTest.createConnectionFactory().createConnection();
    Session sourceSess = sourceConn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageProducer producer = sourceSess.createProducer(sourceDF.createDestination());
    producer.send(sourceSess.createTextMessage());
    sourceConn.close();
    Assert.assertEquals(0, messages.size());
    Thread.sleep(3 * maxBatchTime);
    Assert.assertEquals(1, 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) LinkedList(java.util.LinkedList) TransactionManager(javax.transaction.TransactionManager) ConnectionFactoryFactory(org.apache.activemq.artemis.jms.bridge.ConnectionFactoryFactory) MessageProducer(javax.jms.MessageProducer) Session(javax.jms.Session) Test(org.junit.Test)

Example 37 with MessageListener

use of javax.jms.MessageListener in project activemq-artemis by apache.

the class SimpleNetworkTest method testRequestReply.

@Test(timeout = 60 * 1000)
public void testRequestReply() throws Exception {
    final MessageProducer remoteProducer = remoteSession.createProducer(null);
    MessageConsumer remoteConsumer = remoteSession.createConsumer(included);
    remoteConsumer.setMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message msg) {
            try {
                TextMessage textMsg = (TextMessage) msg;
                String payload = "REPLY: " + textMsg.getText();
                Destination replyTo;
                replyTo = msg.getJMSReplyTo();
                textMsg.clearBody();
                textMsg.setText(payload);
                remoteProducer.send(replyTo, textMsg);
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
    });
    TopicRequestor requestor = new TopicRequestor((TopicSession) localSession, included);
    // allow for consumer infos to perculate around
    Thread.sleep(5000);
    for (int i = 0; i < MESSAGE_COUNT; i++) {
        TextMessage msg = localSession.createTextMessage("test msg: " + i);
        TextMessage result = (TextMessage) requestor.request(msg);
        assertNotNull(result);
        LOG.info(result.getText());
    }
}
Also used : ActiveMQDestination(org.apache.activemq.command.ActiveMQDestination) Destination(javax.jms.Destination) MessageConsumer(javax.jms.MessageConsumer) TopicRequestor(javax.jms.TopicRequestor) ActiveMQMessage(org.apache.activemq.command.ActiveMQMessage) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) MessageListener(javax.jms.MessageListener) JMSException(javax.jms.JMSException) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Test(org.junit.Test)

Example 38 with MessageListener

use of javax.jms.MessageListener in project activemq-artemis by apache.

the class MessageListenerRedeliveryTest method testQueueSessionListenerExceptionRetry.

@Test
public void testQueueSessionListenerExceptionRetry() throws Exception {
    redeliverConnection.start();
    Session session = redeliverConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    String qname = "queue-testQueueSessionListenerExceptionRetry";
    Queue queue = session.createQueue(qname);
    this.makeSureCoreQueueExist(qname);
    MessageProducer producer = createProducer(session, queue);
    Message message = createTextMessage(session, "1");
    producer.send(message);
    message = createTextMessage(session, "2");
    producer.send(message);
    MessageConsumer consumer = session.createConsumer(queue);
    final CountDownLatch gotMessage = new CountDownLatch(2);
    final AtomicInteger count = new AtomicInteger(0);
    final int maxDeliveries = getRedeliveryPolicy().getMaximumRedeliveries();
    final ArrayList<String> received = new ArrayList<>();
    consumer.setMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message message) {
            System.out.println("Message Received: " + message);
            try {
                received.add(((TextMessage) message).getText());
            } catch (JMSException e) {
                e.printStackTrace();
                fail(e.toString());
            }
            if (count.incrementAndGet() < maxDeliveries) {
                throw new RuntimeException(getName() + " force a redelivery");
            }
            // new blood
            count.set(0);
            gotMessage.countDown();
        }
    });
    assertTrue("got message before retry expiry", gotMessage.await(20, TimeUnit.SECONDS));
    for (int i = 0; i < maxDeliveries; i++) {
        assertEquals("got first redelivered: " + i, "1", received.get(i));
    }
    for (int i = maxDeliveries; i < maxDeliveries * 2; i++) {
        assertEquals("got first redelivered: " + i, "2", received.get(i));
    }
    session.close();
}
Also used : ActiveMQMessageConsumer(org.apache.activemq.ActiveMQMessageConsumer) MessageConsumer(javax.jms.MessageConsumer) ActiveMQMessage(org.apache.activemq.command.ActiveMQMessage) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) ArrayList(java.util.ArrayList) MessageListener(javax.jms.MessageListener) JMSException(javax.jms.JMSException) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MessageProducer(javax.jms.MessageProducer) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) Queue(javax.jms.Queue) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) BasicOpenWireTest(org.apache.activemq.artemis.tests.integration.openwire.BasicOpenWireTest) Test(org.junit.Test)

Example 39 with MessageListener

use of javax.jms.MessageListener in project activemq-artemis by apache.

the class MessageListenerRedeliveryTest method testQueueSessionListenerExceptionDlq.

@Test
public void testQueueSessionListenerExceptionDlq() throws Exception {
    redeliverConnection.start();
    Session session = redeliverConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    String qname = "queue-testQueueSessionListenerExceptionDlq";
    Queue queue = session.createQueue(qname);
    this.makeSureCoreQueueExist(qname);
    MessageProducer producer = createProducer(session, queue);
    Message message = createTextMessage(session);
    producer.send(message);
    final Message[] dlqMessage = new Message[1];
    ActiveMQDestination dlqDestination = new ActiveMQQueue("ActiveMQ.DLQ");
    this.makeSureCoreQueueExist("ActiveMQ.DLQ");
    MessageConsumer dlqConsumer = session.createConsumer(dlqDestination);
    final CountDownLatch gotDlqMessage = new CountDownLatch(1);
    dlqConsumer.setMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message message) {
            System.out.println("DLQ Message Received: " + message);
            dlqMessage[0] = message;
            gotDlqMessage.countDown();
        }
    });
    MessageConsumer consumer = session.createConsumer(queue);
    final int maxDeliveries = getRedeliveryPolicy().getMaximumRedeliveries();
    System.out.println("max redlivery: " + maxDeliveries);
    final CountDownLatch gotMessage = new CountDownLatch(maxDeliveries);
    consumer.setMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message message) {
            System.out.println("Message Received: " + message);
            gotMessage.countDown();
            throw new RuntimeException(getName() + " force a redelivery");
        }
    });
    assertTrue("got message before retry expiry", gotMessage.await(20, TimeUnit.SECONDS));
    // check DLQ
    assertTrue("got dlq message", gotDlqMessage.await(20, TimeUnit.SECONDS));
    // check DLQ message cause is captured
    message = dlqMessage[0];
    assertNotNull("dlq message captured", message);
    String cause = message.getStringProperty(ActiveMQMessage.DLQ_DELIVERY_FAILURE_CAUSE_PROPERTY);
    System.out.println("DLQ'd message cause reported as: " + cause);
    assertTrue("cause 'cause' exception is remembered", cause.contains("RuntimeException"));
    assertTrue("is correct exception", cause.contains(getName()));
    assertTrue("cause exception is remembered", cause.contains("Throwable"));
    assertTrue("cause policy is remembered", cause.contains("RedeliveryPolicy"));
    session.close();
}
Also used : ActiveMQMessageConsumer(org.apache.activemq.ActiveMQMessageConsumer) MessageConsumer(javax.jms.MessageConsumer) ActiveMQMessage(org.apache.activemq.command.ActiveMQMessage) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) MessageListener(javax.jms.MessageListener) CountDownLatch(java.util.concurrent.CountDownLatch) ActiveMQDestination(org.apache.activemq.command.ActiveMQDestination) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) MessageProducer(javax.jms.MessageProducer) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) Queue(javax.jms.Queue) Session(javax.jms.Session) BasicOpenWireTest(org.apache.activemq.artemis.tests.integration.openwire.BasicOpenWireTest) Test(org.junit.Test)

Example 40 with MessageListener

use of javax.jms.MessageListener in project activemq-artemis by apache.

the class JMSConsumer1Test method testMessageListenerWithConsumerCanBeStopped.

@Test
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());
}
Also used : Message(javax.jms.Message) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ActiveMQMessageConsumer(org.apache.activemq.ActiveMQMessageConsumer) MessageListener(javax.jms.MessageListener) CountDownLatch(java.util.concurrent.CountDownLatch) Session(javax.jms.Session) BasicOpenWireTest(org.apache.activemq.artemis.tests.integration.openwire.BasicOpenWireTest) 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