Search in sources :

Example 56 with MessageListener

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

the class FailoverConsumerOutstandingCommitTest method doTestFailoverConsumerOutstandingSendTx.

public void doTestFailoverConsumerOutstandingSendTx(final boolean doActualBrokerCommit) throws Exception {
    final boolean watchTopicAdvisories = true;
    server = createBroker();
    server.start();
    brokerStopLatch = new CountDownLatch(1);
    ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("failover:(" + url + ")");
    cf.setWatchTopicAdvisories(watchTopicAdvisories);
    cf.setDispatchAsync(false);
    final ActiveMQConnection connection = (ActiveMQConnection) cf.createConnection();
    connection.start();
    final Session producerSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    final Queue destination = producerSession.createQueue(QUEUE_NAME + "?consumer.prefetchSize=" + prefetch);
    final Queue signalDestination = producerSession.createQueue(QUEUE_NAME + ".signal" + "?consumer.prefetchSize=" + prefetch);
    final Session consumerSession = connection.createSession(true, Session.SESSION_TRANSACTED);
    final CountDownLatch commitDoneLatch = new CountDownLatch(1);
    final CountDownLatch messagesReceived = new CountDownLatch(3);
    final AtomicBoolean gotCommitException = new AtomicBoolean(false);
    final ArrayList<TextMessage> receivedMessages = new ArrayList<>();
    final MessageConsumer testConsumer = consumerSession.createConsumer(destination);
    doByteman.set(true);
    testConsumer.setMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message message) {
            LOG.info("consume one: " + message);
            assertNotNull("got message", message);
            receivedMessages.add((TextMessage) message);
            try {
                LOG.info("send one");
                produceMessage(consumerSession, signalDestination, 1);
                LOG.info("commit session");
                consumerSession.commit();
            } catch (JMSException e) {
                LOG.info("commit exception", e);
                gotCommitException.set(true);
            }
            commitDoneLatch.countDown();
            messagesReceived.countDown();
            LOG.info("done commit");
        }
    });
    // may block if broker shutdown happens quickly
    new Thread() {

        @Override
        public void run() {
            LOG.info("producer started");
            try {
                produceMessage(producerSession, destination, prefetch * 2);
            } catch (javax.jms.IllegalStateException SessionClosedExpectedOnShutdown) {
            } catch (JMSException e) {
                e.printStackTrace();
                fail("unexpceted ex on producer: " + e);
            }
            LOG.info("producer done");
        }
    }.start();
    // will be stopped by the plugin
    brokerStopLatch.await();
    doByteman.set(false);
    server.stop();
    server = createBroker();
    server.start();
    assertTrue("commit done through failover", commitDoneLatch.await(20, TimeUnit.SECONDS));
    assertTrue("commit failed", gotCommitException.get());
    assertTrue("another message was received after failover", messagesReceived.await(20, TimeUnit.SECONDS));
    int receivedIndex = 0;
    assertEquals("get message 0 first", MESSAGE_TEXT + "0", receivedMessages.get(receivedIndex++).getText());
    if (!doActualBrokerCommit) {
        // it will be redelivered and not tracked as a duplicate
        assertEquals("get message 0 second", MESSAGE_TEXT + "0", receivedMessages.get(receivedIndex++).getText());
    }
    assertTrue("another message was received", messagesReceived.await(20, TimeUnit.SECONDS));
    assertEquals("get message 1 eventually", MESSAGE_TEXT + "1", receivedMessages.get(receivedIndex++).getText());
    connection.close();
    server.stop();
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) ActiveMQConnection(org.apache.activemq.ActiveMQConnection) ArrayList(java.util.ArrayList) MessageListener(javax.jms.MessageListener) JMSException(javax.jms.JMSException) CountDownLatch(java.util.concurrent.CountDownLatch) ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Queue(javax.jms.Queue) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Example 57 with MessageListener

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

the class StompTest method testSendOverDiskFull.

@Test
public void testSendOverDiskFull() throws Exception {
    AssertionLoggerHandler.startCapture();
    try {
        MessageConsumer consumer = session.createConsumer(queue);
        conn.connect(defUser, defPass);
        int count = 1000;
        final CountDownLatch latch = new CountDownLatch(count);
        consumer.setMessageListener(new MessageListener() {

            @Override
            public void onMessage(Message arg0) {
                latch.countDown();
            }
        });
        ((ActiveMQServerImpl) server.getActiveMQServer()).getMonitor().setMaxUsage(0).tick();
        // Connection should be closed by broker when disk is full and attempt to send
        Exception e = null;
        try {
            for (int i = 1; i <= count; i++) {
                send(conn, getQueuePrefix() + getQueueName(), null, "Hello World!");
            }
        } catch (Exception se) {
            e = se;
        }
        assertNotNull(e);
        // It should encounter the exception on logs
        AssertionLoggerHandler.findText("AMQ119119");
    } finally {
        AssertionLoggerHandler.clear();
        AssertionLoggerHandler.stopCapture();
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) BytesMessage(javax.jms.BytesMessage) MessageListener(javax.jms.MessageListener) CountDownLatch(java.util.concurrent.CountDownLatch) ActiveMQServerImpl(org.apache.activemq.artemis.core.server.impl.ActiveMQServerImpl) Test(org.junit.Test)

Example 58 with MessageListener

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

the class DestinationGCTest method testDestinationGCWithActiveConsumers.

public void testDestinationGCWithActiveConsumers() throws Exception {
    assertEquals(1, broker.getAdminView().getQueues().length);
    ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("vm://localhost?create=false");
    Connection connection = factory.createConnection();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    session.createProducer(otherQueue).close();
    MessageConsumer consumer = session.createConsumer(queue);
    consumer.setMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message message) {
        }
    });
    connection.start();
    TimeUnit.SECONDS.sleep(5);
    assertTrue("After GC runs there should be one Queue.", Wait.waitFor(new Condition() {

        @Override
        public boolean isSatisified() throws Exception {
            return broker.getAdminView().getQueues().length == 1;
        }
    }));
    connection.close();
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) Condition(org.apache.activemq.util.Wait.Condition) MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) Connection(javax.jms.Connection) MessageListener(javax.jms.MessageListener) Session(javax.jms.Session)

Example 59 with MessageListener

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

the class JobSchedulerTxTest method testTxSendWithRollback.

@Test
public void testTxSendWithRollback() throws Exception {
    final int COUNT = 10;
    Connection connection = createConnection();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer consumer = session.createConsumer(destination);
    final CountDownLatch latch = new CountDownLatch(COUNT);
    consumer.setMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message message) {
            latch.countDown();
        }
    });
    connection.start();
    long time = 5000;
    Session producerSession = connection.createSession(true, Session.SESSION_TRANSACTED);
    MessageProducer producer = producerSession.createProducer(destination);
    for (int i = 0; i < COUNT; ++i) {
        TextMessage message = session.createTextMessage("test msg");
        message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, time);
        producer.send(message);
    }
    producer.close();
    producerSession.rollback();
    // make sure the message isn't delivered early
    Thread.sleep(2000);
    assertEquals(COUNT, latch.getCount());
    latch.await(5, TimeUnit.SECONDS);
    assertEquals(COUNT, latch.getCount());
}
Also used : MessageConsumer(javax.jms.MessageConsumer) ScheduledMessage(org.apache.activemq.ScheduledMessage) TextMessage(javax.jms.TextMessage) Message(javax.jms.Message) Connection(javax.jms.Connection) MessageListener(javax.jms.MessageListener) MessageProducer(javax.jms.MessageProducer) CountDownLatch(java.util.concurrent.CountDownLatch) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) Test(org.junit.Test)

Example 60 with MessageListener

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

the class JobSchedulerTxTest method testTxSendWithCommit.

@Test
public void testTxSendWithCommit() throws Exception {
    final int COUNT = 10;
    Connection connection = createConnection();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer consumer = session.createConsumer(destination);
    final CountDownLatch latch = new CountDownLatch(COUNT);
    consumer.setMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message message) {
            latch.countDown();
        }
    });
    connection.start();
    long time = 5000;
    Session producerSession = connection.createSession(true, Session.SESSION_TRANSACTED);
    MessageProducer producer = producerSession.createProducer(destination);
    for (int i = 0; i < COUNT; ++i) {
        TextMessage message = session.createTextMessage("test msg");
        message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, time);
        producer.send(message);
    }
    producer.close();
    producerSession.commit();
    // make sure the message isn't delivered early
    Thread.sleep(2000);
    assertEquals(COUNT, latch.getCount());
    latch.await(5, TimeUnit.SECONDS);
    assertEquals(0, latch.getCount());
}
Also used : MessageConsumer(javax.jms.MessageConsumer) ScheduledMessage(org.apache.activemq.ScheduledMessage) TextMessage(javax.jms.TextMessage) Message(javax.jms.Message) Connection(javax.jms.Connection) MessageListener(javax.jms.MessageListener) MessageProducer(javax.jms.MessageProducer) CountDownLatch(java.util.concurrent.CountDownLatch) 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