Search in sources :

Example 6 with AsyncProcessor

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

the class RoutingSlip method createErrorHandler.

protected AsyncProcessor createErrorHandler(RouteContext routeContext, Exchange exchange, AsyncProcessor processor, Endpoint endpoint) {
    AsyncProcessor answer = processor;
    boolean tryBlock = exchange.getProperty(Exchange.TRY_ROUTE_BLOCK, false, boolean.class);
    // do not wrap in error handler if we are inside a try block
    if (!tryBlock && routeContext != null) {
        // wrap the producer in error handler so we have fine grained error handling on
        // the output side instead of the input side
        // this is needed to support redelivery on that output alone and not doing redelivery
        // for the entire routingslip/dynamic-router block again which will start from scratch again
        log.trace("Creating error handler for: {}", processor);
        ErrorHandlerFactory builder = routeContext.getRoute().getErrorHandlerBuilder();
        // instead of using ProcessorDefinition.wrapInErrorHandler)
        try {
            answer = (AsyncProcessor) builder.createErrorHandler(routeContext, processor);
            // must start the error handler
            ServiceHelper.startServices(answer);
        } catch (Exception e) {
            throw ObjectHelper.wrapRuntimeCamelException(e);
        }
    }
    return answer;
}
Also used : AsyncProcessor(org.apache.camel.AsyncProcessor) ErrorHandlerFactory(org.apache.camel.ErrorHandlerFactory) FailedToCreateProducerException(org.apache.camel.FailedToCreateProducerException)

Example 7 with AsyncProcessor

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

the class SendDynamicProcessor method process.

public boolean process(Exchange exchange, final AsyncCallback callback) {
    if (!isStarted()) {
        exchange.setException(new IllegalStateException("SendProcessor has not been started: " + this));
        callback.done(true);
        return true;
    }
    // we should preserve existing MEP so remember old MEP
    // if you want to permanently to change the MEP then use .setExchangePattern in the DSL
    final ExchangePattern existingPattern = exchange.getPattern();
    // which endpoint to send to
    final Endpoint endpoint;
    final ExchangePattern destinationExchangePattern;
    // use dynamic endpoint so calculate the endpoint to use
    Object recipient = null;
    try {
        recipient = expression.evaluate(exchange, Object.class);
        endpoint = resolveEndpoint(exchange, recipient);
        destinationExchangePattern = EndpointHelper.resolveExchangePatternFromUrl(endpoint.getEndpointUri());
    } catch (Throwable e) {
        if (isIgnoreInvalidEndpoint()) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Endpoint uri is invalid: " + recipient + ". This exception will be ignored.", e);
            }
        } else {
            exchange.setException(e);
        }
        callback.done(true);
        return true;
    }
    // send the exchange to the destination using the producer cache
    return producerCache.doInAsyncProducer(endpoint, exchange, pattern, callback, new AsyncProducerCallback() {

        public boolean doInAsyncProducer(Producer producer, AsyncProcessor asyncProducer, final Exchange exchange, ExchangePattern pattern, final AsyncCallback callback) {
            final Exchange target = configureExchange(exchange, pattern, destinationExchangePattern, endpoint);
            LOG.debug(">>>> {} {}", endpoint, exchange);
            return asyncProducer.process(target, new AsyncCallback() {

                public void done(boolean doneSync) {
                    // restore previous MEP
                    target.setPattern(existingPattern);
                    // signal we are done
                    callback.done(doneSync);
                }
            });
        }
    });
}
Also used : Exchange(org.apache.camel.Exchange) AsyncProducerCallback(org.apache.camel.AsyncProducerCallback) Endpoint(org.apache.camel.Endpoint) Producer(org.apache.camel.Producer) ExchangePattern(org.apache.camel.ExchangePattern) AsyncProcessor(org.apache.camel.AsyncProcessor) AsyncCallback(org.apache.camel.AsyncCallback)

Example 8 with AsyncProcessor

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

the class DefaultChannel method initChannel.

@SuppressWarnings("deprecation")
public void initChannel(ProcessorDefinition<?> outputDefinition, RouteContext routeContext) throws Exception {
    this.routeContext = routeContext;
    this.definition = outputDefinition;
    this.camelContext = routeContext.getCamelContext();
    Processor target = nextProcessor;
    Processor next;
    // init CamelContextAware as early as possible on target
    if (target instanceof CamelContextAware) {
        ((CamelContextAware) target).setCamelContext(camelContext);
    }
    // the definition to wrap should be the fine grained,
    // so if a child is set then use it, if not then its the original output used
    ProcessorDefinition<?> targetOutputDef = childDefinition != null ? childDefinition : outputDefinition;
    LOG.debug("Initialize channel for target: '{}'", targetOutputDef);
    // ideally we need the design time route -> runtime route to be a 2-phase pass (scheduled work for Camel 3.0)
    if (childDefinition != null && outputDefinition != childDefinition) {
        childDefinition.setParent(outputDefinition);
    }
    // force the creation of an id
    RouteDefinitionHelper.forceAssignIds(routeContext.getCamelContext(), definition);
    // first wrap the output with the managed strategy if any
    InterceptStrategy managed = routeContext.getManagedInterceptStrategy();
    if (managed != null) {
        next = target == nextProcessor ? null : nextProcessor;
        target = managed.wrapProcessorInInterceptors(routeContext.getCamelContext(), targetOutputDef, target, next);
    }
    // then wrap the output with the backlog and tracer (backlog first, as we do not want regular tracer to tracer the backlog)
    InterceptStrategy tracer = getOrCreateBacklogTracer();
    camelContext.addService(tracer);
    if (tracer instanceof BacklogTracer) {
        BacklogTracer backlogTracer = (BacklogTracer) tracer;
        RouteDefinition route = ProcessorDefinitionHelper.getRoute(definition);
        boolean first = false;
        if (route != null && !route.getOutputs().isEmpty()) {
            first = route.getOutputs().get(0) == definition;
        }
        addAdvice(new BacklogTracerAdvice(backlogTracer, targetOutputDef, route, first));
        // add debugger as well so we have both tracing and debugging out of the box
        InterceptStrategy debugger = getOrCreateBacklogDebugger();
        camelContext.addService(debugger);
        if (debugger instanceof BacklogDebugger) {
            BacklogDebugger backlogDebugger = (BacklogDebugger) debugger;
            addAdvice(new BacklogDebuggerAdvice(backlogDebugger, target, targetOutputDef));
        }
    }
    if (routeContext.isMessageHistory()) {
        // add message history advice
        MessageHistoryFactory factory = camelContext.getMessageHistoryFactory();
        addAdvice(new MessageHistoryAdvice(factory, targetOutputDef));
    }
    // the regular tracer is not a task on internalProcessor as this is not really needed
    // end users have to explicit enable the tracer to use it, and then its okay if we wrap
    // the processors (but by default tracer is disabled, and therefore we do not wrap processors)
    tracer = getOrCreateTracer();
    camelContext.addService(tracer);
    if (tracer != null) {
        TraceInterceptor trace = (TraceInterceptor) tracer.wrapProcessorInInterceptors(routeContext.getCamelContext(), targetOutputDef, target, null);
        // trace interceptor need to have a reference to route context so we at runtime can enable/disable tracing on-the-fly
        trace.setRouteContext(routeContext);
        target = trace;
    }
    // sort interceptors according to ordered
    interceptors.sort(new OrderedComparator());
    // then reverse list so the first will be wrapped last, as it would then be first being invoked
    Collections.reverse(interceptors);
    // wrap the output with the configured interceptors
    for (InterceptStrategy strategy : interceptors) {
        next = target == nextProcessor ? null : nextProcessor;
        // skip tracer as we did the specially beforehand and it could potentially be added as an interceptor strategy
        if (strategy instanceof Tracer) {
            continue;
        }
        // skip stream caching as it must be wrapped as outer most, which we do later
        if (strategy instanceof StreamCaching) {
            continue;
        }
        // use the fine grained definition (eg the child if available). Its always possible to get back to the parent
        Processor wrapped = strategy.wrapProcessorInInterceptors(routeContext.getCamelContext(), targetOutputDef, target, next);
        if (!(wrapped instanceof AsyncProcessor)) {
            LOG.warn("Interceptor: " + strategy + " at: " + outputDefinition + " does not return an AsyncProcessor instance." + " This causes the asynchronous routing engine to not work as optimal as possible." + " See more details at the InterceptStrategy javadoc." + " Camel will use a bridge to adapt the interceptor to the asynchronous routing engine," + " but its not the most optimal solution. Please consider changing your interceptor to comply.");
            // use a bridge and wrap again which allows us to adapt and leverage the asynchronous routing engine anyway
            // however its not the most optimal solution, but we can still run.
            InterceptorToAsyncProcessorBridge bridge = new InterceptorToAsyncProcessorBridge(target);
            wrapped = strategy.wrapProcessorInInterceptors(routeContext.getCamelContext(), targetOutputDef, bridge, next);
            // Avoid the stack overflow
            if (!wrapped.equals(bridge)) {
                bridge.setTarget(wrapped);
            } else {
                // Just skip the wrapped processor
                bridge.setTarget(null);
            }
            wrapped = bridge;
        }
        if (!(wrapped instanceof WrapProcessor)) {
            // wrap the target so it becomes a service and we can manage its lifecycle
            wrapped = new WrapProcessor(wrapped, target);
        }
        target = wrapped;
    }
    if (routeContext.isStreamCaching()) {
        addAdvice(new StreamCachingAdvice(camelContext.getStreamCachingStrategy()));
    }
    if (routeContext.getDelayer() != null && routeContext.getDelayer() > 0) {
        addAdvice(new DelayerAdvice(routeContext.getDelayer()));
    }
    // sets the delegate to our wrapped output
    output = target;
}
Also used : MessageHistoryFactory(org.apache.camel.spi.MessageHistoryFactory) CamelInternalProcessor(org.apache.camel.processor.CamelInternalProcessor) Processor(org.apache.camel.Processor) WrapProcessor(org.apache.camel.processor.WrapProcessor) AsyncProcessor(org.apache.camel.AsyncProcessor) CamelContextAware(org.apache.camel.CamelContextAware) InterceptorToAsyncProcessorBridge(org.apache.camel.processor.InterceptorToAsyncProcessorBridge) InterceptStrategy(org.apache.camel.spi.InterceptStrategy) RouteDefinition(org.apache.camel.model.RouteDefinition) WrapProcessor(org.apache.camel.processor.WrapProcessor) AsyncProcessor(org.apache.camel.AsyncProcessor) OrderedComparator(org.apache.camel.util.OrderedComparator)

Example 9 with AsyncProcessor

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

the class FailOverLoadBalancer method processExchange.

private boolean processExchange(Processor processor, Exchange exchange, Exchange copy, AtomicInteger attempts, AtomicInteger index, AsyncCallback callback, List<Processor> processors) {
    if (processor == null) {
        throw new IllegalStateException("No processors could be chosen to process " + copy);
    }
    log.debug("Processing failover at attempt {} for {}", attempts, copy);
    AsyncProcessor albp = AsyncProcessorConverterHelper.convert(processor);
    return albp.process(copy, new FailOverAsyncCallback(exchange, copy, attempts, index, callback, processors));
}
Also used : AsyncProcessor(org.apache.camel.AsyncProcessor)

Example 10 with AsyncProcessor

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

the class RouteboxDirectProducer method process.

public boolean process(Exchange exchange, final AsyncCallback callback) {
    boolean flag = true;
    if ((((RouteboxDirectEndpoint) getRouteboxEndpoint()).getConsumer() == null) && ((getRouteboxEndpoint()).getConfig().isSendToConsumer())) {
        exchange.setException(new CamelExchangeException("No consumers available on endpoint: " + getRouteboxEndpoint(), exchange));
        callback.done(true);
        flag = true;
    } else {
        try {
            LOG.debug("Dispatching to Inner Route {}", exchange);
            RouteboxDispatcher dispatcher = new RouteboxDispatcher(producer);
            exchange = dispatcher.dispatchAsync(getRouteboxEndpoint(), exchange);
            if (getRouteboxEndpoint().getConfig().isSendToConsumer()) {
                AsyncProcessor processor = ((RouteboxDirectEndpoint) getRouteboxEndpoint()).getConsumer().getAsyncProcessor();
                flag = processor.process(exchange, callback);
            }
        } catch (Exception e) {
            getExceptionHandler().handleException("Error processing exchange", exchange, e);
        }
    }
    return flag;
}
Also used : CamelExchangeException(org.apache.camel.CamelExchangeException) AsyncProcessor(org.apache.camel.AsyncProcessor) RouteboxDispatcher(org.apache.camel.component.routebox.strategy.RouteboxDispatcher) CamelExchangeException(org.apache.camel.CamelExchangeException)

Aggregations

AsyncProcessor (org.apache.camel.AsyncProcessor)23 AsyncCallback (org.apache.camel.AsyncCallback)11 Exchange (org.apache.camel.Exchange)11 Processor (org.apache.camel.Processor)10 Producer (org.apache.camel.Producer)7 Endpoint (org.apache.camel.Endpoint)5 StopWatch (org.apache.camel.util.StopWatch)5 ExchangePattern (org.apache.camel.ExchangePattern)4 FailedToCreateProducerException (org.apache.camel.FailedToCreateProducerException)4 AsyncProducerCallback (org.apache.camel.AsyncProducerCallback)3 CamelExchangeException (org.apache.camel.CamelExchangeException)3 DefaultExchange (org.apache.camel.impl.DefaultExchange)2 TracedRouteNodes (org.apache.camel.spi.TracedRouteNodes)2 AtomicExchange (org.apache.camel.util.concurrent.AtomicExchange)2 ByteBuf (io.netty.buffer.ByteBuf)1 InetSocketAddress (java.net.InetSocketAddress)1 ByteBuffer (java.nio.ByteBuffer)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1 CamelContextAware (org.apache.camel.CamelContextAware)1 ErrorHandlerFactory (org.apache.camel.ErrorHandlerFactory)1