Search in sources :

Example 81 with MessageListener

use of javax.jms.MessageListener in project cxf by apache.

the class MessageListenerTest method testLocalTransaction.

@Test
public void testLocalTransaction() throws JMSException, XAException, InterruptedException {
    Connection connection = createConnection("brokerLocalTransaction");
    Queue dest = JMSUtil.createQueue(connection, "test");
    MessageListener listenerHandler = new TestMessageListener();
    AbstractMessageListenerContainer container = new MessageListenerContainer(connection, dest, listenerHandler);
    container.setTransacted(true);
    container.setAcknowledgeMode(Session.SESSION_TRANSACTED);
    container.start();
    testTransactionalBehaviour(connection, dest);
    container.stop();
    connection.close();
}
Also used : Connection(javax.jms.Connection) MessageListener(javax.jms.MessageListener) Queue(javax.jms.Queue) Test(org.junit.Test)

Example 82 with MessageListener

use of javax.jms.MessageListener in project cxf by apache.

the class MessageListenerTest method testConnectionProblemXA.

@Test
public void testConnectionProblemXA() throws JMSException, XAException, InterruptedException {
    TransactionManager transactionManager = new GeronimoTransactionManager();
    Connection connection = createXAConnection("brokerJTA", transactionManager);
    Queue dest = JMSUtil.createQueue(connection, "test");
    MessageListener listenerHandler = new TestMessageListener();
    ExceptionListener exListener = createMock(ExceptionListener.class);
    Capture<JMSException> captured = newCapture();
    exListener.onException(capture(captured));
    expectLastCall();
    replay(exListener);
    // 
    PollingMessageListenerContainer container = new PollingMessageListenerContainer(connection, dest, listenerHandler, exListener);
    container.setTransacted(false);
    container.setAcknowledgeMode(Session.SESSION_TRANSACTED);
    container.setTransactionManager(transactionManager);
    // Simulate connection problem
    connection.close();
    container.start();
    Awaitility.await().until(() -> !container.isRunning());
    verify(exListener);
    JMSException ex = captured.getValue();
    // Closing the pooled connection will result in a NPE when using it
    Assert.assertEquals("Wrapped exception. null", ex.getMessage());
}
Also used : GeronimoTransactionManager(org.apache.geronimo.transaction.manager.GeronimoTransactionManager) TransactionManager(javax.transaction.TransactionManager) Connection(javax.jms.Connection) MessageListener(javax.jms.MessageListener) ExceptionListener(javax.jms.ExceptionListener) JMSException(javax.jms.JMSException) GeronimoTransactionManager(org.apache.geronimo.transaction.manager.GeronimoTransactionManager) Queue(javax.jms.Queue) Test(org.junit.Test)

Example 83 with MessageListener

use of javax.jms.MessageListener in project cxf by apache.

the class MessageListenerTest method testNoTransaction.

@Test
public void testNoTransaction() throws JMSException, XAException, InterruptedException {
    Connection connection = createConnection("brokerNoTransaction");
    Queue dest = JMSUtil.createQueue(connection, "test");
    MessageListener listenerHandler = new TestMessageListener();
    AbstractMessageListenerContainer container = new MessageListenerContainer(connection, dest, listenerHandler);
    container.setTransacted(false);
    container.setAcknowledgeMode(Session.AUTO_ACKNOWLEDGE);
    container.start();
    assertNumMessagesInQueue("At the start the queue should be empty", connection, dest, 0, 0);
    sendMessage(connection, dest, OK);
    assertNumMessagesInQueue("This message should be committed", connection, dest, 0, 1000);
    sendMessage(connection, dest, FAIL);
    assertNumMessagesInQueue("Even when an exception occurs the message should be committed", connection, dest, 0, 1000);
    container.stop();
    connection.close();
}
Also used : Connection(javax.jms.Connection) MessageListener(javax.jms.MessageListener) Queue(javax.jms.Queue) Test(org.junit.Test)

Example 84 with MessageListener

use of javax.jms.MessageListener in project rabbitmq-jms-client by rabbitmq.

the class RecoverMessagesIT method testRecoverTextMessageAsyncSync.

@Test
public void testRecoverTextMessageAsyncSync() throws Exception {
    final ArrayList<Message> messages = new ArrayList<Message>();
    queueConn.start();
    QueueSession queueSession = queueConn.createQueueSession(false, Session.CLIENT_ACKNOWLEDGE);
    Queue queue = queueSession.createQueue(QUEUE_NAME);
    QueueSender queueSender = queueSession.createSender(queue);
    queueSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
    TextMessage message = queueSession.createTextMessage(MESSAGE);
    queueSender.send(message);
    QueueReceiver queueReceiver = queueSession.createReceiver(queue);
    final CountDownLatch latch = new CountDownLatch(2);
    queueReceiver.setMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message message) {
            messages.add(message);
            latch.countDown();
        }
    });
    // allow subscription to take place
    Thread.sleep(100);
    // we should have received one message
    assertEquals(1, messages.size());
    TextMessage tmsg1 = (TextMessage) messages.get(0);
    assertFalse(tmsg1.getJMSRedelivered());
    queueSession.recover();
    latch.await(1000, TimeUnit.MILLISECONDS);
    // we should have received two messages
    // There is no synchronisation so no guarantee we see the latest messages!
    assertEquals(2, messages.size());
    TextMessage tmsg2 = (TextMessage) messages.get(1);
    assertEquals(tmsg1, tmsg2);
    assertTrue(tmsg2.getJMSRedelivered());
    tmsg2.acknowledge();
    queueReceiver.setMessageListener(null);
    TextMessage tmsg3 = (TextMessage) queueReceiver.receiveNoWait();
    assertNull(tmsg3);
}
Also used : TextMessage(javax.jms.TextMessage) Message(javax.jms.Message) QueueSender(javax.jms.QueueSender) QueueReceiver(javax.jms.QueueReceiver) ArrayList(java.util.ArrayList) MessageListener(javax.jms.MessageListener) CountDownLatch(java.util.concurrent.CountDownLatch) Queue(javax.jms.Queue) QueueSession(javax.jms.QueueSession) TextMessage(javax.jms.TextMessage) Test(org.junit.jupiter.api.Test)

Example 85 with MessageListener

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

the class JmsBenchmark method testConcurrentSendReceive.

/**
 * @throws Throwable
 */
public void testConcurrentSendReceive() throws Throwable {
    final Semaphore connectionsEstablished = new Semaphore(1 - (CONSUMER_COUNT + PRODUCER_COUNT));
    final Semaphore workerDone = new Semaphore(1 - (CONSUMER_COUNT + PRODUCER_COUNT));
    final CountDownLatch sampleTimeDone = new CountDownLatch(1);
    final AtomicInteger producedMessages = new AtomicInteger(0);
    final AtomicInteger receivedMessages = new AtomicInteger(0);
    final Callable<Object> producer = new Callable<Object>() {

        @Override
        public Object call() throws JMSException, InterruptedException {
            Connection connection = factory.createConnection();
            connections.add(connection);
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            MessageProducer producer = session.createProducer(destination);
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
            BytesMessage message = session.createBytesMessage();
            message.writeBytes(new byte[1024]);
            connection.start();
            connectionsEstablished.release();
            while (!sampleTimeDone.await(0, TimeUnit.MILLISECONDS)) {
                producer.send(message);
                producedMessages.incrementAndGet();
            }
            connection.close();
            workerDone.release();
            return null;
        }
    };
    final Callable<Object> consumer = new Callable<Object>() {

        @Override
        public Object call() throws JMSException, InterruptedException {
            Connection connection = factory.createConnection();
            connections.add(connection);
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            MessageConsumer consumer = session.createConsumer(destination);
            consumer.setMessageListener(new MessageListener() {

                @Override
                public void onMessage(Message msg) {
                    receivedMessages.incrementAndGet();
                }
            });
            connection.start();
            connectionsEstablished.release();
            sampleTimeDone.await();
            connection.close();
            workerDone.release();
            return null;
        }
    };
    final Throwable[] workerError = new Throwable[1];
    for (int i = 0; i < PRODUCER_COUNT; i++) {
        new Thread("Producer:" + i) {

            @Override
            public void run() {
                try {
                    producer.call();
                } catch (Throwable e) {
                    e.printStackTrace();
                    workerError[0] = e;
                }
            }
        }.start();
    }
    for (int i = 0; i < CONSUMER_COUNT; i++) {
        new Thread("Consumer:" + i) {

            @Override
            public void run() {
                try {
                    consumer.call();
                } catch (Throwable e) {
                    e.printStackTrace();
                    workerError[0] = e;
                }
            }
        }.start();
    }
    LOG.info(getName() + ": Waiting for Producers and Consumers to startup.");
    connectionsEstablished.acquire();
    LOG.info("Producers and Consumers are now running.  Waiting for system to reach steady state: " + (SAMPLE_DELAY / 1000.0f) + " seconds");
    Thread.sleep(1000 * 10);
    LOG.info("Starting sample: " + SAMPLES + " each lasting " + (SAMPLE_DURATION / 1000.0f) + " seconds");
    for (int i = 0; i < SAMPLES; i++) {
        long start = System.currentTimeMillis();
        producedMessages.set(0);
        receivedMessages.set(0);
        Thread.sleep(SAMPLE_DURATION);
        long end = System.currentTimeMillis();
        int r = receivedMessages.get();
        int p = producedMessages.get();
        LOG.info("published: " + p + " msgs at " + (p * 1000f / (end - start)) + " msgs/sec, " + "consumed: " + r + " msgs at " + (r * 1000f / (end - start)) + " msgs/sec");
    }
    LOG.info("Sample done.");
    sampleTimeDone.countDown();
    workerDone.acquire();
    if (workerError[0] != null) {
        throw workerError[0];
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) BytesMessage(javax.jms.BytesMessage) Connection(javax.jms.Connection) MessageListener(javax.jms.MessageListener) BytesMessage(javax.jms.BytesMessage) Semaphore(java.util.concurrent.Semaphore) CountDownLatch(java.util.concurrent.CountDownLatch) Callable(java.util.concurrent.Callable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MessageProducer(javax.jms.MessageProducer) Session(javax.jms.Session)

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