Search in sources :

Example 31 with AsyncCallback

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

the class ClientChannelHandler method channelRead0.

@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
    messageReceived = true;
    if (LOG.isTraceEnabled()) {
        LOG.trace("Message received: {}", msg);
    }
    ChannelHandler handler = ctx.pipeline().get("timeout");
    if (handler != null) {
        LOG.trace("Removing timeout channel as we received message");
        ctx.pipeline().remove(handler);
    }
    Exchange exchange = getExchange(ctx);
    if (exchange == null) {
        // we just ignore the received message as the channel is closed
        return;
    }
    AsyncCallback callback = getAsyncCallback(ctx);
    Message message;
    try {
        message = getResponseMessage(exchange, ctx, msg);
    } catch (Exception e) {
        exchange.setException(e);
        callback.done(false);
        return;
    }
    // set the result on either IN or OUT on the original exchange depending on its pattern
    if (ExchangeHelper.isOutCapable(exchange)) {
        exchange.setOut(message);
    } else {
        exchange.setIn(message);
    }
    try {
        // should channel be closed after complete?
        Boolean close;
        if (ExchangeHelper.isOutCapable(exchange)) {
            close = exchange.getOut().getHeader(NettyConstants.NETTY_CLOSE_CHANNEL_WHEN_COMPLETE, Boolean.class);
        } else {
            close = exchange.getIn().getHeader(NettyConstants.NETTY_CLOSE_CHANNEL_WHEN_COMPLETE, Boolean.class);
        }
        // check the setting on the exchange property
        if (close == null) {
            close = exchange.getProperty(NettyConstants.NETTY_CLOSE_CHANNEL_WHEN_COMPLETE, Boolean.class);
        }
        // should we disconnect, the header can override the configuration
        boolean disconnect = producer.getConfiguration().isDisconnect();
        if (close != null) {
            disconnect = close;
        }
        // we should not close if we are reusing the channel
        if (!producer.getConfiguration().isReuseChannel() && disconnect) {
            if (LOG.isTraceEnabled()) {
                LOG.trace("Closing channel when complete at address: {}", producer.getConfiguration().getAddress());
            }
            NettyHelper.close(ctx.channel());
        }
    } finally {
        // signal callback
        callback.done(false);
    }
}
Also used : Exchange(org.apache.camel.Exchange) Message(org.apache.camel.Message) AsyncCallback(org.apache.camel.AsyncCallback) ChannelHandler(io.netty.channel.ChannelHandler) CamelExchangeException(org.apache.camel.CamelExchangeException)

Example 32 with AsyncCallback

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

the class ReactiveStreamsConsumer method doSend.

private boolean doSend(Exchange exchange, AsyncCallback callback) {
    ExecutorService executorService = this.executor;
    if (executorService != null && this.isRunAllowed()) {
        executorService.execute(() -> this.getAsyncProcessor().process(exchange, doneSync -> {
            if (exchange.getException() != null) {
                getExceptionHandler().handleException("Error processing exchange", exchange, exchange.getException());
            }
            callback.done(doneSync);
        }));
        return false;
    } else {
        LOG.warn("Consumer not ready to process exchanges. The exchange {} will be discarded", exchange);
        callback.done(true);
        return true;
    }
}
Also used : Processor(org.apache.camel.Processor) DefaultConsumer(org.apache.camel.impl.DefaultConsumer) CamelReactiveStreams(org.apache.camel.component.reactive.streams.api.CamelReactiveStreams) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) AsyncCallback(org.apache.camel.AsyncCallback) Exchange(org.apache.camel.Exchange) CamelReactiveStreamsService(org.apache.camel.component.reactive.streams.api.CamelReactiveStreamsService) ExecutorService(java.util.concurrent.ExecutorService) ExecutorService(java.util.concurrent.ExecutorService)

Example 33 with AsyncCallback

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

the class VertxConsumer method onEventBusEvent.

protected void onEventBusEvent(final Message event) {
    LOG.debug("onEvent {}", event);
    final boolean reply = event.replyAddress() != null;
    final Exchange exchange = endpoint.createExchange(reply ? ExchangePattern.InOut : ExchangePattern.InOnly);
    exchange.getIn().setBody(event.body());
    try {
        getAsyncProcessor().process(exchange, new AsyncCallback() {

            @Override
            public void done(boolean doneSync) {
                if (reply) {
                    Object body = getVertxBody(exchange);
                    if (body != null) {
                        LOG.debug("Sending reply to: {} with body: {}", event.replyAddress(), body);
                        event.reply(body);
                    }
                }
            }
        });
    } catch (Exception e) {
        getExceptionHandler().handleException("Error processing Vertx event: " + event, exchange, e);
    }
}
Also used : Exchange(org.apache.camel.Exchange) AsyncCallback(org.apache.camel.AsyncCallback)

Example 34 with AsyncCallback

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

the class BeanInfoTest method testFindsSingleMethodMatchingBody.

public void testFindsSingleMethodMatchingBody() throws Throwable {
    MethodInvocation invocation = info.createInvocation(bean, exchange);
    assertNotNull("Should have found a method invocation!", invocation);
    AtomicBoolean sync = new AtomicBoolean(true);
    invocation.proceed(new AsyncCallback() {

        public void done(boolean doneSync) {
        // nnop
        }
    });
    assertEquals(true, sync.get());
    assertEquals("Hello James!", exchange.getIn().getBody());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AsyncCallback(org.apache.camel.AsyncCallback) MethodInvocation(org.apache.camel.component.bean.MethodInvocation)

Example 35 with AsyncCallback

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

the class StompEndpoint method send.

protected void send(final Exchange exchange, final AsyncCallback callback) {
    final StompFrame frame = new StompFrame(SEND);
    frame.addHeader(DESTINATION, StompFrame.encodeHeader(destination));
    //Fix for CAMEL-9506 leveraging the camel converter to do the change
    frame.content(utf8(exchange.getIn().getBody(String.class)));
    connection.getDispatchQueue().execute(new Task() {

        @Override
        public void run() {
            connection.send(frame, new Callback<Void>() {

                @Override
                public void onFailure(Throwable e) {
                    exchange.setException(e);
                    callback.done(false);
                }

                @Override
                public void onSuccess(Void v) {
                    callback.done(false);
                }
            });
        }
    });
}
Also used : Task(org.fusesource.hawtdispatch.Task) Callback(org.fusesource.stomp.client.Callback) AsyncCallback(org.apache.camel.AsyncCallback) StompFrame(org.fusesource.stomp.codec.StompFrame)

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