Search in sources :

Example 6 with MessageListener

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

the class MessageListenerTest method testConnectionProblem.

@Test
public void testConnectionProblem() throws JMSException {
    Connection connection = createConnection("broker");
    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);
    // Simulate connection problem
    connection.close();
    container.start();
    Awaitility.await().until(() -> !container.isRunning());
    verify(exListener);
    JMSException ex = captured.getValue();
    Assert.assertEquals("The connection is already closed", ex.getMessage());
}
Also used : Connection(javax.jms.Connection) MessageListener(javax.jms.MessageListener) ExceptionListener(javax.jms.ExceptionListener) JMSException(javax.jms.JMSException) Queue(javax.jms.Queue) Test(org.junit.Test)

Example 7 with MessageListener

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

the class MessageListenerTest method testWithJTA.

@Test
public void testWithJTA() 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 = new ExceptionListener() {

        @Override
        public void onException(JMSException exception) {
        }
    };
    PollingMessageListenerContainer container = new PollingMessageListenerContainer(connection, dest, listenerHandler, exListener);
    container.setTransacted(false);
    container.setAcknowledgeMode(Session.SESSION_TRANSACTED);
    container.setTransactionManager(transactionManager);
    container.start();
    testTransactionalBehaviour(connection, dest);
    container.stop();
    connection.close();
}
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 8 with MessageListener

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

the class MessageListenerContainer method start.

@Override
public void start() {
    try {
        session = connection.createSession(transacted, acknowledgeMode);
        if (durableSubscriptionName != null && destination instanceof Topic) {
            consumer = session.createDurableSubscriber((Topic) destination, durableSubscriptionName, messageSelector, pubSubNoLocal);
        } else {
            consumer = session.createConsumer(destination, messageSelector);
        }
        MessageListener intListener = new LocalTransactionalMessageListener(session, listenerHandler);
        // new DispachingListener(getExecutor(), listenerHandler);
        consumer.setMessageListener(intListener);
        running = true;
    } catch (JMSException e) {
        throw JMSUtil.convertJmsException(e);
    }
}
Also used : MessageListener(javax.jms.MessageListener) JMSException(javax.jms.JMSException) Topic(javax.jms.Topic)

Example 9 with MessageListener

use of javax.jms.MessageListener in project JFramework by gugumall.

the class Receiver method listen.

/**
 * 初始化并开始监听消息
 * @throws Exception
 */
private void listen() throws Exception {
    try {
        MessageListener listener = (MessageListener) Class.forName(this.getProperty("LISTENER")).newInstance();
        if (type == 0) {
            initP2P(DeliveryMode.PERSISTENT);
            queueReceiver.setMessageListener(listener);
        } else if (type == 1) {
            initP2S(DeliveryMode.PERSISTENT);
            subscriber.setMessageListener(listener);
        } else if (type == 2) {
            initP2C(DeliveryMode.PERSISTENT);
            if (this.durable == Parameters.PERSISTENT) {
                subscriber.setMessageListener(listener);
            } else {
                consumer.setMessageListener(listener);
            }
        } else {
            throw new Exception("type illegal!");
        }
        System.out.println("message listener is to listen......\r\nlistener name is: " + this.getProperty("LISTENER"));
    } catch (Exception e) {
        log.log(e, Logger.LEVEL_ERROR);
        this.close();
    }
}
Also used : MessageListener(javax.jms.MessageListener)

Example 10 with MessageListener

use of javax.jms.MessageListener in project wildfly-camel by wildfly-extras.

the class JMSIntegrationTest method testMessageProviderRouteWithClientAck.

@Test
public void testMessageProviderRouteWithClientAck() throws Exception {
    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("direct:start").transform(simple("Hello ${body}")).toF("jms:queue:%s?connectionFactory=ConnectionFactory", QUEUE_NAME);
        }
    });
    final List<String> result = new ArrayList<>();
    final CountDownLatch latch = new CountDownLatch(4);
    camelctx.start();
    try {
        // Get the message from the queue
        ConnectionFactory cfactory = (ConnectionFactory) initialctx.lookup("java:/ConnectionFactory");
        Connection connection = cfactory.createConnection();
        final Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
        Destination destination = (Destination) initialctx.lookup(QUEUE_JNDI_NAME);
        MessageConsumer consumer = session.createConsumer(destination);
        consumer.setMessageListener(new MessageListener() {

            @Override
            public synchronized void onMessage(Message message) {
                TextMessage text = (TextMessage) message;
                long count = latch.getCount();
                try {
                    // always append the message text
                    result.add(text.getText() + " " + (5 - count));
                    if (count == 4) {
                    // do nothing on first
                    } else if (count == 3) {
                        // recover causing a redelivery
                        session.recover();
                    } else {
                        // ackknowledge
                        message.acknowledge();
                    }
                } catch (JMSException ex) {
                    result.add(ex.getMessage());
                }
                latch.countDown();
            }
        });
        connection.start();
        try {
            ProducerTemplate producer = camelctx.createProducerTemplate();
            producer.asyncSendBody("direct:start", "Message");
            producer.asyncSendBody("direct:start", "Message");
            Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
            Assert.assertEquals("Four messages", 4, result.size());
            Assert.assertEquals("Hello Message 1", result.get(0));
            Assert.assertEquals("Hello Message 2", result.get(1));
            Assert.assertEquals("Hello Message 3", result.get(2));
            Assert.assertEquals("Hello Message 4", result.get(3));
        } finally {
            connection.close();
        }
    } finally {
        camelctx.stop();
    }
}
Also used : DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) CamelContext(org.apache.camel.CamelContext) Destination(javax.jms.Destination) ProducerTemplate(org.apache.camel.ProducerTemplate) MessageConsumer(javax.jms.MessageConsumer) RouteBuilder(org.apache.camel.builder.RouteBuilder) ActiveMQTextMessage(org.apache.activemq.command.ActiveMQTextMessage) Message(javax.jms.Message) JmsMessage(org.apache.camel.component.jms.JmsMessage) TextMessage(javax.jms.TextMessage) ArrayList(java.util.ArrayList) Connection(javax.jms.Connection) MessageListener(javax.jms.MessageListener) JMSException(javax.jms.JMSException) CountDownLatch(java.util.concurrent.CountDownLatch) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) MessageConversionException(org.springframework.jms.support.converter.MessageConversionException) JMSException(javax.jms.JMSException) ConnectionFactory(javax.jms.ConnectionFactory) ActiveMQTextMessage(org.apache.activemq.command.ActiveMQTextMessage) 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