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: 727988 $
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 String threadPoolExecutorRef;
053 @XmlAttribute(required = false)
054 private Boolean streaming = false;
055
056 public SplitterType() {
057 }
058
059 public SplitterType(Expression expression) {
060 super(expression);
061 }
062
063 public SplitterType(ExpressionType expression) {
064 super(expression);
065 }
066
067 @Override
068 public String toString() {
069 return "Splitter[" + getExpression() + " -> " + getOutputs() + "]";
070 }
071
072 @Override
073 public String getShortName() {
074 return "splitter";
075 }
076
077 @Override
078 public Processor createProcessor(RouteContext routeContext) throws Exception {
079 Processor childProcessor = routeContext.createProcessor(this);
080 if (aggregationStrategy == null) {
081 aggregationStrategy = new UseLatestAggregationStrategy();
082 }
083 threadPoolExecutor = createThreadPoolExecutor(routeContext);
084 return new Splitter(getExpression().createExpression(routeContext), childProcessor, aggregationStrategy,
085 isParallelProcessing(), threadPoolExecutor, streaming);
086 }
087
088 // Fluent API
089 // -------------------------------------------------------------------------
090 /**
091 * Set the spliter's aggregationStrategy
092 * @param aggregationStrategy
093 *
094 * @return the builder
095 */
096 public SplitterType aggregationStrategy(AggregationStrategy aggregationStrategy) {
097 setAggregationStrategy(aggregationStrategy);
098 return this;
099 }
100
101 /**
102 * Set to run the splitting action parallelly
103 *
104 * @return the builder
105 */
106 public SplitterType parallelProcessing() {
107 setParallelProcessing(true);
108 return this;
109 }
110
111 /**
112 * Set the splitting action's thread model
113 * @param parallelProcessing <tt>true</tt> to use a thread pool,
114 * if <tt>false</tt> then work is done in the calling thread.
115 *
116 * @return the builder
117 */
118 public SplitterType parallelProcessing(boolean parallelProcessing) {
119 setParallelProcessing(parallelProcessing);
120 return this;
121 }
122
123 /**
124 * Enables streaming.
125 * See {@link SplitterType#setStreaming(boolean)} for more information
126 *
127 * @return the builder
128 */
129 public SplitterType streaming() {
130 setStreaming(true);
131 return this;
132 }
133
134 /**
135 * Setting the executor for executing the splitting action.
136 * @param executor , it should be a instance of ThreadPoolExcutor
137 * NOTE in Camel 2.0 , it will change to use the instance which implements Executor interface
138 * @return the builder
139 */
140 public SplitterType executor(ThreadPoolExecutor executor) {
141 setThreadPoolExecutor(executor);
142 return this;
143 }
144
145 public AggregationStrategy getAggregationStrategy() {
146 return aggregationStrategy;
147 }
148
149 public void setAggregationStrategy(AggregationStrategy aggregationStrategy) {
150 this.aggregationStrategy = aggregationStrategy;
151 }
152
153 public boolean isParallelProcessing() {
154 return parallelProcessing != null ? parallelProcessing : false;
155 }
156
157 public void setParallelProcessing(boolean parallelProcessing) {
158 this.parallelProcessing = parallelProcessing;
159 }
160
161 /**
162 * The splitter should use streaming -- exchanges are being sent as the data for them becomes available.
163 * This improves throughput and memory usage, but it has a drawback:
164 * - the sent exchanges will no longer contain the {@link Splitter#SPLIT_SIZE} header property
165 *
166 * @return whether or not streaming should be used
167 */
168 public boolean isStreaming() {
169 return streaming != null ? streaming : false;
170 }
171
172 public void setStreaming(boolean streaming) {
173 this.streaming = streaming;
174 }
175
176
177 private ThreadPoolExecutor createThreadPoolExecutor(RouteContext routeContext) {
178 ThreadPoolExecutor threadPoolExecutor = getThreadPoolExecutor();
179 if (threadPoolExecutor == null && threadPoolExecutorRef != null) {
180 threadPoolExecutor = routeContext.lookup(threadPoolExecutorRef, ThreadPoolExecutor.class);
181 }
182 if (threadPoolExecutor == null) {
183 // fall back and use default
184 threadPoolExecutor = new ThreadPoolExecutor(4, 16, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue());
185 }
186 return threadPoolExecutor;
187 }
188
189 public ThreadPoolExecutor getThreadPoolExecutor() {
190 return threadPoolExecutor;
191 }
192
193 public void setThreadPoolExecutor(ThreadPoolExecutor threadPoolExecutor) {
194 this.threadPoolExecutor = threadPoolExecutor;
195 }
196 }