Search in sources :

Example 1 with DefaultRouteNode

use of org.apache.camel.impl.DefaultRouteNode in project camel by apache.

the class TraceInterceptor method traceDoFinally.

private void traceDoFinally(TracedRouteNodes traced, Exchange exchange) throws Exception {
    if (traced.getLastNode() != null) {
        traced.addTraced(new DefaultRouteNode(traced.getLastNode().getProcessorDefinition(), traced.getLastNode().getProcessor()));
    }
    traced.addTraced(new DoFinallyRouteNode());
    // log and trace so we have the from -> doFinally event as well
    logExchange(exchange);
    traceExchange(exchange);
    traced.addTraced(new DefaultRouteNode(node, super.getProcessor()));
}
Also used : DoFinallyRouteNode(org.apache.camel.impl.DoFinallyRouteNode) DefaultRouteNode(org.apache.camel.impl.DefaultRouteNode)

Example 2 with DefaultRouteNode

use of org.apache.camel.impl.DefaultRouteNode in project camel by apache.

the class TraceInterceptor method traceDoCatch.

private void traceDoCatch(TracedRouteNodes traced, Exchange exchange) throws Exception {
    if (traced.getLastNode() != null) {
        traced.addTraced(new DefaultRouteNode(traced.getLastNode().getProcessorDefinition(), traced.getLastNode().getProcessor()));
    }
    traced.addTraced(new DoCatchRouteNode());
    // log and trace so we have the from -> doCatch event as well
    logExchange(exchange);
    traceExchange(exchange);
    traced.addTraced(new DefaultRouteNode(node, super.getProcessor()));
}
Also used : DoCatchRouteNode(org.apache.camel.impl.DoCatchRouteNode) DefaultRouteNode(org.apache.camel.impl.DefaultRouteNode)

Example 3 with DefaultRouteNode

use of org.apache.camel.impl.DefaultRouteNode in project camel by apache.

the class TraceInterceptor method process.

@Override
public boolean process(final Exchange exchange, final AsyncCallback callback) {
    // do not trace if tracing is disabled
    if (!tracer.isEnabled() || (routeContext != null && !routeContext.isTracing())) {
        return processor.process(exchange, callback);
    }
    // logging TraceEvents to avoid infinite looping
    if (exchange.getProperty(Exchange.TRACE_EVENT, false, Boolean.class)) {
        // but we must still process to allow routing of TraceEvents to eg a JPA endpoint
        return processor.process(exchange, callback);
    }
    final boolean shouldLog = shouldLogNode(node) && shouldLogExchange(exchange);
    // whether we should trace it or not, some nodes should be skipped as they are abstract
    // intermediate steps for instance related to on completion
    boolean trace = true;
    boolean sync = true;
    // okay this is a regular exchange being routed we might need to log and trace
    try {
        // before
        if (shouldLog) {
            // traced holds the information about the current traced route path
            if (exchange.getUnitOfWork() != null) {
                TracedRouteNodes traced = exchange.getUnitOfWork().getTracedRouteNodes();
                if (node instanceof OnCompletionDefinition || node instanceof OnExceptionDefinition) {
                    // skip any of these as its just a marker definition
                    trace = false;
                } else if (ProcessorDefinitionHelper.isFirstChildOfType(OnCompletionDefinition.class, node)) {
                    // special for on completion tracing
                    traceOnCompletion(traced, exchange);
                } else if (ProcessorDefinitionHelper.isFirstChildOfType(OnExceptionDefinition.class, node)) {
                    // special for on exception
                    traceOnException(traced, exchange);
                } else if (ProcessorDefinitionHelper.isFirstChildOfType(CatchDefinition.class, node)) {
                    // special for do catch
                    traceDoCatch(traced, exchange);
                } else if (ProcessorDefinitionHelper.isFirstChildOfType(FinallyDefinition.class, node)) {
                    // special for do finally
                    traceDoFinally(traced, exchange);
                } else if (ProcessorDefinitionHelper.isFirstChildOfType(AggregateDefinition.class, node)) {
                    // special for aggregate
                    traceAggregate(traced, exchange);
                } else {
                    // regular so just add it
                    traced.addTraced(new DefaultRouteNode(node, super.getProcessor()));
                }
            } else {
                LOG.trace("Cannot trace as this Exchange does not have an UnitOfWork: {}", exchange);
            }
        }
        // log and trace the processor
        Object state = null;
        if (shouldLog && trace) {
            logExchange(exchange);
            // either call the in or generic trace method depending on OUT has been enabled or not
            if (tracer.isTraceOutExchanges()) {
                state = traceExchangeIn(exchange);
            } else {
                traceExchange(exchange);
            }
        }
        final Object traceState = state;
        // special for interceptor where we need to keep booking how far we have routed in the intercepted processors
        if (node.getParent() instanceof InterceptDefinition && exchange.getUnitOfWork() != null) {
            TracedRouteNodes traced = exchange.getUnitOfWork().getTracedRouteNodes();
            traceIntercept((InterceptDefinition) node.getParent(), traced, exchange);
        }
        // process the exchange
        sync = processor.process(exchange, new AsyncCallback() {

            @Override
            public void done(boolean doneSync) {
                try {
                    // after (trace out)
                    if (shouldLog && tracer.isTraceOutExchanges()) {
                        logExchange(exchange);
                        traceExchangeOut(exchange, traceState);
                    }
                } catch (Throwable e) {
                    // some exception occurred in trace logic
                    if (shouldLogException(exchange)) {
                        logException(exchange, e);
                    }
                    exchange.setException(e);
                } finally {
                    // ensure callback is always invoked
                    callback.done(doneSync);
                }
            }
        });
    } catch (Throwable e) {
        // some exception occurred in trace logic
        if (shouldLogException(exchange)) {
            logException(exchange, e);
        }
        exchange.setException(e);
    }
    return sync;
}
Also used : CatchDefinition(org.apache.camel.model.CatchDefinition) AsyncCallback(org.apache.camel.AsyncCallback) OnCompletionDefinition(org.apache.camel.model.OnCompletionDefinition) OnExceptionDefinition(org.apache.camel.model.OnExceptionDefinition) AggregateDefinition(org.apache.camel.model.AggregateDefinition) TracedRouteNodes(org.apache.camel.spi.TracedRouteNodes) DefaultRouteNode(org.apache.camel.impl.DefaultRouteNode) InterceptDefinition(org.apache.camel.model.InterceptDefinition)

Example 4 with DefaultRouteNode

use of org.apache.camel.impl.DefaultRouteNode in project camel by apache.

the class TraceInterceptor method traceOnCompletion.

private void traceOnCompletion(TracedRouteNodes traced, Exchange exchange) {
    traced.addTraced(new OnCompletionRouteNode());
    // do not log and trace as onCompletion should be a new event on its own
    // add the next step as well so we have onCompletion -> new step
    traced.addTraced(new DefaultRouteNode(node, super.getProcessor()));
}
Also used : OnCompletionRouteNode(org.apache.camel.impl.OnCompletionRouteNode) DefaultRouteNode(org.apache.camel.impl.DefaultRouteNode)

Example 5 with DefaultRouteNode

use of org.apache.camel.impl.DefaultRouteNode in project camel by apache.

the class TraceInterceptor method traceIntercept.

protected void traceIntercept(InterceptDefinition intercept, TracedRouteNodes traced, Exchange exchange) throws Exception {
    // use the counter to get the index of the intercepted processor to be traced
    Processor last = intercept.getInterceptedProcessor(traced.getAndIncrementCounter(intercept));
    // skip doing any double tracing of interceptors, so the last must not be a TraceInterceptor instance
    if (last != null && !(last instanceof TraceInterceptor)) {
        traced.addTraced(new DefaultRouteNode(node, last));
        boolean shouldLog = shouldLogNode(node) && shouldLogExchange(exchange);
        if (shouldLog) {
            // log and trace the processor that was intercepted so we can see it
            logExchange(exchange);
            traceExchange(exchange);
        }
    }
}
Also used : DelegateAsyncProcessor(org.apache.camel.processor.DelegateAsyncProcessor) CamelLogProcessor(org.apache.camel.processor.CamelLogProcessor) Processor(org.apache.camel.Processor) DefaultRouteNode(org.apache.camel.impl.DefaultRouteNode)

Aggregations

DefaultRouteNode (org.apache.camel.impl.DefaultRouteNode)7 AggregateDefinition (org.apache.camel.model.AggregateDefinition)2 AsyncCallback (org.apache.camel.AsyncCallback)1 Processor (org.apache.camel.Processor)1 AggregateRouteNode (org.apache.camel.impl.AggregateRouteNode)1 DoCatchRouteNode (org.apache.camel.impl.DoCatchRouteNode)1 DoFinallyRouteNode (org.apache.camel.impl.DoFinallyRouteNode)1 OnCompletionRouteNode (org.apache.camel.impl.OnCompletionRouteNode)1 OnExceptionRouteNode (org.apache.camel.impl.OnExceptionRouteNode)1 CatchDefinition (org.apache.camel.model.CatchDefinition)1 InterceptDefinition (org.apache.camel.model.InterceptDefinition)1 OnCompletionDefinition (org.apache.camel.model.OnCompletionDefinition)1 OnExceptionDefinition (org.apache.camel.model.OnExceptionDefinition)1 CamelLogProcessor (org.apache.camel.processor.CamelLogProcessor)1 DelegateAsyncProcessor (org.apache.camel.processor.DelegateAsyncProcessor)1 TracedRouteNodes (org.apache.camel.spi.TracedRouteNodes)1