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.model;
018
019import java.util.concurrent.ExecutorService;
020import javax.xml.bind.annotation.XmlAccessType;
021import javax.xml.bind.annotation.XmlAccessorType;
022import javax.xml.bind.annotation.XmlAttribute;
023import javax.xml.bind.annotation.XmlRootElement;
024import javax.xml.bind.annotation.XmlTransient;
025
026import org.apache.camel.CamelContextAware;
027import org.apache.camel.Expression;
028import org.apache.camel.Processor;
029import org.apache.camel.model.language.ExpressionDefinition;
030import org.apache.camel.processor.CamelInternalProcessor;
031import org.apache.camel.processor.Splitter;
032import org.apache.camel.processor.aggregate.AggregationStrategy;
033import org.apache.camel.processor.aggregate.AggregationStrategyBeanAdapter;
034import org.apache.camel.spi.Metadata;
035import org.apache.camel.spi.RouteContext;
036import org.apache.camel.util.CamelContextHelper;
037
038/**
039 * Splits a single message into many sub-messages.
040 *
041 * @version 
042 */
043@Metadata(label = "eip,routing")
044@XmlRootElement(name = "split")
045@XmlAccessorType(XmlAccessType.FIELD)
046public class SplitDefinition extends ExpressionNode implements ExecutorServiceAwareDefinition<SplitDefinition> {
047    @XmlTransient
048    private AggregationStrategy aggregationStrategy;
049    @XmlTransient
050    private ExecutorService executorService;
051    @XmlAttribute
052    private Boolean parallelProcessing;
053    @XmlAttribute
054    private String strategyRef;
055    @XmlAttribute
056    private String strategyMethodName;
057    @XmlAttribute
058    private Boolean strategyMethodAllowNull;
059    @XmlAttribute
060    private String executorServiceRef;
061    @XmlAttribute
062    private Boolean streaming;
063    @XmlAttribute
064    private Boolean stopOnException;
065    @XmlAttribute @Metadata(defaultValue = "0")
066    private Long timeout;
067    @XmlAttribute
068    private String onPrepareRef;
069    @XmlTransient
070    private Processor onPrepare;
071    @XmlAttribute
072    private Boolean shareUnitOfWork;
073    @XmlAttribute
074    private Boolean parallelAggregate;
075
076    public SplitDefinition() {
077    }
078
079    public SplitDefinition(Expression expression) {
080        super(expression);
081    }
082
083    public SplitDefinition(ExpressionDefinition expression) {
084        super(expression);
085    }
086
087    @Override
088    public String toString() {
089        return "Split[" + getExpression() + " -> " + getOutputs() + "]";
090    }
091
092    @Override
093    public String getLabel() {
094        return "split[" + getExpression() + "]";
095    }
096
097    @Override
098    public Processor createProcessor(RouteContext routeContext) throws Exception {
099        Processor childProcessor = this.createChildProcessor(routeContext, true);
100        aggregationStrategy = createAggregationStrategy(routeContext);
101
102        boolean isParallelProcessing = getParallelProcessing() != null && getParallelProcessing();
103        boolean isStreaming = getStreaming() != null && getStreaming();
104        boolean isShareUnitOfWork = getShareUnitOfWork() != null && getShareUnitOfWork();
105        boolean isParallelAggregate = getParallelAggregate() != null && getParallelAggregate();
106        boolean shutdownThreadPool = ProcessorDefinitionHelper.willCreateNewThreadPool(routeContext, this, isParallelProcessing);
107        ExecutorService threadPool = ProcessorDefinitionHelper.getConfiguredExecutorService(routeContext, "Split", this, isParallelProcessing);
108
109        long timeout = getTimeout() != null ? getTimeout() : 0;
110        if (timeout > 0 && !isParallelProcessing) {
111            throw new IllegalArgumentException("Timeout is used but ParallelProcessing has not been enabled.");
112        }
113        if (onPrepareRef != null) {
114            onPrepare = CamelContextHelper.mandatoryLookup(routeContext.getCamelContext(), onPrepareRef, Processor.class);
115        }
116
117        Expression exp = getExpression().createExpression(routeContext);
118
119        Splitter answer = new Splitter(routeContext.getCamelContext(), exp, childProcessor, aggregationStrategy,
120                            isParallelProcessing, threadPool, shutdownThreadPool, isStreaming, isStopOnException(),
121                            timeout, onPrepare, isShareUnitOfWork, isParallelAggregate);
122        if (isShareUnitOfWork) {
123            // wrap answer in a sub unit of work, since we share the unit of work
124            CamelInternalProcessor internalProcessor = new CamelInternalProcessor(answer);
125            internalProcessor.addAdvice(new CamelInternalProcessor.SubUnitOfWorkProcessorAdvice());
126            return internalProcessor;
127        }
128        return answer;
129    }
130
131    private AggregationStrategy createAggregationStrategy(RouteContext routeContext) {
132        AggregationStrategy strategy = getAggregationStrategy();
133        if (strategy == null && strategyRef != null) {
134            Object aggStrategy = routeContext.lookup(strategyRef, Object.class);
135            if (aggStrategy instanceof AggregationStrategy) {
136                strategy = (AggregationStrategy) aggStrategy;
137            } else if (aggStrategy != null) {
138                AggregationStrategyBeanAdapter adapter = new AggregationStrategyBeanAdapter(aggStrategy, getStrategyMethodName());
139                if (getStrategyMethodAllowNull() != null) {
140                    adapter.setAllowNullNewExchange(getStrategyMethodAllowNull());
141                    adapter.setAllowNullOldExchange(getStrategyMethodAllowNull());
142                }
143                strategy = adapter;
144            } else {
145                throw new IllegalArgumentException("Cannot find AggregationStrategy in Registry with name: " + strategyRef);
146            }
147        }
148
149        if (strategy != null && strategy instanceof CamelContextAware) {
150            ((CamelContextAware) strategy).setCamelContext(routeContext.getCamelContext());
151        }
152
153        return strategy;
154    }
155
156    // Fluent API
157    // -------------------------------------------------------------------------
158
159    /**
160     * Sets the AggregationStrategy to be used to assemble the replies from the splitted messages, into a single outgoing message from the Splitter.
161     * By default Camel will use the original incoming message to the splitter (leave it unchanged). You can also use a POJO as the AggregationStrategy
162     */
163    public SplitDefinition aggregationStrategy(AggregationStrategy aggregationStrategy) {
164        setAggregationStrategy(aggregationStrategy);
165        return this;
166    }
167
168    /**
169     * Sets a reference to the AggregationStrategy to be used to assemble the replies from the splitted messages, into a single outgoing message from the Splitter.
170     * By default Camel will use the original incoming message to the splitter (leave it unchanged). You can also use a POJO as the AggregationStrategy
171     */
172    public SplitDefinition aggregationStrategyRef(String aggregationStrategyRef) {
173        setStrategyRef(aggregationStrategyRef);
174        return this;
175    }
176
177    /**
178     * This option can be used to explicit declare the method name to use, when using POJOs as the AggregationStrategy.
179     *
180     * @param  methodName the method name to call
181     * @return the builder
182     */
183    public SplitDefinition aggregationStrategyMethodName(String methodName) {
184        setStrategyMethodName(methodName);
185        return this;
186    }
187
188    /**
189     * If this option is false then the aggregate method is not used if there was no data to enrich.
190     * If this option is true then null values is used as the oldExchange (when no data to enrich), when using POJOs as the AggregationStrategy
191     *
192     * @return the builder
193     */
194    public SplitDefinition aggregationStrategyMethodAllowNull() {
195        setStrategyMethodAllowNull(true);
196        return this;
197    }
198
199    /**
200     * If enabled then processing each splitted messages occurs concurrently.
201     * Note the caller thread will still wait until all messages has been fully processed, before it continues.
202     * Its only processing the sub messages from the splitter which happens concurrently.
203     *
204     * @return the builder
205     */
206    public SplitDefinition parallelProcessing() {
207        setParallelProcessing(true);
208        return this;
209    }
210
211    /**
212     * If enabled then the aggregate method on AggregationStrategy can be called concurrently.
213     * Notice that this would require the implementation of AggregationStrategy to be implemented as thread-safe.
214     * By default this is false meaning that Camel synchronizes the call to the aggregate method.
215     * Though in some use-cases this can be used to archive higher performance when the AggregationStrategy is implemented as thread-safe.
216     *
217     * @return the builder
218     */
219    public SplitDefinition parallelAggregate() {
220        setParallelAggregate(true);
221        return this;
222    }
223
224    /**
225     * When in streaming mode, then the splitter splits the original message on-demand, and each splitted
226     * message is processed one by one. This reduces memory usage as the splitter do not split all the messages first,
227     * but then we do not know the total size, and therefore the {@link org.apache.camel.Exchange#SPLIT_SIZE} is empty.
228     * <p/>
229     * In non-streaming mode (default) the splitter will split each message first, to know the total size, and then
230     * process each message one by one. This requires to keep all the splitted messages in memory and therefore requires
231     * more memory. The total size is provided in the {@link org.apache.camel.Exchange#SPLIT_SIZE} header.
232     * <p/>
233     * The streaming mode also affects the aggregation behavior.
234     * If enabled then Camel will process replies out-of-order, eg in the order they come back.
235     * If disabled, Camel will process replies in the same order as the messages was splitted.
236     *
237     * @return the builder
238     */
239    public SplitDefinition streaming() {
240        setStreaming(true);
241        return this;
242    }
243    
244    /**
245     * Will now stop further processing if an exception or failure occurred during processing of an
246     * {@link org.apache.camel.Exchange} and the caused exception will be thrown.
247     * <p/>
248     * Will also stop if processing the exchange failed (has a fault message) or an exception
249     * was thrown and handled by the error handler (such as using onException). In all situations
250     * the splitter will stop further processing. This is the same behavior as in pipeline, which
251     * is used by the routing engine.
252     * <p/>
253     * The default behavior is to <b>not</b> stop but continue processing till the end
254     *
255     * @return the builder
256     */
257    public SplitDefinition stopOnException() {
258        setStopOnException(true);
259        return this;
260    }
261
262    /**
263     * To use a custom Thread Pool to be used for parallel processing.
264     * Notice if you set this option, then parallel processing is automatic implied, and you do not have to enable that option as well.
265     */
266    public SplitDefinition executorService(ExecutorService executorService) {
267        setExecutorService(executorService);
268        return this;
269    }
270
271    /**
272     * Refers to a custom Thread Pool to be used for parallel processing.
273     * Notice if you set this option, then parallel processing is automatic implied, and you do not have to enable that option as well.
274     */
275    public SplitDefinition executorServiceRef(String executorServiceRef) {
276        setExecutorServiceRef(executorServiceRef);
277        return this;
278    }
279
280    /**
281     * Uses the {@link Processor} when preparing the {@link org.apache.camel.Exchange} to be send.
282     * This can be used to deep-clone messages that should be send, or any custom logic needed before
283     * the exchange is send.
284     *
285     * @param onPrepare the processor
286     * @return the builder
287     */
288    public SplitDefinition onPrepare(Processor onPrepare) {
289        setOnPrepare(onPrepare);
290        return this;
291    }
292
293    /**
294     * Uses the {@link Processor} when preparing the {@link org.apache.camel.Exchange} to be send.
295     * This can be used to deep-clone messages that should be send, or any custom logic needed before
296     * the exchange is send.
297     *
298     * @param onPrepareRef reference to the processor to lookup in the {@link org.apache.camel.spi.Registry}
299     * @return the builder
300     */
301    public SplitDefinition onPrepareRef(String onPrepareRef) {
302        setOnPrepareRef(onPrepareRef);
303        return this;
304    }
305
306    /**
307     * Sets a total timeout specified in millis, when using parallel processing.
308     * If the Splitter hasn't been able to split and process all the sub messages within the given timeframe,
309     * then the timeout triggers and the Splitter breaks out and continues.
310     * Notice if you provide a TimeoutAwareAggregationStrategy then the timeout method is invoked before breaking out.
311     * If the timeout is reached with running tasks still remaining, certain tasks for which it is difficult for Camel
312     * to shut down in a graceful manner may continue to run. So use this option with a bit of care.
313     *
314     * @param timeout timeout in millis
315     * @return the builder
316     */
317    public SplitDefinition timeout(long timeout) {
318        setTimeout(timeout);
319        return this;
320    }
321
322    /**
323     * Shares the {@link org.apache.camel.spi.UnitOfWork} with the parent and each of the sub messages.
324     * Splitter will by default not share unit of work between the parent exchange and each splitted exchange.
325     * This means each splitted exchange has its own individual unit of work.
326     *
327     * @return the builder.
328     * @see org.apache.camel.spi.SubUnitOfWork
329     */
330    public SplitDefinition shareUnitOfWork() {
331        setShareUnitOfWork(true);
332        return this;
333    }
334
335    // Properties
336    //-------------------------------------------------------------------------
337
338    /**
339     * Expression of how to split the message body, such as as-is, using a tokenizer, or using an xpath.
340     */
341    @Override
342    public void setExpression(ExpressionDefinition expression) {
343        // override to include javadoc what the expression is used for
344        super.setExpression(expression);
345    }
346
347    public AggregationStrategy getAggregationStrategy() {
348        return aggregationStrategy;
349    }
350
351    /**
352     * Sets the AggregationStrategy to be used to assemble the replies from the splitted messages, into a single outgoing message from the Splitter.
353     * By default Camel will use the original incoming message to the splitter (leave it unchanged). You can also use a POJO as the AggregationStrategy
354     */
355    public void setAggregationStrategy(AggregationStrategy aggregationStrategy) {
356        this.aggregationStrategy = aggregationStrategy;
357    }
358
359    public Boolean getParallelProcessing() {
360        return parallelProcessing;
361    }
362
363    public void setParallelProcessing(Boolean parallelProcessing) {
364        this.parallelProcessing = parallelProcessing;
365    }
366
367    public Boolean getStreaming() {
368        return streaming;
369    }
370
371    public void setStreaming(Boolean streaming) {
372        this.streaming = streaming;
373    }
374
375    public Boolean getParallelAggregate() {
376        return parallelAggregate;
377    }
378
379    public void setParallelAggregate(Boolean parallelAggregate) {
380        this.parallelAggregate = parallelAggregate;
381    }
382
383    public Boolean getStopOnException() {
384        return stopOnException;
385    }
386
387    public void setStopOnException(Boolean stopOnException) {
388        this.stopOnException = stopOnException;
389    }
390
391    public Boolean isStopOnException() {
392        return stopOnException != null && stopOnException;
393    }
394
395    public ExecutorService getExecutorService() {
396        return executorService;
397    }
398
399    public void setExecutorService(ExecutorService executorService) {
400        this.executorService = executorService;
401    }
402
403    public String getStrategyRef() {
404        return strategyRef;
405    }
406
407    /**
408     * Sets a reference to the AggregationStrategy to be used to assemble the replies from the splitted messages, into a single outgoing message from the Splitter.
409     * By default Camel will use the original incoming message to the splitter (leave it unchanged). You can also use a POJO as the AggregationStrategy
410     */
411    public void setStrategyRef(String strategyRef) {
412        this.strategyRef = strategyRef;
413    }
414
415    public String getStrategyMethodName() {
416        return strategyMethodName;
417    }
418
419    /**
420     * This option can be used to explicit declare the method name to use, when using POJOs as the AggregationStrategy.
421     */
422    public void setStrategyMethodName(String strategyMethodName) {
423        this.strategyMethodName = strategyMethodName;
424    }
425
426    public Boolean getStrategyMethodAllowNull() {
427        return strategyMethodAllowNull;
428    }
429
430    /**
431     * If this option is false then the aggregate method is not used if there was no data to enrich.
432     * If this option is true then null values is used as the oldExchange (when no data to enrich), when using POJOs as the AggregationStrategy
433     */
434    public void setStrategyMethodAllowNull(Boolean strategyMethodAllowNull) {
435        this.strategyMethodAllowNull = strategyMethodAllowNull;
436    }
437
438    public String getExecutorServiceRef() {
439        return executorServiceRef;
440    }
441
442    public void setExecutorServiceRef(String executorServiceRef) {
443        this.executorServiceRef = executorServiceRef;
444    }
445
446    public Long getTimeout() {
447        return timeout;
448    }
449
450    public void setTimeout(Long timeout) {
451        this.timeout = timeout;
452    }
453
454    public String getOnPrepareRef() {
455        return onPrepareRef;
456    }
457
458    public void setOnPrepareRef(String onPrepareRef) {
459        this.onPrepareRef = onPrepareRef;
460    }
461
462    public Processor getOnPrepare() {
463        return onPrepare;
464    }
465
466    public void setOnPrepare(Processor onPrepare) {
467        this.onPrepare = onPrepare;
468    }
469
470    public Boolean getShareUnitOfWork() {
471        return shareUnitOfWork;
472    }
473
474    public void setShareUnitOfWork(Boolean shareUnitOfWork) {
475        this.shareUnitOfWork = shareUnitOfWork;
476    }
477
478}