JMSMessageHeadersType headers = getOrCreateJmsHeaders(outMessage);
String userCID = headers.getJMSCorrelationID();
assertIsNotAsyncSyncAndUserCID(exchange, userCID);
ResourceCloser closer = new ResourceCloser();
try {
if (connection == null) {
connection = JMSFactory.createConnection(jmsConfig);
}
Session session = closer.register(connection.createSession(jmsConfig.isSessionTransacted(),
Session.AUTO_ACKNOWLEDGE));
Destination targetDest = jmsConfig.getTargetDestination(session);
Destination replyToDestination = null;
if (!exchange.isOneWay()) {
if (!exchange.isSynchronous() && staticReplyDestination == null) {
staticReplyDestination = jmsConfig.getReplyDestination(session);
getJMSListener(staticReplyDestination);
}
replyToDestination = jmsConfig.getReplyToDestination(session, headers.getJMSReplyTo());
}
connection.start();
String messageType = jmsConfig.getMessageType();
String correlationId = createCorrelationId(exchange, userCID);
if (correlationId != null) {
correlationMap.put(correlationId, exchange);
}
javax.jms.Message message = JMSMessageUtils.asJMSMessage(jmsConfig,
outMessage,
request,
messageType,
session,
correlationId,
JMSConstants.JMS_CLIENT_REQUEST_HEADERS);
if (replyToDestination != null) {
message.setJMSReplyTo(replyToDestination);
}
JMSSender sender = JMSFactory.createJmsSender(jmsConfig, headers);
synchronized (exchange) {
sender.sendMessage(closer, session, targetDest, message);
LOG.log(Level.INFO, "client sending request message "
+ message.getJMSMessageID() + " to " + targetDest);
headers.setJMSMessageID(message.getJMSMessageID());
if (correlationId == null) {
// Warning: We might loose the reply if it already arrived at this point
correlationId = message.getJMSMessageID();
correlationMap.put(correlationId, exchange);
}
}
/**
* If the message is not oneWay we will expect to receive a reply on the listener.
*/
if (!exchange.isOneWay() && (exchange.isSynchronous())) {
Destination replyDestination = staticReplyDestination != null
? staticReplyDestination : replyToDestination;
javax.jms.Message replyMessage = JMSUtil.receive(session, replyDestination, correlationId,
jmsConfig.getReceiveTimeout(),
jmsConfig.isPubSubNoLocal());
correlationMap.remove(correlationId);
doReplyMessage(exchange, replyMessage);
}
} catch (JMSException e) {
throw new RuntimeException(e.getMessage(), e);
} finally {
closer.close();
}
}