Example usage for java.util.concurrent Exchanger Exchanger

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

Introduction

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

Prototype

public Exchanger() 

Source Link

Document

Creates a new Exchanger.

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
 *///from   w  ww.  jav  a 2  s  . c om
@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());
}

From source file:org.apache.hadoop.hdfs.server.datanode.TestDirectoryScannerDelta.java

private void setUp(boolean inlineChecksums) {
    LOG.info("setting up!");
    exchanger = new Exchanger<InjectionEventI>();
    InjectionHandler.clear();/*from  w ww  . j  av a  2s. co  m*/
    Configuration CONF = new Configuration();
    CONF.setLong("dfs.block.size", 100);
    CONF.setInt("io.bytes.per.checksum", 1);
    CONF.setLong("dfs.heartbeat.interval", 1L);
    CONF.setInt("dfs.datanode.directoryscan.interval", 1);
    CONF.setBoolean("dfs.use.inline.checksum", inlineChecksums);

    try {
        cluster = new MiniDFSCluster(CONF, 1, true, null);
        cluster.waitActive();

        dn = cluster.getDataNodes().get(0);
        nsid = dn.getAllNamespaces()[0];
        scanner = dn.directoryScanner;
        data = (FSDataset) dn.data;
        Field f = DirectoryScanner.class.getDeclaredField("delta");
        f.setAccessible(true);
        delta = (FSDatasetDelta) f.get(scanner);

        fs = cluster.getFileSystem();
    } catch (Exception e) {
        e.printStackTrace();
        fail("setup failed");
    }
}