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: 61170 $
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 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 whether or not streaming should be used
110 */
111 public boolean isStreaming() {
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 private ThreadPoolExecutor createThreadPoolExecutor(RouteContext routeContext) {
129 ThreadPoolExecutor threadPoolExecutor = getThreadPoolExecutor();
130 if (threadPoolExecutor == null && threadPoolExecutorRef != null) {
131 threadPoolExecutor = routeContext.lookup(threadPoolExecutorRef, ThreadPoolExecutor.class);
132 }
133 if (threadPoolExecutor == null) {
134 // fall back and use default
135 threadPoolExecutor = new ThreadPoolExecutor(4, 16, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue());
136 }
137 return threadPoolExecutor;
138 }
139
140 public ThreadPoolExecutor getThreadPoolExecutor() {
141 return threadPoolExecutor;
142 }
143
144 public void setThreadPoolExecutor(ThreadPoolExecutor threadPoolExecutor) {
145 this.threadPoolExecutor = threadPoolExecutor;
146 }
147 }