Search in sources :

Example 1 with JmsMessage

use of org.apache.camel.component.jms.JmsMessage in project camel by apache.

the class ReplyManagerSupport method processReply.

public void processReply(ReplyHolder holder) {
    if (holder != null && isRunAllowed()) {
        try {
            Exchange exchange = holder.getExchange();
            boolean timeout = holder.isTimeout();
            if (timeout) {
                // timeout occurred do a WARN log so its easier to spot in the logs
                if (log.isWarnEnabled()) {
                    log.warn("Timeout occurred after {} millis waiting for reply message with correlationID [{}] on destination {}." + " Setting ExchangeTimedOutException on {} and continue routing.", new Object[] { holder.getRequestTimeout(), holder.getCorrelationId(), replyTo, ExchangeHelper.logIds(exchange) });
                }
                // no response, so lets set a timed out exception
                String msg = "reply message with correlationID: " + holder.getCorrelationId() + " not received on destination: " + replyTo;
                exchange.setException(new ExchangeTimedOutException(exchange, holder.getRequestTimeout(), msg));
            } else {
                Message message = holder.getMessage();
                Session session = holder.getSession();
                JmsMessage response = new JmsMessage(message, session, endpoint.getBinding());
                // the JmsBinding is designed to be "pull-based": it will populate the Camel message on demand
                // therefore, we link Exchange and OUT message before continuing, so that the JmsBinding has full access 
                // to everything it may need, and can populate headers, properties, etc. accordingly (solves CAMEL-6218).
                exchange.setOut(response);
                Object body = response.getBody();
                if (endpoint.isTransferException() && body instanceof Exception) {
                    log.debug("Reply was an Exception. Setting the Exception on the Exchange: {}", body);
                    // we got an exception back and endpoint was configured to transfer exception
                    // therefore set response as exception
                    exchange.setException((Exception) body);
                } else {
                    log.debug("Reply received. OUT message body set to reply payload: {}", body);
                }
                if (endpoint.isTransferFault()) {
                    // remove the header as we do not want to keep it on the Camel Message either
                    Object faultHeader = response.removeHeader(JmsConstants.JMS_TRANSFER_FAULT);
                    if (faultHeader != null) {
                        boolean isFault = exchange.getContext().getTypeConverter().tryConvertTo(boolean.class, faultHeader);
                        log.debug("Transfer fault on OUT message: {}", isFault);
                        if (isFault) {
                            exchange.getOut().setFault(true);
                        }
                    }
                }
                // restore correlation id in case the remote server messed with it
                if (holder.getOriginalCorrelationId() != null) {
                    JmsMessageHelper.setCorrelationId(message, holder.getOriginalCorrelationId());
                    exchange.getOut().setHeader("JMSCorrelationID", holder.getOriginalCorrelationId());
                }
            }
        } finally {
            // notify callback
            AsyncCallback callback = holder.getCallback();
            callback.done(false);
        }
    }
}
Also used : Exchange(org.apache.camel.Exchange) Message(javax.jms.Message) JmsMessage(org.apache.camel.component.jms.JmsMessage) AsyncCallback(org.apache.camel.AsyncCallback) ExchangeTimedOutException(org.apache.camel.ExchangeTimedOutException) JMSException(javax.jms.JMSException) ExchangeTimedOutException(org.apache.camel.ExchangeTimedOutException) Session(javax.jms.Session) JmsMessage(org.apache.camel.component.jms.JmsMessage)

Example 2 with JmsMessage

use of org.apache.camel.component.jms.JmsMessage in project camel by apache.

the class JmsGetHeaderKeyFormatIssueWithContentTypeHeaderTest method createRouteBuilder.

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
    return new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from(uri).process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    assertEquals("text/plain", exchange.getIn().getHeader("Content-Type"));
                    // do not mutate it
                    JmsMessage msg = assertIsInstanceOf(JmsMessage.class, exchange.getIn());
                    assertNotNull("javax.jms.Message should not be null", msg.getJmsMessage());
                }
            }).to("activemq:queue:copy", "mock:result");
            from("activemq:queue:copy").to("mock:copy");
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) JmsMessage(org.apache.camel.component.jms.JmsMessage)

Example 3 with JmsMessage

use of org.apache.camel.component.jms.JmsMessage in project wildfly-camel by wildfly-extras.

the class JMSIntegrationTest method testMessageConsumerRouteWithClientAck.

@Test
public void testMessageConsumerRouteWithClientAck() throws Exception {
    final List<String> result = new ArrayList<>();
    final CountDownLatch latch = new CountDownLatch(3);
    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            fromF("jms:queue:%s?connectionFactory=ConnectionFactory&acknowledgementModeName=CLIENT_ACKNOWLEDGE", QUEUE_NAME).process(exchange -> {
                JmsMessage in = (JmsMessage) exchange.getIn();
                Session session = in.getJmsSession();
                TextMessage message = (TextMessage) in.getJmsMessage();
                long count = latch.getCount();
                try {
                    // always append the message text
                    result.add(message.getText() + " " + (4 - count));
                    if (count == 3) {
                    // do nothing on first
                    // note, this message is still acked because of
                    // [CAMEL-8749] JMS message always acknowledged even with CLIENT_ACKNOWLEDGE
                    } else if (count == 2) {
                        // recover causing a redelivery
                        session.recover();
                    } else {
                        // acknowledge
                        message.acknowledge();
                    }
                } catch (JMSException ex) {
                    result.add(ex.getMessage());
                }
                latch.countDown();
            }).transform(simple("Hello ${body}")).to("seda:end");
        }
    });
    camelctx.start();
    PollingConsumer consumer = camelctx.getEndpoint("seda:end").createPollingConsumer();
    consumer.start();
    try {
        // Send a message to the queue
        ConnectionFactory cfactory = (ConnectionFactory) initialctx.lookup("java:/ConnectionFactory");
        Connection connection = cfactory.createConnection();
        Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
        connection.start();
        Destination destination = (Destination) initialctx.lookup(QUEUE_JNDI_NAME);
        MessageProducer producer = session.createProducer(destination);
        try {
            producer.send(session.createTextMessage("Kermit"));
            producer.send(session.createTextMessage("Piggy"));
            Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
            Assert.assertEquals("Four messages", 3, result.size());
            Assert.assertEquals("Kermit 1", result.get(0));
            Assert.assertEquals("Piggy 2", result.get(1));
            Assert.assertEquals("Piggy 3", result.get(2));
        } finally {
            connection.close();
        }
    } finally {
        camelctx.stop();
    }
}
Also used : DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) CamelContext(org.apache.camel.CamelContext) ServerSetupTask(org.jboss.as.arquillian.api.ServerSetupTask) Arquillian(org.jboss.arquillian.junit.Arquillian) PollingConsumer(org.apache.camel.PollingConsumer) ServerSetup(org.jboss.as.arquillian.api.ServerSetup) RunWith(org.junit.runner.RunWith) MessageListener(javax.jms.MessageListener) JMSUtils(org.wildfly.camel.test.common.utils.JMSUtils) ArrayList(java.util.ArrayList) ActiveMQTextMessage(org.apache.activemq.command.ActiveMQTextMessage) Session(javax.jms.Session) CamelAware(org.wildfly.extension.camel.CamelAware) ArquillianResource(org.jboss.arquillian.test.api.ArquillianResource) ProducerTemplate(org.apache.camel.ProducerTemplate) MessageConversionException(org.springframework.jms.support.converter.MessageConversionException) MessageProducer(javax.jms.MessageProducer) Message(javax.jms.Message) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) JmsMessage(org.apache.camel.component.jms.JmsMessage) Connection(javax.jms.Connection) InitialContext(javax.naming.InitialContext) CamelContext(org.apache.camel.CamelContext) ShrinkWrap(org.jboss.shrinkwrap.api.ShrinkWrap) TextMessage(javax.jms.TextMessage) Test(org.junit.Test) JMSException(javax.jms.JMSException) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) MessageConsumer(javax.jms.MessageConsumer) Destination(javax.jms.Destination) RouteBuilder(org.apache.camel.builder.RouteBuilder) Deployment(org.jboss.arquillian.container.test.api.Deployment) JavaArchive(org.jboss.shrinkwrap.api.spec.JavaArchive) MessageConverter(org.springframework.jms.support.converter.MessageConverter) ManagementClient(org.jboss.as.arquillian.container.ManagementClient) Assert(org.junit.Assert) ConnectionFactory(javax.jms.ConnectionFactory) Destination(javax.jms.Destination) PollingConsumer(org.apache.camel.PollingConsumer) RouteBuilder(org.apache.camel.builder.RouteBuilder) ArrayList(java.util.ArrayList) Connection(javax.jms.Connection) 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) MessageProducer(javax.jms.MessageProducer) ActiveMQTextMessage(org.apache.activemq.command.ActiveMQTextMessage) TextMessage(javax.jms.TextMessage) JmsMessage(org.apache.camel.component.jms.JmsMessage) Session(javax.jms.Session) Test(org.junit.Test)

Example 4 with JmsMessage

use of org.apache.camel.component.jms.JmsMessage in project camel by apache.

the class JmsMutateMessageTest method createRouteBuilder.

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
    return new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from(uri).process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    // do not mutate it
                    JmsMessage msg = assertIsInstanceOf(JmsMessage.class, exchange.getIn());
                    assertNotNull("javax.jms.Message should not be null", msg.getJmsMessage());
                    // get header should not mutate it
                    assertEquals("VALUE_1", exchange.getIn().getHeader("HEADER_1"));
                }
            }).setHeader("HEADER_1", constant("VALUE_2")).process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    // it should have been mutated
                    JmsMessage msg = assertIsInstanceOf(JmsMessage.class, exchange.getIn());
                    assertNotNull("javax.jms.Message should not be null", msg.getJmsMessage());
                    // get header should not mutate it
                    assertEquals("VALUE_2", exchange.getIn().getHeader("HEADER_1"));
                }
            }).to("mock:result");
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) JmsMessage(org.apache.camel.component.jms.JmsMessage)

Example 5 with JmsMessage

use of org.apache.camel.component.jms.JmsMessage in project camel by apache.

the class JmsMutateRemoveHeaderMessageTest method createRouteBuilder.

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
    return new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from(uri).process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    // do not mutate it
                    JmsMessage msg = assertIsInstanceOf(JmsMessage.class, exchange.getIn());
                    assertNotNull("javax.jms.Message should not be null", msg.getJmsMessage());
                    // get header should not mutate it
                    assertEquals("VALUE_1", exchange.getIn().getHeader("HEADER_1"));
                }
            }).removeHeader("HEADER_1").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    // it should have been mutated
                    JmsMessage msg = assertIsInstanceOf(JmsMessage.class, exchange.getIn());
                    assertNotNull("javax.jms.Message should not be null", msg.getJmsMessage());
                    // get header should not mutate it
                    assertNull("Header should have been removed", exchange.getIn().getHeader("HEADER_1"));
                }
            }).to("mock:result");
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) JmsMessage(org.apache.camel.component.jms.JmsMessage)

Aggregations

JmsMessage (org.apache.camel.component.jms.JmsMessage)6 Exchange (org.apache.camel.Exchange)5 RouteBuilder (org.apache.camel.builder.RouteBuilder)5 Processor (org.apache.camel.Processor)4 JMSException (javax.jms.JMSException)2 Message (javax.jms.Message)2 Session (javax.jms.Session)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 TimeUnit (java.util.concurrent.TimeUnit)1 Connection (javax.jms.Connection)1 ConnectionFactory (javax.jms.ConnectionFactory)1 Destination (javax.jms.Destination)1 MessageConsumer (javax.jms.MessageConsumer)1 MessageListener (javax.jms.MessageListener)1 MessageProducer (javax.jms.MessageProducer)1 TextMessage (javax.jms.TextMessage)1 InitialContext (javax.naming.InitialContext)1 ActiveMQTextMessage (org.apache.activemq.command.ActiveMQTextMessage)1