Search in sources :

Example 91 with MessageListener

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

the class NetworkBrokerDetachTest method testNetworkedBrokerDurableSubAfterRestart.

@Test
public void testNetworkedBrokerDurableSubAfterRestart() throws Exception {
    final AtomicInteger count = new AtomicInteger(0);
    MessageListener counter = new MessageListener() {

        @Override
        public void onMessage(Message message) {
            count.incrementAndGet();
        }
    };
    LOG.info("Creating durable consumer on each broker ...");
    ActiveMQTopic destination = registerDurableConsumer(networkedBroker, counter);
    registerDurableConsumer(broker, counter);
    assertTrue("got expected consumer count from local broker mbean within time limit", verifyConsumerCount(2, destination, broker));
    assertTrue("got expected consumer count from network broker mbean within time limit", verifyConsumerCount(2, destination, networkedBroker));
    sendMessageTo(destination, broker);
    assertTrue("Got one message on each", verifyMessageCount(2, count));
    LOG.info("Stopping brokerTwo...");
    networkedBroker.stop();
    networkedBroker.waitUntilStopped();
    LOG.info("restarting  broker Two...");
    networkedBroker = createNetworkedBroker();
    networkedBroker.start();
    LOG.info("Recreating durable Consumer on the broker after restart...");
    registerDurableConsumer(networkedBroker, counter);
    // give advisories a chance to percolate
    TimeUnit.SECONDS.sleep(5);
    sendMessageTo(destination, broker);
    // expect similar after restart
    assertTrue("got expected consumer count from local broker mbean within time limit", verifyConsumerCount(2, destination, broker));
    // a durable sub is auto bridged on restart unless dynamicOnly=true
    assertTrue("got expected consumer count from network broker mbean within time limit", verifyConsumerCount(2, destination, networkedBroker));
    assertTrue("got no inactive subs on broker", verifyDurableConsumerCount(0, broker));
    assertTrue("got no inactive subs on other broker", verifyDurableConsumerCount(0, networkedBroker));
    assertTrue("Got two more messages after restart", verifyMessageCount(4, count));
    TimeUnit.SECONDS.sleep(1);
    assertTrue("still Got just two more messages", verifyMessageCount(4, count));
}
Also used : ActiveMQTopic(org.apache.activemq.command.ActiveMQTopic) Message(javax.jms.Message) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MessageListener(javax.jms.MessageListener) Test(org.junit.Test)

Example 92 with MessageListener

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

the class NetworkFailoverTest method testRequestReply.

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) {
            final TextMessage textMsg = (TextMessage) msg;
            try {
                String payload = "REPLY: " + textMsg.getText() + ", " + textMsg.getJMSMessageID();
                Destination replyTo;
                replyTo = msg.getJMSReplyTo();
                textMsg.clearBody();
                textMsg.setText(payload);
                LOG.info("*** Sending response: {}", textMsg.getText());
                remoteProducer.send(replyTo, textMsg);
                LOG.info("replied with: " + textMsg.getJMSMessageID());
            } catch (DestinationDoesNotExistException expected) {
                // been removed but not yet recreated
                replyToNonExistDest.incrementAndGet();
                try {
                    LOG.info("NED: " + textMsg.getJMSMessageID());
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            } catch (Exception e) {
                LOG.warn("*** Responder listener caught exception: ", e);
                e.printStackTrace();
            }
        }
    });
    Queue tempQueue = localSession.createTemporaryQueue();
    MessageProducer requestProducer = localSession.createProducer(included);
    requestProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
    MessageConsumer requestConsumer = localSession.createConsumer(tempQueue);
    // track remote dlq for forward failures
    MessageConsumer dlqconsumer = remoteSession.createConsumer(new ActiveMQQueue(SharedDeadLetterStrategy.DEFAULT_DEAD_LETTER_QUEUE_NAME));
    dlqconsumer.setMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message message) {
            try {
                LOG.info("dlq " + message.getJMSMessageID());
            } catch (JMSException e) {
                e.printStackTrace();
            }
            remoteDLQCount.incrementAndGet();
        }
    });
    // allow for consumer infos to perculate around
    Thread.sleep(2000);
    long done = System.currentTimeMillis() + (MESSAGE_COUNT * 6000);
    int i = 0;
    while (MESSAGE_COUNT > roundTripComplete.get() + remoteDLQCount.get() + replyToNonExistDest.get() && done > System.currentTimeMillis()) {
        if (i < MESSAGE_COUNT) {
            String payload = "test msg " + i;
            i++;
            TextMessage msg = localSession.createTextMessage(payload);
            msg.setJMSReplyTo(tempQueue);
            requestProducer.send(msg);
            LOG.info("Sent: " + msg.getJMSMessageID() + ", Failing over");
            ((FailoverTransport) ((TransportFilter) ((TransportFilter) ((ActiveMQConnection) localConnection).getTransport()).getNext()).getNext()).handleTransportFailure(new IOException("Forcing failover from test"));
        }
        TextMessage result = (TextMessage) requestConsumer.receive(5000);
        if (result != null) {
            LOG.info("Got reply: " + result.getJMSMessageID() + ", " + result.getText());
            roundTripComplete.incrementAndGet();
        }
    }
    LOG.info("complete: " + roundTripComplete.get() + ", remoteDLQCount: " + remoteDLQCount.get() + ", replyToNonExistDest: " + replyToNonExistDest.get());
    assertEquals("complete:" + roundTripComplete.get() + ", remoteDLQCount: " + remoteDLQCount.get() + ", replyToNonExistDest: " + replyToNonExistDest.get(), MESSAGE_COUNT, roundTripComplete.get() + remoteDLQCount.get() + replyToNonExistDest.get());
}
Also used : DestinationDoesNotExistException(org.apache.activemq.DestinationDoesNotExistException) Destination(javax.jms.Destination) MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) MessageListener(javax.jms.MessageListener) JMSException(javax.jms.JMSException) FailoverTransport(org.apache.activemq.transport.failover.FailoverTransport) IOException(java.io.IOException) TransportFilter(org.apache.activemq.transport.TransportFilter) DestinationDoesNotExistException(org.apache.activemq.DestinationDoesNotExistException) IOException(java.io.IOException) JMSException(javax.jms.JMSException) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) MessageProducer(javax.jms.MessageProducer) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) Queue(javax.jms.Queue) TextMessage(javax.jms.TextMessage)

Example 93 with MessageListener

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

the class ZeroPrefetchConsumerTest method testCannotUseMessageListener.

public void testCannotUseMessageListener() throws Exception {
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer consumer = session.createConsumer(queue);
    MessageListener listener = new SpringConsumer();
    try {
        consumer.setMessageListener(listener);
        fail("Should have thrown JMSException as we cannot use MessageListener with zero prefetch");
    } catch (JMSException e) {
        LOG.info("Received expected exception : " + e);
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) MessageListener(javax.jms.MessageListener) JMSException(javax.jms.JMSException) SpringConsumer(org.apache.activemq.spring.SpringConsumer) Session(javax.jms.Session)

Example 94 with MessageListener

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

the class RedeliveryPolicyTest method testRepeatedRedeliveryOnMessageNoCommit.

public void testRepeatedRedeliveryOnMessageNoCommit() throws Exception {
    connection.start();
    Session dlqSession = connection.createSession(true, Session.SESSION_TRANSACTED);
    ActiveMQQueue destination = new ActiveMQQueue("TEST");
    MessageProducer producer = dlqSession.createProducer(destination);
    // Send the messages
    producer.send(dlqSession.createTextMessage("1st"));
    dlqSession.commit();
    MessageConsumer dlqConsumer = dlqSession.createConsumer(new ActiveMQQueue("ActiveMQ.DLQ"));
    final int maxRedeliveries = 4;
    final AtomicInteger receivedCount = new AtomicInteger(0);
    for (int i = 0; i <= maxRedeliveries + 1; i++) {
        connection = (ActiveMQConnection) factory.createConnection(userName, password);
        connections.add(connection);
        RedeliveryPolicy policy = connection.getRedeliveryPolicy();
        policy.setInitialRedeliveryDelay(0);
        policy.setUseExponentialBackOff(false);
        policy.setMaximumRedeliveries(maxRedeliveries);
        connection.start();
        final Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
        MessageConsumer consumer = session.createConsumer(destination);
        final CountDownLatch done = new CountDownLatch(1);
        consumer.setMessageListener(new MessageListener() {

            @Override
            public void onMessage(Message message) {
                try {
                    ActiveMQTextMessage m = (ActiveMQTextMessage) message;
                    assertEquals("1st", m.getText());
                    assertEquals(receivedCount.get(), m.getRedeliveryCounter());
                    receivedCount.incrementAndGet();
                    done.countDown();
                } catch (Exception ignored) {
                    ignored.printStackTrace();
                }
            }
        });
        if (i <= maxRedeliveries) {
            assertTrue("listener done", done.await(5, TimeUnit.SECONDS));
        } else {
            // final redlivery gets poisoned before dispatch
            assertFalse("listener done", done.await(1, TimeUnit.SECONDS));
        }
        connection.close();
        connections.remove(connection);
    }
    // We should be able to get the message off the DLQ now.
    TextMessage m = (TextMessage) dlqConsumer.receive(1000);
    assertNotNull("Got message from DLQ", m);
    assertEquals("1st", m.getText());
    String cause = m.getStringProperty(ActiveMQMessage.DLQ_DELIVERY_FAILURE_CAUSE_PROPERTY);
    assertTrue("cause exception has policy ref", cause.contains("RedeliveryPolicy"));
    dlqSession.commit();
}
Also used : MessageConsumer(javax.jms.MessageConsumer) TextMessage(javax.jms.TextMessage) ActiveMQMessage(org.apache.activemq.command.ActiveMQMessage) ActiveMQTextMessage(org.apache.activemq.command.ActiveMQTextMessage) Message(javax.jms.Message) MessageListener(javax.jms.MessageListener) CountDownLatch(java.util.concurrent.CountDownLatch) JMSException(javax.jms.JMSException) ActiveMQTextMessage(org.apache.activemq.command.ActiveMQTextMessage) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) ActiveMQTextMessage(org.apache.activemq.command.ActiveMQTextMessage) Session(javax.jms.Session) ServerSession(javax.jms.ServerSession)

Example 95 with MessageListener

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

the class RedeliveryPolicyTest method testRepeatedRedeliveryServerSessionNoCommit.

public void testRepeatedRedeliveryServerSessionNoCommit() throws Exception {
    connection.start();
    Session dlqSession = connection.createSession(true, Session.SESSION_TRANSACTED);
    ActiveMQQueue destination = new ActiveMQQueue("TEST");
    MessageProducer producer = dlqSession.createProducer(destination);
    // Send the messages
    producer.send(dlqSession.createTextMessage("1st"));
    dlqSession.commit();
    MessageConsumer dlqConsumer = dlqSession.createConsumer(new ActiveMQQueue("ActiveMQ.DLQ"));
    final int maxRedeliveries = 4;
    final AtomicInteger receivedCount = new AtomicInteger(0);
    for (int i = 0; i <= maxRedeliveries + 1; i++) {
        connection = (ActiveMQConnection) factory.createConnection(userName, password);
        connections.add(connection);
        RedeliveryPolicy policy = connection.getRedeliveryPolicy();
        policy.setInitialRedeliveryDelay(0);
        policy.setUseExponentialBackOff(false);
        policy.setMaximumRedeliveries(maxRedeliveries);
        connection.start();
        final CountDownLatch done = new CountDownLatch(1);
        final ActiveMQSession session = (ActiveMQSession) connection.createSession(true, Session.SESSION_TRANSACTED);
        session.setMessageListener(new MessageListener() {

            @Override
            public void onMessage(Message message) {
                try {
                    ActiveMQTextMessage m = (ActiveMQTextMessage) message;
                    assertEquals("1st", m.getText());
                    assertEquals(receivedCount.get(), m.getRedeliveryCounter());
                    receivedCount.incrementAndGet();
                    done.countDown();
                } catch (Exception ignored) {
                    ignored.printStackTrace();
                }
            }
        });
        connection.createConnectionConsumer(destination, null, new ServerSessionPool() {

            @Override
            public ServerSession getServerSession() throws JMSException {
                return new ServerSession() {

                    @Override
                    public Session getSession() throws JMSException {
                        return session;
                    }

                    @Override
                    public void start() throws JMSException {
                    }
                };
            }
        }, 100, false);
        Wait.waitFor(new Wait.Condition() {

            @Override
            public boolean isSatisified() throws Exception {
                session.run();
                return done.await(10, TimeUnit.MILLISECONDS);
            }
        });
        if (i <= maxRedeliveries) {
            assertTrue("listener done @" + i, done.await(5, TimeUnit.SECONDS));
        } else {
            // final redlivery gets poisoned before dispatch
            assertFalse("listener not done @" + i, done.await(1, TimeUnit.SECONDS));
        }
        connection.close();
        connections.remove(connection);
    }
    // We should be able to get the message off the DLQ now.
    TextMessage m = (TextMessage) dlqConsumer.receive(1000);
    assertNotNull("Got message from DLQ", m);
    assertEquals("1st", m.getText());
    String cause = m.getStringProperty(ActiveMQMessage.DLQ_DELIVERY_FAILURE_CAUSE_PROPERTY);
    assertTrue("cause exception has policy ref", cause.contains("RedeliveryPolicy"));
    dlqSession.commit();
}
Also used : MessageConsumer(javax.jms.MessageConsumer) ServerSessionPool(javax.jms.ServerSessionPool) ServerSession(javax.jms.ServerSession) TextMessage(javax.jms.TextMessage) ActiveMQMessage(org.apache.activemq.command.ActiveMQMessage) ActiveMQTextMessage(org.apache.activemq.command.ActiveMQTextMessage) Message(javax.jms.Message) MessageListener(javax.jms.MessageListener) JMSException(javax.jms.JMSException) CountDownLatch(java.util.concurrent.CountDownLatch) JMSException(javax.jms.JMSException) ActiveMQTextMessage(org.apache.activemq.command.ActiveMQTextMessage) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) MessageProducer(javax.jms.MessageProducer) Wait(org.apache.activemq.util.Wait) TextMessage(javax.jms.TextMessage) ActiveMQTextMessage(org.apache.activemq.command.ActiveMQTextMessage) Session(javax.jms.Session) ServerSession(javax.jms.ServerSession)

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