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);
}
}
}
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");
}
};
}
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();
}
}
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");
}
};
}
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");
}
};
}
Aggregations