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);
}
}
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;
}
}
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);
}
}
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());
}
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);
}
});
}
});
}
Aggregations