Search in sources :

Example 1 with MessageConsumerResources

use of org.apache.camel.component.sjms.MessageConsumerResources in project camel by apache.

the class InOutProducer method sendMessage.

/**
     * TODO time out is actually double as it waits for the producer and then
     * waits for the response. Use an atomic long to manage the countdown
     */
@Override
public void sendMessage(final Exchange exchange, final AsyncCallback callback, final MessageProducerResources producer, final ReleaseProducerCallback releaseProducerCallback) throws Exception {
    Message request = getEndpoint().getBinding().makeJmsMessage(exchange, producer.getSession());
    String correlationId = exchange.getIn().getHeader(JmsConstants.JMS_CORRELATION_ID, String.class);
    if (correlationId == null) {
        // we append the 'Camel-' prefix to know it was generated by us
        correlationId = GENERATED_CORRELATION_ID_PREFIX + getUuidGenerator().generateUuid();
    }
    Object responseObject = null;
    Exchanger<Object> messageExchanger = new Exchanger<Object>();
    JmsMessageHelper.setCorrelationId(request, correlationId);
    EXCHANGERS.put(correlationId, messageExchanger);
    MessageConsumerResources consumer = consumers.borrowObject();
    JmsMessageHelper.setJMSReplyTo(request, consumer.getReplyToDestination());
    consumers.returnObject(consumer);
    producer.getMessageProducer().send(request);
    // without waiting on us to complete the exchange
    try {
        releaseProducerCallback.release(producer);
    } catch (Exception exception) {
    // thrown if the pool is full. safe to ignore.
    }
    try {
        responseObject = messageExchanger.exchange(null, getResponseTimeOut(), TimeUnit.MILLISECONDS);
        EXCHANGERS.remove(correlationId);
    } catch (InterruptedException e) {
        log.debug("Exchanger was interrupted while waiting on response", e);
        exchange.setException(e);
    } catch (TimeoutException e) {
        log.debug("Exchanger timed out while waiting on response", e);
        exchange.setException(e);
    }
    if (exchange.getException() == null) {
        if (responseObject instanceof Throwable) {
            exchange.setException((Throwable) responseObject);
        } else if (responseObject instanceof Message) {
            Message message = (Message) responseObject;
            SjmsMessage response = new SjmsMessage(message, consumer.getSession(), getEndpoint().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);
        } else {
            exchange.setException(new CamelException("Unknown response type: " + responseObject));
        }
    }
    callback.done(isSynchronous());
}
Also used : Message(javax.jms.Message) SjmsMessage(org.apache.camel.component.sjms.SjmsMessage) Exchanger(java.util.concurrent.Exchanger) CamelException(org.apache.camel.CamelException) SjmsMessage(org.apache.camel.component.sjms.SjmsMessage) MessageConsumerResources(org.apache.camel.component.sjms.MessageConsumerResources) TimeoutException(java.util.concurrent.TimeoutException) CamelException(org.apache.camel.CamelException) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

Exchanger (java.util.concurrent.Exchanger)1 TimeoutException (java.util.concurrent.TimeoutException)1 Message (javax.jms.Message)1 CamelException (org.apache.camel.CamelException)1 MessageConsumerResources (org.apache.camel.component.sjms.MessageConsumerResources)1 SjmsMessage (org.apache.camel.component.sjms.SjmsMessage)1