Example usage for java.util.concurrent Exchanger exchange

List of usage examples for java.util.concurrent Exchanger exchange

Introduction

In this page you can find the example usage for java.util.concurrent Exchanger exchange.

Prototype

@SuppressWarnings("unchecked")
public V exchange(V x, long timeout, TimeUnit unit) throws InterruptedException, TimeoutException 

Source Link

Document

Waits for another thread to arrive at this exchange point (unless the current thread is Thread#interrupt interrupted or the specified waiting time elapses), and then transfers the given object to it, receiving its object in return.

Usage

From source file:org.apache.camel.component.sjms.producer.InOutProducer.java

/**
 * 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
 *///w ww . j ava 2  s. com
@Override
public void sendMessage(final Exchange exchange, final AsyncCallback callback,
        final MessageProducerResources producer) throws Exception {
    if (isEndpointTransacted()) {
        exchange.getUnitOfWork().addSynchronization(
                new SessionTransactionSynchronization(producer.getSession(), getCommitStrategy()));
    }

    Message request = JmsMessageHelper.createMessage(exchange, producer.getSession(), getEndpoint());

    // TODO just set the correlation id don't get it from the
    // message
    String correlationId;
    if (exchange.getIn().getHeader(JmsConstants.JMS_CORRELATION_ID, String.class) == null) {
        correlationId = UUID.randomUUID().toString().replace("-", "");
    } else {
        correlationId = exchange.getIn().getHeader(JmsConstants.JMS_CORRELATION_ID, String.class);
    }
    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);

    // Return the producer to the pool so another waiting producer
    // can move forward
    // without waiting on us to complete the exchange
    try {
        getProducers().returnObject(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 response = (Message) responseObject;
            JmsMessageHelper.populateExchange(response, exchange, true,
                    getEndpoint().getJmsKeyFormatStrategy());
        } else {
            exchange.setException(new CamelException("Unknown response type: " + responseObject));
        }
    }

    callback.done(isSynchronous());
}