use of javax.jms.Message in project camel by apache.
the class JmsProducer method processInOut.
protected boolean processInOut(final Exchange exchange, final AsyncCallback callback) {
final org.apache.camel.Message in = exchange.getIn();
String destinationName = in.getHeader(JmsConstants.JMS_DESTINATION_NAME, String.class);
// remove the header so it wont be propagated
in.removeHeader(JmsConstants.JMS_DESTINATION_NAME);
if (destinationName == null) {
destinationName = endpoint.getDestinationName();
}
Destination destination = in.getHeader(JmsConstants.JMS_DESTINATION, Destination.class);
// remove the header so it wont be propagated
in.removeHeader(JmsConstants.JMS_DESTINATION);
if (destination == null) {
destination = endpoint.getDestination();
}
if (destination != null) {
// prefer to use destination over destination name
destinationName = null;
}
initReplyManager();
// the request timeout can be overruled by a header otherwise the endpoint configured value is used
final long timeout = exchange.getIn().getHeader(JmsConstants.JMS_REQUEST_TIMEOUT, endpoint.getRequestTimeout(), long.class);
final JmsConfiguration configuration = endpoint.getConfiguration();
// when using message id as correlation id, we need at first to use a provisional correlation id
// which we then update to the real JMSMessageID when the message has been sent
// this is done with the help of the MessageSentCallback
final boolean msgIdAsCorrId = configuration.isUseMessageIDAsCorrelationID();
final String provisionalCorrelationId = msgIdAsCorrId ? getUuidGenerator().generateUuid() : null;
MessageSentCallback messageSentCallback = null;
if (msgIdAsCorrId) {
messageSentCallback = new UseMessageIdAsCorrelationIdMessageSentCallback(replyManager, provisionalCorrelationId, timeout);
}
final String correlationProperty = configuration.getCorrelationProperty();
final String correlationPropertyToUse = ofNullable(correlationProperty).orElse("JMSCorrelationID");
final String originalCorrelationId = in.getHeader(correlationPropertyToUse, String.class);
boolean generateFreshCorrId = (ObjectHelper.isEmpty(originalCorrelationId) && !msgIdAsCorrId) || (originalCorrelationId != null && originalCorrelationId.startsWith(GENERATED_CORRELATION_ID_PREFIX));
if (generateFreshCorrId) {
// we append the 'Camel-' prefix to know it was generated by us
in.setHeader(correlationPropertyToUse, GENERATED_CORRELATION_ID_PREFIX + getUuidGenerator().generateUuid());
}
MessageCreator messageCreator = new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
Message answer = endpoint.getBinding().makeJmsMessage(exchange, in, session, null);
Destination replyTo = null;
String replyToOverride = configuration.getReplyToOverride();
if (replyToOverride != null) {
replyTo = resolveOrCreateDestination(replyToOverride, session);
} else {
// get the reply to destination to be used from the reply manager
replyTo = replyManager.getReplyTo();
}
if (replyTo == null) {
throw new RuntimeExchangeException("Failed to resolve replyTo destination", exchange);
}
JmsMessageHelper.setJMSReplyTo(answer, replyTo);
replyManager.setReplyToSelectorHeader(in, answer);
String correlationId = determineCorrelationId(answer, provisionalCorrelationId);
replyManager.registerReply(replyManager, exchange, callback, originalCorrelationId, correlationId, timeout);
if (correlationProperty != null) {
replyManager.setCorrelationProperty(correlationProperty);
}
if (LOG.isDebugEnabled()) {
LOG.debug("Using {}: {}, JMSReplyTo destination: {}, with request timeout: {} ms.", new Object[] { correlationPropertyToUse, correlationId, replyTo, timeout });
}
LOG.trace("Created javax.jms.Message: {}", answer);
return answer;
}
};
doSend(true, destinationName, destination, messageCreator, messageSentCallback);
// continue routing asynchronously (reply will be processed async when its received)
return false;
}
use of javax.jms.Message in project camel by apache.
the class JmsProducer method processInOnly.
protected boolean processInOnly(final Exchange exchange, final AsyncCallback callback) {
final org.apache.camel.Message in = exchange.getIn();
String destinationName = in.getHeader(JmsConstants.JMS_DESTINATION_NAME, String.class);
if (destinationName != null) {
// remove the header so it wont be propagated
in.removeHeader(JmsConstants.JMS_DESTINATION_NAME);
}
if (destinationName == null) {
destinationName = endpoint.getDestinationName();
}
Destination destination = in.getHeader(JmsConstants.JMS_DESTINATION, Destination.class);
if (destination != null) {
// remove the header so it wont be propagated
in.removeHeader(JmsConstants.JMS_DESTINATION);
}
if (destination == null) {
destination = endpoint.getDestination();
}
if (destination != null) {
// prefer to use destination over destination name
destinationName = null;
}
final String to = destinationName != null ? destinationName : "" + destination;
MessageSentCallback messageSentCallback = getEndpoint().getConfiguration().isIncludeSentJMSMessageID() ? new InOnlyMessageSentCallback(exchange) : null;
MessageCreator messageCreator = new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
Message answer = endpoint.getBinding().makeJmsMessage(exchange, in, session, null);
// when in InOnly mode the JMSReplyTo is a bit complicated
// we only want to set the JMSReplyTo on the answer if
// there is a JMSReplyTo from the header/endpoint and
// we have been told to preserveMessageQos
Object jmsReplyTo = JmsMessageHelper.getJMSReplyTo(answer);
if (endpoint.isDisableReplyTo()) {
// honor disable reply to configuration
LOG.trace("ReplyTo is disabled on endpoint: {}", endpoint);
JmsMessageHelper.setJMSReplyTo(answer, null);
} else {
// if the binding did not create the reply to then we have to try to create it here
if (jmsReplyTo == null) {
// prefer reply to from header over endpoint configured
jmsReplyTo = exchange.getIn().getHeader("JMSReplyTo", String.class);
if (jmsReplyTo == null) {
jmsReplyTo = endpoint.getReplyTo();
}
}
}
// unless we use preserveMessageQos=true to tell that we still want to use JMSReplyTo
if (jmsReplyTo != null && !(endpoint.isPreserveMessageQos() || endpoint.isExplicitQosEnabled())) {
// this behavior is also documented at the camel website
if (LOG.isDebugEnabled()) {
LOG.debug("Disabling JMSReplyTo: {} for destination: {}. Use preserveMessageQos=true to force Camel to keep the JMSReplyTo on endpoint: {}", new Object[] { jmsReplyTo, to, endpoint });
}
jmsReplyTo = null;
}
// and if needed create the destination using the session if needed to
if (jmsReplyTo != null && jmsReplyTo instanceof String) {
String replyTo = (String) jmsReplyTo;
// we need to null it as we use the String to resolve it as a Destination instance
jmsReplyTo = resolveOrCreateDestination(replyTo, session);
}
// set the JMSReplyTo on the answer if we are to use it
Destination replyTo = null;
String replyToOverride = endpoint.getConfiguration().getReplyToOverride();
if (replyToOverride != null) {
replyTo = resolveOrCreateDestination(replyToOverride, session);
} else if (jmsReplyTo instanceof Destination) {
replyTo = (Destination) jmsReplyTo;
}
if (replyTo != null) {
LOG.debug("Using JMSReplyTo destination: {}", replyTo);
JmsMessageHelper.setJMSReplyTo(answer, replyTo);
} else {
// do not use JMSReplyTo
log.trace("Not using JMSReplyTo");
JmsMessageHelper.setJMSReplyTo(answer, null);
}
LOG.trace("Created javax.jms.Message: {}", answer);
return answer;
}
};
doSend(false, destinationName, destination, messageCreator, messageSentCallback);
// after sending then set the OUT message id to the JMSMessageID so its identical
setMessageId(exchange);
// we are synchronous so return true
callback.done(true);
return true;
}
use of javax.jms.Message 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 javax.jms.Message in project camel by apache.
the class JmsBinding method createJmsMessage.
protected Message createJmsMessage(Exchange exchange, org.apache.camel.Message camelMessage, Session session, CamelContext context) throws JMSException {
Message answer = createJmsMessage(exchange, camelMessage.getBody(), camelMessage.getHeaders(), session, context);
// special for transferFault
boolean isFault = camelMessage.isFault();
if (answer != null && isFault && endpoint != null && endpoint.isTransferFault()) {
answer.setBooleanProperty(JmsConstants.JMS_TRANSFER_FAULT, true);
}
return answer;
}
use of javax.jms.Message in project camel by apache.
the class ConsumeJmsObjectMessageTest method testConsumeObjectMessage.
@Test
public void testConsumeObjectMessage() throws Exception {
endpoint.expectedMessageCount(1);
jmsTemplate.setPubSubDomain(false);
jmsTemplate.send("test.object", new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
ObjectMessage msg = session.createObjectMessage();
MyUser user = new MyUser();
user.setName("Claus");
msg.setObject(user);
return msg;
}
});
endpoint.assertIsSatisfied();
assertCorrectObjectReceived();
}
Aggregations