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     */
017    package org.apache.camel.processor;
018    
019    import java.util.concurrent.Callable;
020    import java.util.concurrent.ExecutorService;
021    
022    import org.apache.camel.CamelContext;
023    import org.apache.camel.Exchange;
024    import org.apache.camel.ExchangePattern;
025    import org.apache.camel.Message;
026    import org.apache.camel.Ordered;
027    import org.apache.camel.Predicate;
028    import org.apache.camel.Processor;
029    import org.apache.camel.Traceable;
030    import org.apache.camel.support.ServiceSupport;
031    import org.apache.camel.support.SynchronizationAdapter;
032    import org.apache.camel.util.ExchangeHelper;
033    import org.apache.camel.util.ServiceHelper;
034    import org.slf4j.Logger;
035    import org.slf4j.LoggerFactory;
036    
037    import static org.apache.camel.util.ObjectHelper.notNull;
038    
039    /**
040     * @version 
041     */
042    public class OnCompletionProcessor extends ServiceSupport implements Processor, Traceable {
043    
044        private static final transient Logger LOG = LoggerFactory.getLogger(OnCompletionProcessor.class);
045        private final CamelContext camelContext;
046        private final Processor processor;
047        private final ExecutorService executorService;
048        private final boolean shutdownExecutorService;
049        private final boolean onCompleteOnly;
050        private final boolean onFailureOnly;
051        private final Predicate onWhen;
052        private final boolean useOriginalBody;
053    
054        public OnCompletionProcessor(CamelContext camelContext, Processor processor, ExecutorService executorService, boolean shutdownExecutorService,
055                                     boolean onCompleteOnly, boolean onFailureOnly, Predicate onWhen, boolean useOriginalBody) {
056            notNull(camelContext, "camelContext");
057            notNull(processor, "processor");
058            this.camelContext = camelContext;
059            // wrap processor in UnitOfWork so what we send out runs in a UoW
060            this.processor = new UnitOfWorkProcessor(processor);
061            this.executorService = executorService;
062            this.shutdownExecutorService = shutdownExecutorService;
063            this.onCompleteOnly = onCompleteOnly;
064            this.onFailureOnly = onFailureOnly;
065            this.onWhen = onWhen;
066            this.useOriginalBody = useOriginalBody;
067        }
068    
069        @Override
070        protected void doStart() throws Exception {
071            ServiceHelper.startService(processor);
072        }
073    
074        @Override
075        protected void doStop() throws Exception {
076            ServiceHelper.stopService(processor);
077        }
078    
079        @Override
080        protected void doShutdown() throws Exception {
081            ServiceHelper.stopAndShutdownService(processor);
082            if (shutdownExecutorService) {
083                getCamelContext().getExecutorServiceManager().shutdownNow(executorService);
084            }
085        }
086    
087        public CamelContext getCamelContext() {
088            return camelContext;
089        }
090    
091        public void process(Exchange exchange) throws Exception {
092            if (processor == null) {
093                return;
094            }
095    
096            // register callback
097            exchange.getUnitOfWork().addSynchronization(new OnCompletionSynchronization());
098        }
099    
100        /**
101         * Processes the exchange by the processors
102         *
103         * @param processor the processor
104         * @param exchange the exchange
105         */
106        protected static void doProcess(Processor processor, Exchange exchange) {
107            try {
108                processor.process(exchange);
109            } catch (Exception e) {
110                exchange.setException(e);
111            }
112        }
113    
114        /**
115         * Prepares the {@link Exchange} to send as onCompletion.
116         *
117         * @param exchange the current exchange
118         * @return the exchange to be routed in onComplete
119         */
120        protected Exchange prepareExchange(Exchange exchange) {
121            Exchange answer;
122    
123            // for asynchronous routing we must use a copy as we dont want it
124            // to cause side effects of the original exchange
125            // (the original thread will run in parallel)
126            answer = ExchangeHelper.createCorrelatedCopy(exchange, false);
127            if (answer.hasOut()) {
128                // move OUT to IN (pipes and filters)
129                answer.setIn(answer.getOut());
130                answer.setOut(null);
131            }
132            // set MEP to InOnly as this wire tap is a fire and forget
133            answer.setPattern(ExchangePattern.InOnly);
134    
135            if (useOriginalBody) {
136                LOG.trace("Using the original IN message instead of current");
137    
138                Message original = exchange.getUnitOfWork().getOriginalInMessage();
139                answer.setIn(original);
140            }
141    
142            // add a header flag to indicate its a on completion exchange
143            answer.setProperty(Exchange.ON_COMPLETION, Boolean.TRUE);
144    
145            return answer;
146        }
147    
148        private final class OnCompletionSynchronization extends SynchronizationAdapter implements Ordered {
149    
150            public int getOrder() {
151                // we want to be last
152                return Ordered.LOWEST;
153            }
154    
155            @Override
156            public void onComplete(final Exchange exchange) {
157                if (onFailureOnly) {
158                    return;
159                }
160    
161                if (onWhen != null && !onWhen.matches(exchange)) {
162                    // predicate did not match so do not route the onComplete
163                    return;
164                }
165    
166                // must use a copy as we dont want it to cause side effects of the original exchange
167                final Exchange copy = prepareExchange(exchange);
168    
169                executorService.submit(new Callable<Exchange>() {
170                    public Exchange call() throws Exception {
171                        LOG.debug("Processing onComplete: {}", copy);
172                        doProcess(processor, copy);
173                        return copy;
174                    }
175                });
176            }
177    
178            public void onFailure(final Exchange exchange) {
179                if (onCompleteOnly) {
180                    return;
181                }
182    
183                if (onWhen != null && !onWhen.matches(exchange)) {
184                    // predicate did not match so do not route the onComplete
185                    return;
186                }
187    
188                // must use a copy as we dont want it to cause side effects of the original exchange
189                final Exchange copy = prepareExchange(exchange);
190                // must remove exception otherwise onFailure routing will fail as well
191                // the caused exception is stored as a property (Exchange.EXCEPTION_CAUGHT) on the exchange
192                copy.setException(null);
193    
194                executorService.submit(new Callable<Exchange>() {
195                    public Exchange call() throws Exception {
196                        LOG.debug("Processing onFailure: {}", copy);
197                        doProcess(processor, copy);
198                        return null;
199                    }
200                });
201            }
202    
203            @Override
204            public String toString() {
205                if (!onCompleteOnly && !onFailureOnly) {
206                    return "onCompleteOrFailure";
207                } else if (onCompleteOnly) {
208                    return "onCompleteOnly";
209                } else {
210                    return "onFailureOnly";
211                }
212            }
213        }
214    
215        @Override
216        public String toString() {
217            return "OnCompletionProcessor[" + processor + "]";
218        }
219    
220        public String getTraceLabel() {
221            return "onCompletion";
222        }
223    }