JMSMessageHeadersType headers = getOrCreateJmsHeaders(outMessage);
String replyTo = headers.getJMSReplyTo();
if (replyTo == null) {
replyTo = jmsConfig.getReplyDestination();
}
final JmsTemplate jmsTemplate = JMSFactory.createJmsTemplate(jmsConfig, headers);
String userCID = headers.getJMSCorrelationID();
String correlationId = createCorrelationId(exchange, userCID);
Destination replyToDestination = null;
if (!exchange.isOneWay() || !jmsConfig.isEnforceSpec() && isSetReplyTo(outMessage)
&& replyTo != null) {
if (!jmsConfig.isReplyPubSubDomain()
&& (exchange.isSynchronous()
|| exchange.isOneWay())) {
replyToDestination = JMSFactory.resolveOrCreateDestination(jmsTemplate, replyTo,
jmsConfig.isReplyPubSubDomain());
} else {
if (userCID == null || !jmsConfig.isUseConduitIdSelector()) {
replyToDestination = getJMSListener().getDestination();
} else {
replyToDestination = getAllListener().getDestination();
}
}
}
final String cid = correlationId;
final Destination rtd = replyToDestination;
class JMSConduitMessageCreator implements MessageCreator {
private javax.jms.Message jmsMessage;
public javax.jms.Message createMessage(Session session) throws JMSException {
String messageType = jmsConfig.getMessageType();
Destination destination = rtd;
String replyToAddress = jmsConfig.getReplyToDestination();
if (replyToAddress != null) {
destination = JMSFactory.resolveOrCreateDestination(jmsTemplate, replyToAddress,
jmsConfig.isPubSubDomain());
}
jmsMessage = JMSUtils.buildJMSMessageFromCXFMessage(jmsConfig, outMessage, request,
messageType, session, destination,
cid);
if ((jmsConfig.isReplyPubSubDomain() || !exchange.isSynchronous()) && !exchange.isOneWay()) {
correlationMap.put(cid, exchange);
}
LOG.log(Level.FINE, "client sending request: ", jmsMessage);
return jmsMessage;
}
public String getMessageID() {
if (jmsMessage != null) {
try {
return jmsMessage.getJMSMessageID();
} catch (JMSException e) {
return null;
}
}
return null;
}
}
JMSConduitMessageCreator messageCreator = new JMSConduitMessageCreator();
/**
* If the message is not oneWay we will expect to receive a reply on the listener.
*
*/
if (!exchange.isOneWay()) {
synchronized (exchange) {
jmsTemplate.send(jmsConfig.getTargetDestination(), messageCreator);
if (correlationId == null) {
correlationId = messageCreator.getMessageID();
}
headers.setJMSMessageID(messageCreator.getMessageID());
final String messageSelector = "JMSCorrelationID = '" + correlationId + "'";
if (exchange.isSynchronous() && !jmsConfig.isReplyPubSubDomain()) {
javax.jms.Message replyMessage = jmsTemplate.receiveSelected(replyToDestination,
messageSelector);
if (replyMessage == null) {
throw new RuntimeException("Timeout receiving message with correlationId "
+ correlationId);
} else {
doReplyMessage(exchange, replyMessage);
}
// TODO How do we delete the temp queue in case of an async request
// or is async with a temp queue not possible ?
if (replyToDestination instanceof TemporaryQueue) {
try {
((TemporaryQueue)replyToDestination).delete();
} catch (JMSException e) {
// Only log the exception as the exchange should be able to proceed
LOG.log(Level.WARNING, "Unable to remove temporary queue: " + e.getMessage(), e);
}
}
}
}
} else {
jmsTemplate.send(jmsConfig.getTargetDestination(), messageCreator);
headers.setJMSMessageID(messageCreator.getMessageID());
}
}