final ValueHolder<FutureTask> futureHolder = new ValueHolder<FutureTask>();
final DeferredMessageSentCallback callback = msgIdAsCorrId ? deferredRequestReplyMap.createDeferredMessageSentCallback() : null;
final CamelJmsTemplate template = (CamelJmsTemplate)getInOutTemplate();
template.send(endpoint.getDestination(), new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
Message message = endpoint.getBinding().makeJmsMessage(exchange, in, session);
message.setJMSReplyTo(replyTo);
requestor.setReplyToSelectorHeader(in, message);
FutureTask future = null;
future = (!msgIdAsCorrId)
? requestor.getReceiveFuture(message.getJMSCorrelationID(), endpoint
.getRequestTimeout()) : requestor.getReceiveFuture(callback);
futureHolder.set(future);
if (LOG.isDebugEnabled()) {
LOG.debug(endpoint + " sending JMS message: " + message);
}
return message;
}
}, callback);
setMessageId(exchange);
// lets wait and return the response
long requestTimeout = endpoint.getConfiguration().getRequestTimeout();
try {
Message message = null;
try {
if (requestTimeout < 0) {
message = (Message)futureHolder.get().get();
} else {
message = (Message)futureHolder.get().get(requestTimeout, TimeUnit.MILLISECONDS);
}
} catch (InterruptedException e) {
if (LOG.isDebugEnabled()) {
LOG.debug("Future interupted: " + e, e);
}
} catch (TimeoutException e) {
if (LOG.isDebugEnabled()) {
LOG.debug("Future timed out: " + e, e);
}
}
if (message != null) {
exchange.setOut(new JmsMessage(message, endpoint.getBinding()));
if (correlationId != null) {
message.setJMSCorrelationID(correlationId);
exchange.getOut(false).setHeader("JMSCorrelationID", correlationId);
}
} else {
// lets set a timed out exception
exchange.setException(new ExchangeTimedOutException(exchange, requestTimeout));
}
} catch (Exception e) {
exchange.setException(e);
}
} else {
if (!endpoint.getConfiguration().isPreserveMessageQos() && !endpoint.getConfiguration().isExplicitQosEnabled()
&& exchange.getIn().getHeaders().containsKey("JMSReplyTo")) {
// we are routing an existing JmsMessage, origin from another JMS endpoint
// then we need to remove the existing JMSReplyTo, JMSCorrelationID.
// as we are not out capable and thus do not expect a reply, and therefore
// the consumer of this message we send should not return a reply
String to = endpoint.getDestination();
LOG.warn("Disabling JMSReplyTo as this Exchange is not OUT capable: " + exchange + " with destination: " + to);
exchange.getIn().setHeader("JMSReplyTo", null);
}
getInOnlyTemplate().send(endpoint.getDestination(), new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
Message message = endpoint.getBinding().makeJmsMessage(exchange, in, session);
if (LOG.isDebugEnabled()) {
LOG.debug(endpoint + " sending JMS message: " + message);
}