Search in sources :

Example 11 with AsyncCallback

use of org.apache.camel.AsyncCallback in project camel by apache.

the class AsyncProcessorHelper method process.

/**
     * Calls the async version of the processor's process method.
     * <p/>
     * This implementation supports transacted {@link Exchange}s which ensure those are run in a synchronous fashion.
     * See more details at {@link org.apache.camel.AsyncProcessor}.
     *
     * @param processor the processor
     * @param exchange  the exchange
     * @param callback  the callback
     * @return <tt>true</tt> to continue execute synchronously, <tt>false</tt> to continue being executed asynchronously
     * @deprecated should no longer be needed, instead invoke the process method on the {@link AsyncProcessor} directly,
     * instead of using this method.
     */
@Deprecated
public static boolean process(final AsyncProcessor processor, final Exchange exchange, final AsyncCallback callback) {
    boolean sync;
    if (exchange.isTransacted()) {
        // must be synchronized for transacted exchanges
        LOG.trace("Transacted Exchange must be routed synchronously for exchangeId: {} -> {}", exchange.getExchangeId(), exchange);
        try {
            process(processor, exchange);
        } catch (Throwable e) {
            exchange.setException(e);
        }
        callback.done(true);
        sync = true;
    } else {
        final UnitOfWork uow = exchange.getUnitOfWork();
        // allow unit of work to wrap callback in case it need to do some special work
        // for example the MDCUnitOfWork
        AsyncCallback async = callback;
        if (uow != null) {
            async = uow.beforeProcess(processor, exchange, callback);
        }
        // we support asynchronous routing so invoke it
        sync = processor.process(exchange, async);
        // execute any after processor work (in current thread, not in the callback)
        if (uow != null) {
            uow.afterProcess(processor, exchange, callback, sync);
        }
    }
    if (LOG.isTraceEnabled()) {
        LOG.trace("Exchange processed and is continued routed {} for exchangeId: {} -> {}", new Object[] { sync ? "synchronously" : "asynchronously", exchange.getExchangeId(), exchange });
    }
    return sync;
}
Also used : UnitOfWork(org.apache.camel.spi.UnitOfWork) AsyncCallback(org.apache.camel.AsyncCallback)

Example 12 with AsyncCallback

use of org.apache.camel.AsyncCallback in project camel by apache.

the class WebsocketConsumer method sendMessage.

public void sendMessage(final String connectionKey, Object message) {
    final Exchange exchange = getEndpoint().createExchange();
    // set header and body
    exchange.getIn().setHeader(WebsocketConstants.CONNECTION_KEY, connectionKey);
    exchange.getIn().setBody(message);
    // send exchange using the async routing engine
    getAsyncProcessor().process(exchange, new AsyncCallback() {

        public void done(boolean doneSync) {
            if (exchange.getException() != null) {
                getExceptionHandler().handleException("Error processing exchange", exchange, exchange.getException());
            }
        }
    });
}
Also used : Exchange(org.apache.camel.Exchange) AsyncCallback(org.apache.camel.AsyncCallback)

Example 13 with AsyncCallback

use of org.apache.camel.AsyncCallback in project camel by apache.

the class SchedulerConsumer method sendTimerExchange.

protected int sendTimerExchange() {
    final Exchange exchange = getEndpoint().createExchange();
    exchange.setProperty(Exchange.TIMER_NAME, getEndpoint().getName());
    Date now = new Date();
    exchange.setProperty(Exchange.TIMER_FIRED_TIME, now);
    if (log.isTraceEnabled()) {
        log.trace("Timer {} is firing", getEndpoint().getName());
    }
    if (!getEndpoint().isSynchronous()) {
        getAsyncProcessor().process(exchange, new AsyncCallback() {

            @Override
            public void done(boolean doneSync) {
                // handle any thrown exception
                if (exchange.getException() != null) {
                    getExceptionHandler().handleException("Error processing exchange", exchange, exchange.getException());
                }
            }
        });
    } else {
        try {
            getProcessor().process(exchange);
        } catch (Exception e) {
            exchange.setException(e);
        }
        // handle any thrown exception
        if (exchange.getException() != null) {
            getExceptionHandler().handleException("Error processing exchange", exchange, exchange.getException());
        }
    }
    // a property can be used to control if the scheduler polled a message or not
    // for example to overrule and indicate no message was polled, which can affect the scheduler
    // to leverage backoff on idle etc.
    boolean polled = exchange.getProperty(Exchange.SCHEDULER_POLLED_MESSAGES, true, boolean.class);
    return polled ? 1 : 0;
}
Also used : Exchange(org.apache.camel.Exchange) AsyncCallback(org.apache.camel.AsyncCallback) Date(java.util.Date)

Example 14 with AsyncCallback

use of org.apache.camel.AsyncCallback in project camel by apache.

the class TimerConsumer method sendTimerExchange.

protected void sendTimerExchange(long counter) {
    final Exchange exchange = endpoint.createExchange();
    exchange.setProperty(Exchange.TIMER_COUNTER, counter);
    exchange.setProperty(Exchange.TIMER_NAME, endpoint.getTimerName());
    exchange.setProperty(Exchange.TIMER_TIME, endpoint.getTime());
    exchange.setProperty(Exchange.TIMER_PERIOD, endpoint.getPeriod());
    Date now = new Date();
    exchange.setProperty(Exchange.TIMER_FIRED_TIME, now);
    // also set now on in header with same key as quartz to be consistent
    exchange.getIn().setHeader("firedTime", now);
    if (LOG.isTraceEnabled()) {
        LOG.trace("Timer {} is firing #{} count", endpoint.getTimerName(), counter);
    }
    if (!endpoint.isSynchronous()) {
        getAsyncProcessor().process(exchange, new AsyncCallback() {

            @Override
            public void done(boolean doneSync) {
                // handle any thrown exception
                if (exchange.getException() != null) {
                    getExceptionHandler().handleException("Error processing exchange", exchange, exchange.getException());
                }
            }
        });
    } else {
        try {
            getProcessor().process(exchange);
        } catch (Exception e) {
            exchange.setException(e);
        }
        // handle any thrown exception
        if (exchange.getException() != null) {
            getExceptionHandler().handleException("Error processing exchange", exchange, exchange.getException());
        }
    }
}
Also used : Exchange(org.apache.camel.Exchange) AsyncCallback(org.apache.camel.AsyncCallback) Date(java.util.Date)

Example 15 with AsyncCallback

use of org.apache.camel.AsyncCallback in project camel by apache.

the class WsConsumer method sendMessageInternal.

private void sendMessageInternal(Object message) {
    final Exchange exchange = getEndpoint().createExchange();
    if (message instanceof Throwable) {
        exchange.setException((Throwable) message);
    } else {
        exchange.getIn().setBody(message);
    }
    // send exchange using the async routing engine
    getAsyncProcessor().process(exchange, new AsyncCallback() {

        public void done(boolean doneSync) {
            if (exchange.getException() != null) {
                getExceptionHandler().handleException("Error processing exchange", exchange, exchange.getException());
            }
        }
    });
}
Also used : Exchange(org.apache.camel.Exchange) AsyncCallback(org.apache.camel.AsyncCallback)

Aggregations

AsyncCallback (org.apache.camel.AsyncCallback)67 Exchange (org.apache.camel.Exchange)47 AsyncProcessor (org.apache.camel.AsyncProcessor)12 CamelExchangeException (org.apache.camel.CamelExchangeException)8 Message (org.apache.camel.Message)5 Processor (org.apache.camel.Processor)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)4 Endpoint (org.apache.camel.Endpoint)4 Producer (org.apache.camel.Producer)4 Synchronization (org.apache.camel.spi.Synchronization)4 StopWatch (org.apache.camel.util.StopWatch)4 InetSocketAddress (java.net.InetSocketAddress)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 ExchangePattern (org.apache.camel.ExchangePattern)3 ChannelHandler (io.netty.channel.ChannelHandler)2 IOException (java.io.IOException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ConnectException (java.net.ConnectException)2