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.model;
018    
019    import java.util.concurrent.LinkedBlockingQueue;
020    import java.util.concurrent.ThreadPoolExecutor;
021    import java.util.concurrent.TimeUnit;
022    
023    import javax.xml.bind.annotation.XmlAccessType;
024    import javax.xml.bind.annotation.XmlAccessorType;
025    import javax.xml.bind.annotation.XmlAttribute;
026    import javax.xml.bind.annotation.XmlRootElement;
027    import javax.xml.bind.annotation.XmlTransient;
028    
029    import org.apache.camel.Expression;
030    import org.apache.camel.Processor;
031    import org.apache.camel.model.language.ExpressionType;
032    import org.apache.camel.processor.Splitter;
033    import org.apache.camel.processor.aggregate.AggregationStrategy;
034    import org.apache.camel.processor.aggregate.UseLatestAggregationStrategy;
035    import org.apache.camel.spi.RouteContext;
036    
037    /**
038     * Represents an XML <splitter/> element
039     *
040     * @version $Revision: 51106 $
041     */
042    @XmlRootElement(name = "splitter")
043    @XmlAccessorType(XmlAccessType.FIELD)
044    public class SplitterType extends ExpressionNode {
045        @XmlTransient
046        private AggregationStrategy aggregationStrategy;
047        @XmlAttribute(required = false)
048        private Boolean parallelProcessing;
049        @XmlTransient
050        private ThreadPoolExecutor threadPoolExecutor;
051        @XmlAttribute(required = false)
052        private Boolean streaming = false;
053        
054        public SplitterType() {
055        }
056    
057        public SplitterType(Expression expression) {
058            super(expression);
059        }
060    
061        public SplitterType(ExpressionType expression) {
062            super(expression);
063        }
064    
065        @Override
066        public String toString() {
067            return "Splitter[ " + getExpression() + " -> " + getOutputs() + "]";
068        }
069    
070        @Override
071        public String getShortName() {
072            return "splitter";
073        }
074    
075        @Override
076        public Processor createProcessor(RouteContext routeContext) throws Exception {
077            Processor childProcessor = routeContext.createProcessor(this);
078            if (aggregationStrategy == null) {
079                aggregationStrategy = new UseLatestAggregationStrategy();
080            }
081            if (threadPoolExecutor == null) {
082                threadPoolExecutor = new ThreadPoolExecutor(4, 16, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue());
083            }
084            return new Splitter(getExpression().createExpression(routeContext), childProcessor, aggregationStrategy,
085                    isParallelProcessing(), threadPoolExecutor, streaming);
086        }
087        
088        public AggregationStrategy getAggregationStrategy() {
089            return aggregationStrategy;
090        }
091    
092        public void setAggregationStrategy(AggregationStrategy aggregationStrategy) {
093            this.aggregationStrategy = aggregationStrategy;
094        }
095        
096        public boolean isParallelProcessing() {
097            return parallelProcessing != null ? parallelProcessing : false;
098        }
099    
100        public void setParallelProcessing(boolean parallelProcessing) {
101            this.parallelProcessing = parallelProcessing;
102        }
103        
104        /**
105         * The splitter should use streaming -- exchanges are being sent as the data for them becomes available.
106         * This improves throughput and memory usage, but it has a drawback: 
107         * - the sent exchanges will no longer contain the {@link Splitter#SPLIT_SIZE} header property 
108         * 
109         * @return 
110         */
111        public boolean getStreaming() {
112            return streaming != null ? streaming : false;
113        }
114    
115        public void setStreaming(boolean streaming) {
116            this.streaming = streaming;
117        }
118        
119        /**
120         * Enables streaming. 
121         * Cfr. {@link SplitterType#setStreaming(boolean)} for more information
122         */
123        public SplitterType streaming() {
124            setStreaming(true);
125            return this;
126        }
127    
128        public ThreadPoolExecutor getThreadPoolExecutor() {
129            return threadPoolExecutor;
130        }
131    
132        public void setThreadPoolExecutor(ThreadPoolExecutor threadPoolExecutor) {
133            this.threadPoolExecutor = threadPoolExecutor;
134        }
135    }