001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.camel.processor; 018 019import java.util.Collection; 020import java.util.Iterator; 021import java.util.List; 022 023import org.apache.camel.AsyncCallback; 024import org.apache.camel.AsyncProcessor; 025import org.apache.camel.CamelContext; 026import org.apache.camel.Exchange; 027import org.apache.camel.Processor; 028import org.apache.camel.Traceable; 029import org.apache.camel.spi.IdAware; 030import org.apache.camel.util.AsyncProcessorConverterHelper; 031import org.apache.camel.util.AsyncProcessorHelper; 032import org.apache.camel.util.ExchangeHelper; 033import org.slf4j.Logger; 034import org.slf4j.LoggerFactory; 035 036import static org.apache.camel.processor.PipelineHelper.continueProcessing; 037 038/** 039 * Creates a Pipeline pattern where the output of the previous step is sent as 040 * input to the next step, reusing the same message exchanges 041 * 042 * @version 043 */ 044public class Pipeline extends MulticastProcessor implements AsyncProcessor, Traceable, IdAware { 045 private static final Logger LOG = LoggerFactory.getLogger(Pipeline.class); 046 047 private String id; 048 049 public Pipeline(CamelContext camelContext, Collection<Processor> processors) { 050 super(camelContext, processors); 051 } 052 053 public static Processor newInstance(CamelContext camelContext, List<Processor> processors) { 054 if (processors.isEmpty()) { 055 return null; 056 } else if (processors.size() == 1) { 057 return processors.get(0); 058 } 059 return new Pipeline(camelContext, processors); 060 } 061 062 public void process(Exchange exchange) throws Exception { 063 AsyncProcessorHelper.process(this, exchange); 064 } 065 066 public boolean process(Exchange exchange, AsyncCallback callback) { 067 Iterator<Processor> processors = getProcessors().iterator(); 068 Exchange nextExchange = exchange; 069 boolean first = true; 070 071 while (continueRouting(processors, nextExchange)) { 072 if (first) { 073 first = false; 074 } else { 075 // prepare for next run 076 nextExchange = createNextExchange(nextExchange); 077 } 078 079 // get the next processor 080 Processor processor = processors.next(); 081 082 AsyncProcessor async = AsyncProcessorConverterHelper.convert(processor); 083 boolean sync = process(exchange, nextExchange, callback, processors, async); 084 085 // continue as long its being processed synchronously 086 if (!sync) { 087 LOG.trace("Processing exchangeId: {} is continued being processed asynchronously", exchange.getExchangeId()); 088 // the remainder of the pipeline will be completed async 089 // so we break out now, then the callback will be invoked which then continue routing from where we left here 090 return false; 091 } 092 093 LOG.trace("Processing exchangeId: {} is continued being processed synchronously", exchange.getExchangeId()); 094 095 // check for error if so we should break out 096 if (!continueProcessing(nextExchange, "so breaking out of pipeline", LOG)) { 097 break; 098 } 099 } 100 101 // logging nextExchange as it contains the exchange that might have altered the payload and since 102 // we are logging the completion if will be confusing if we log the original instead 103 // we could also consider logging the original and the nextExchange then we have *before* and *after* snapshots 104 LOG.trace("Processing complete for exchangeId: {} >>> {}", exchange.getExchangeId(), nextExchange); 105 106 // copy results back to the original exchange 107 ExchangeHelper.copyResults(exchange, nextExchange); 108 109 callback.done(true); 110 return true; 111 } 112 113 private boolean process(final Exchange original, final Exchange exchange, final AsyncCallback callback, 114 final Iterator<Processor> processors, final AsyncProcessor asyncProcessor) { 115 // this does the actual processing so log at trace level 116 LOG.trace("Processing exchangeId: {} >>> {}", exchange.getExchangeId(), exchange); 117 118 // implement asynchronous routing logic in callback so we can have the callback being 119 // triggered and then continue routing where we left 120 //boolean sync = AsyncProcessorHelper.process(asyncProcessor, exchange, 121 boolean sync = asyncProcessor.process(exchange, new AsyncCallback() { 122 public void done(boolean doneSync) { 123 // we only have to handle async completion of the pipeline 124 if (doneSync) { 125 return; 126 } 127 128 // continue processing the pipeline asynchronously 129 Exchange nextExchange = exchange; 130 while (continueRouting(processors, nextExchange)) { 131 AsyncProcessor processor = AsyncProcessorConverterHelper.convert(processors.next()); 132 133 // check for error if so we should break out 134 if (!continueProcessing(nextExchange, "so breaking out of pipeline", LOG)) { 135 break; 136 } 137 138 nextExchange = createNextExchange(nextExchange); 139 doneSync = process(original, nextExchange, callback, processors, processor); 140 if (!doneSync) { 141 LOG.trace("Processing exchangeId: {} is continued being processed asynchronously", exchange.getExchangeId()); 142 return; 143 } 144 } 145 146 ExchangeHelper.copyResults(original, nextExchange); 147 LOG.trace("Processing complete for exchangeId: {} >>> {}", original.getExchangeId(), original); 148 callback.done(false); 149 } 150 }); 151 152 return sync; 153 } 154 155 /** 156 * Strategy method to create the next exchange from the previous exchange. 157 * <p/> 158 * Remember to copy the original exchange id otherwise correlation of ids in the log is a problem 159 * 160 * @param previousExchange the previous exchange 161 * @return a new exchange 162 */ 163 protected Exchange createNextExchange(Exchange previousExchange) { 164 return PipelineHelper.createNextExchange(previousExchange); 165 } 166 167 protected boolean continueRouting(Iterator<Processor> it, Exchange exchange) { 168 boolean answer = true; 169 170 Object stop = exchange.getProperty(Exchange.ROUTE_STOP); 171 if (stop != null) { 172 boolean doStop = exchange.getContext().getTypeConverter().convertTo(Boolean.class, stop); 173 if (doStop) { 174 LOG.debug("ExchangeId: {} is marked to stop routing: {}", exchange.getExchangeId(), exchange); 175 answer = false; 176 } 177 } else { 178 // continue if there are more processors to route 179 answer = it.hasNext(); 180 } 181 182 LOG.trace("ExchangeId: {} should continue routing: {}", exchange.getExchangeId(), answer); 183 return answer; 184 } 185 186 @Override 187 public String toString() { 188 return "Pipeline[" + getProcessors() + "]"; 189 } 190 191 @Override 192 public String getTraceLabel() { 193 return "pipeline"; 194 } 195 196 public String getId() { 197 return id; 198 } 199 200 public void setId(String id) { 201 this.id = id; 202 } 203}