Search in sources :

Example 91 with MessageConsumer

use of javax.jms.MessageConsumer in project quickstarts by jboss-switchyard.

the class JCAOutboundBindingTest method testOrderService.

@Test
public void testOrderService() throws Exception {
    String[] orders = { "BREAD", "PIZZA", "JAM", "POTATO", "MILK", "JAM" };
    Connection conn = _connectionFactory.createConnection(HORNETQ_USER, HORNETQ_PASSWORD);
    conn.start();
    try {
        Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer producer = session.createProducer(_orderQueue);
        for (String order : orders) {
            final TextMessage message = session.createTextMessage();
            message.setText(order);
            producer.send(message);
        }
        session.close();
        session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageConsumer consumer = session.createConsumer(_orderQueue);
        Assert.assertNull("Request message is still in the queue:" + _orderQueue, consumer.receive(3000));
        consumer.close();
        consumer = session.createConsumer(_shippingQueue);
        List<String> expectedShippingOrders = new ArrayList<String>(Arrays.asList("BREAD", "JAM", "MILK", "JAM"));
        Message msg = null;
        while ((msg = consumer.receive(1000)) != null) {
            Assert.assertTrue(expectedShippingOrders.remove(TextMessage.class.cast(msg).getText()));
        }
        Assert.assertEquals(0, expectedShippingOrders.size());
        consumer.close();
        consumer = session.createConsumer(_fillingStockQueue);
        List<String> expectedFillingStockOrders = new ArrayList<String>(Arrays.asList("PIZZA", "POTATO"));
        while ((msg = consumer.receive(1000)) != null) {
            Assert.assertTrue(expectedFillingStockOrders.remove(TextMessage.class.cast(msg).getText()));
        }
        Assert.assertEquals(0, expectedFillingStockOrders.size());
        consumer.close();
    } finally {
        conn.close();
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) Connection(javax.jms.Connection) ArrayList(java.util.ArrayList) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) Test(org.junit.Test)

Example 92 with MessageConsumer

use of javax.jms.MessageConsumer in project camel by apache.

the class InOnlyTopicProducerTest method testInOnlyTopicProducerProducer.

@Test
public void testInOnlyTopicProducerProducer() throws Exception {
    MessageConsumer mc = createTopicConsumer(TEST_DESTINATION_NAME, null);
    assertNotNull(mc);
    final String expectedBody = "Hello World!";
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(1);
    mock.expectedBodiesReceived(expectedBody);
    template.sendBody("direct:start", expectedBody);
    Message message = mc.receive(5000);
    assertNotNull(message);
    assertTrue(message instanceof TextMessage);
    TextMessage tm = (TextMessage) message;
    String text = tm.getText();
    assertNotNull(text);
    template.sendBody("direct:finish", text);
    mock.assertIsSatisfied();
    mc.close();
}
Also used : MessageConsumer(javax.jms.MessageConsumer) TextMessage(javax.jms.TextMessage) Message(javax.jms.Message) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) TextMessage(javax.jms.TextMessage) Test(org.junit.Test)

Example 93 with MessageConsumer

use of javax.jms.MessageConsumer in project camel by apache.

the class InOutQueueProducerTest method testInOutQueueProducerWithCorrelationId.

@Test
public void testInOutQueueProducerWithCorrelationId() throws Exception {
    MessageConsumer mc = createQueueConsumer(TEST_DESTINATION_NAME + ".request");
    assertNotNull(mc);
    final String requestText = "Hello World!";
    final String responseText = "How are you";
    mc.setMessageListener(new MyMessageListener(requestText, responseText));
    final String correlationId = UUID.randomUUID().toString().replace("-", "");
    Exchange exchange = template.request("direct:start", new Processor() {

        @Override
        public void process(Exchange exchange) throws Exception {
            exchange.getOut().setBody(requestText);
            exchange.getOut().setHeader("JMSCorrelationID", correlationId);
        }
    });
    assertNotNull(exchange);
    assertTrue(exchange.getIn().getBody() instanceof String);
    assertEquals(responseText, exchange.getIn().getBody());
    assertEquals(correlationId, exchange.getIn().getHeader("JMSCorrelationID", String.class));
    mc.close();
}
Also used : Exchange(org.apache.camel.Exchange) MessageConsumer(javax.jms.MessageConsumer) Processor(org.apache.camel.Processor) JMSException(javax.jms.JMSException) Test(org.junit.Test)

Example 94 with MessageConsumer

use of javax.jms.MessageConsumer in project ignite by apache.

the class JmsStreamer method initializeJmsObjectsForTopic.

private void initializeJmsObjectsForTopic() throws JMSException {
    Session session = connection.createSession(transacted, Session.AUTO_ACKNOWLEDGE);
    Topic topic = (Topic) destination;
    if (destination == null)
        topic = session.createTopic(destinationName);
    MessageConsumer consumer = durableSubscription ? session.createDurableSubscriber(topic, durableSubscriptionName) : session.createConsumer(topic);
    IgniteJmsMessageListener messageListener = new IgniteJmsMessageListener(session, true);
    consumer.setMessageListener(messageListener);
    consumers.add(consumer);
    sessions.add(session);
    listeners.add(messageListener);
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Topic(javax.jms.Topic) Session(javax.jms.Session)

Example 95 with MessageConsumer

use of javax.jms.MessageConsumer in project karaf by apache.

the class PooledSession method close.

@Override
public void close() throws JMSException {
    if (!ignoreClose) {
        boolean invalidate = false;
        try {
            // lets reset the session
            getInternalSession().setMessageListener(null);
            // Close any consumers and browsers that may have been created.
            for (MessageConsumer consumer : consumers) {
                consumer.close();
            }
            for (QueueBrowser browser : browsers) {
                browser.close();
            }
            if (transactional && !isXa) {
                try {
                    getInternalSession().rollback();
                } catch (JMSException e) {
                    invalidate = true;
                    LOG.warn("Caught exception trying rollback() when putting session back into the pool, will invalidate. " + e, e);
                }
            }
        } catch (JMSException ex) {
            invalidate = true;
            LOG.warn("Caught exception trying close() when putting session back into the pool, will invalidate. " + ex, ex);
        } finally {
            consumers.clear();
            browsers.clear();
            for (PooledSessionEventListener listener : this.sessionEventListeners) {
                listener.onSessionClosed(this);
            }
            sessionEventListeners.clear();
        }
        if (invalidate) {
            // instead invalidate it so the pool can create a new one on demand.
            if (session != null) {
                try {
                    session.close();
                } catch (JMSException e1) {
                    LOG.trace("Ignoring exception on close as discarding session: " + e1, e1);
                }
                session = null;
            }
            try {
                sessionPool.invalidateObject(key, this);
            } catch (Exception e) {
                LOG.trace("Ignoring exception on invalidateObject as discarding session: " + e, e);
            }
        } else {
            try {
                sessionPool.returnObject(key, this);
            } catch (Exception e) {
                javax.jms.IllegalStateException illegalStateException = new javax.jms.IllegalStateException(e.toString());
                illegalStateException.initCause(e);
                throw illegalStateException;
            }
        }
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) JMSException(javax.jms.JMSException) QueueBrowser(javax.jms.QueueBrowser) JMSException(javax.jms.JMSException)

Aggregations

MessageConsumer (javax.jms.MessageConsumer)107 Session (javax.jms.Session)64 Message (javax.jms.Message)50 MessageProducer (javax.jms.MessageProducer)49 Connection (javax.jms.Connection)47 TextMessage (javax.jms.TextMessage)45 Test (org.junit.Test)45 Destination (javax.jms.Destination)24 JMSException (javax.jms.JMSException)23 ConnectionFactory (javax.jms.ConnectionFactory)15 TemporaryQueue (javax.jms.TemporaryQueue)9 Queue (javax.jms.Queue)8 CountDownLatch (java.util.concurrent.CountDownLatch)7 ObjectMessage (javax.jms.ObjectMessage)7 MessageListener (javax.jms.MessageListener)6 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)5 ActiveMQConnectionFactory (org.apache.activemq.ActiveMQConnectionFactory)4 HornetQMixIn (org.switchyard.component.test.mixins.hornetq.HornetQMixIn)4 Serializable (java.io.Serializable)3 ArrayList (java.util.ArrayList)3