All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.camel.processor.Pipeline Maven / Gradle / Ivy

There is a newer version: 4.9.0
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.camel.processor;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.camel.AsyncCallback;
import org.apache.camel.AsyncProcessor;
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Navigate;
import org.apache.camel.Processor;
import org.apache.camel.Traceable;
import org.apache.camel.spi.IdAware;
import org.apache.camel.support.AsyncProcessorConverterHelper;
import org.apache.camel.support.AsyncProcessorSupport;
import org.apache.camel.support.ExchangeHelper;
import org.apache.camel.support.service.ServiceHelper;

import static org.apache.camel.processor.PipelineHelper.continueProcessing;

/**
 * Creates a Pipeline pattern where the output of the previous step is sent as
 * input to the next step, reusing the same message exchanges
 */
public class Pipeline extends AsyncProcessorSupport implements Navigate, Traceable, IdAware {

    private final CamelContext camelContext;
    private List processors;
    private String id;

    public Pipeline(CamelContext camelContext, Collection processors) {
        this.camelContext = camelContext;
        this.processors = processors.stream().map(AsyncProcessorConverterHelper::convert).collect(Collectors.toList());
    }

    public static Processor newInstance(CamelContext camelContext, List processors) {
        if (processors.isEmpty()) {
            return null;
        } else if (processors.size() == 1) {
            return processors.get(0);
        }
        return new Pipeline(camelContext, processors);
    }

    public static Processor newInstance(final CamelContext camelContext, final Processor... processors) {
        if (processors == null || processors.length == 0) {
            return null;
        } else if (processors.length == 1) {
            return processors[0];
        }

        final List toBeProcessed = new ArrayList<>(processors.length);
        for (Processor processor : processors) {
            if (processor != null) {
                toBeProcessed.add(processor);
            }
        }

        return new Pipeline(camelContext, toBeProcessed);
    }

    @Override
    public boolean process(Exchange exchange, AsyncCallback callback) {
        if (exchange.isTransacted()) {
            camelContext.getReactiveExecutor().scheduleSync(() -> Pipeline.this.doProcess(exchange, callback, processors.iterator(), true),
                    "Step[" + exchange.getExchangeId() + "," + Pipeline.this + "]");
        } else {
            camelContext.getReactiveExecutor().scheduleMain(() -> Pipeline.this.doProcess(exchange, callback, processors.iterator(), true),
                    "Step[" + exchange.getExchangeId() + "," + Pipeline.this + "]");
        }
        return false;
    }

    protected void doProcess(Exchange exchange, AsyncCallback callback, Iterator processors, boolean first) {
        if (continueRouting(processors, exchange)
                && (first || continueProcessing(exchange, "so breaking out of pipeline", log))) {

            // prepare for next run
            ExchangeHelper.prepareOutToIn(exchange);

            // get the next processor
            AsyncProcessor processor = processors.next();

            processor.process(exchange, doneSync ->
                    camelContext.getReactiveExecutor().schedule(() -> doProcess(exchange, callback, processors, false),
                            "Step[" + exchange.getExchangeId() + "," + Pipeline.this + "]"));
        } else {
            ExchangeHelper.copyResults(exchange, exchange);

            // logging nextExchange as it contains the exchange that might have altered the payload and since
            // we are logging the completion if will be confusing if we log the original instead
            // we could also consider logging the original and the nextExchange then we have *before* and *after* snapshots
            log.trace("Processing complete for exchangeId: {} >>> {}", exchange.getExchangeId(), exchange);

            camelContext.getReactiveExecutor().callback(callback);
        }
    }

    protected boolean continueRouting(Iterator it, Exchange exchange) {
        Object stop = exchange.getProperty(Exchange.ROUTE_STOP);
        if (stop != null) {
            boolean doStop = exchange.getContext().getTypeConverter().convertTo(Boolean.class, stop);
            if (doStop) {
                log.debug("ExchangeId: {} is marked to stop routing: {}", exchange.getExchangeId(), exchange);
                return false;
            }
        }
        // continue if there are more processors to route
        boolean answer = it.hasNext();
        log.trace("ExchangeId: {} should continue routing: {}", exchange.getExchangeId(), answer);
        return answer;
    }

    @Override
    protected void doStart() throws Exception {
        ServiceHelper.startService(processors);
    }

    @Override
    protected void doStop() throws Exception {
        ServiceHelper.stopService(processors);
    }

    @Override
    public String toString() {
        return "Pipeline[" + getProcessors() + "]";
    }

    public List getProcessors() {
        return (List) processors;
    }

    @Override
    public String getTraceLabel() {
        return "pipeline";
    }

    @Override
    public String getId() {
        return id;
    }

    @Override
    public void setId(String id) {
        this.id = id;
    }

    @Override
    public List next() {
        if (!hasNext()) {
            return null;
        }
        return new ArrayList<>(processors);
    }

    @Override
    public boolean hasNext() {
        return processors != null && !processors.isEmpty();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy