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.ArrayList; 020 import java.util.List; 021 import java.util.concurrent.BlockingQueue; 022 import java.util.concurrent.ThreadPoolExecutor; 023 024 import javax.xml.bind.annotation.XmlAccessType; 025 import javax.xml.bind.annotation.XmlAccessorType; 026 import javax.xml.bind.annotation.XmlAttribute; 027 import javax.xml.bind.annotation.XmlElementRef; 028 import javax.xml.bind.annotation.XmlRootElement; 029 import javax.xml.bind.annotation.XmlTransient; 030 031 import org.apache.camel.Processor; 032 import org.apache.camel.impl.RouteContext; 033 import org.apache.camel.processor.Pipeline; 034 import org.apache.camel.processor.ThreadProcessor; 035 036 /** 037 * Represents an XML <thread/> element 038 * 039 * @version $Revision: 36865 $ 040 */ 041 @XmlRootElement(name = "thread") 042 @XmlAccessorType(XmlAccessType.FIELD) 043 public class ThreadType extends ProcessorType<ProcessorType> { 044 @XmlAttribute(required = false) 045 private Integer coreSize = 1; 046 @XmlAttribute(required = false) 047 private Boolean daemon = Boolean.TRUE; 048 @XmlAttribute(required = false) 049 private Long keepAliveTime; 050 @XmlAttribute(required = false) 051 private Integer maxSize = 1; 052 @XmlAttribute(required = false) 053 private String name = "Thread Processor"; 054 @XmlAttribute(required = false) 055 private Integer priority = Thread.NORM_PRIORITY; 056 @XmlAttribute(required = false) 057 private Long stackSize; 058 @XmlElementRef 059 private List<ProcessorType<?>> outputs = new ArrayList<ProcessorType<?>>(); 060 @XmlTransient 061 private BlockingQueue<Runnable> taskQueue; 062 @XmlTransient 063 private ThreadGroup threadGroup; 064 @XmlTransient 065 private ThreadPoolExecutor executor; 066 067 public ThreadType() { 068 } 069 070 public ThreadType(int coreSize) { 071 this.coreSize = coreSize; 072 this.maxSize = coreSize; 073 } 074 075 public ThreadType(ThreadPoolExecutor executor) { 076 this.executor = executor; 077 } 078 079 @Override 080 public List<ProcessorType<?>> getOutputs() { 081 return outputs; 082 } 083 084 @Override 085 public String toString() { 086 return "Thread[" + getLabel() + "]"; 087 } 088 089 @Override 090 public String getLabel() { 091 return "coreSize=" + coreSize; 092 } 093 094 @Override 095 public Processor createProcessor(RouteContext routeContext) throws Exception { 096 097 ThreadProcessor thread = new ThreadProcessor(); 098 thread.setExecutor(executor); 099 if (coreSize != null) { 100 thread.setCoreSize(coreSize); 101 } 102 if (daemon != null) { 103 thread.setDaemon(daemon); 104 } 105 if (keepAliveTime != null) { 106 thread.setKeepAliveTime(keepAliveTime); 107 } 108 if (maxSize != null) { 109 thread.setMaxSize(maxSize); 110 } 111 thread.setName(name); 112 thread.setPriority(priority); 113 if (stackSize != null) { 114 thread.setStackSize(stackSize); 115 } 116 thread.setTaskQueue(taskQueue); 117 thread.setThreadGroup(threadGroup); 118 119 // TODO: see if we can avoid creating so many nested pipelines 120 121 ArrayList<Processor> pipe = new ArrayList<Processor>(2); 122 pipe.add(thread); 123 pipe.add(createOutputsProcessor(routeContext, outputs)); 124 return new Pipeline(pipe); 125 } 126 127 /////////////////////////////////////////////////////////////////// 128 // 129 // Fluent Methods 130 // 131 /////////////////////////////////////////////////////////////////// 132 public ThreadType coreSize(int coreSize) { 133 setCoreSize(coreSize); 134 return this; 135 } 136 137 public ThreadType daemon(boolean daemon) { 138 setDaemon(daemon); 139 return this; 140 } 141 142 public ThreadType keepAliveTime(long keepAliveTime) { 143 setKeepAliveTime(keepAliveTime); 144 return this; 145 } 146 147 public ThreadType maxSize(int maxSize) { 148 setMaxSize(maxSize); 149 return this; 150 } 151 152 public ThreadType name(String name) { 153 setName(name); 154 return this; 155 } 156 157 public ThreadType priority(int priority) { 158 setPriority(priority); 159 return this; 160 } 161 162 public ThreadType stackSize(long stackSize) { 163 setStackSize(stackSize); 164 return this; 165 } 166 167 public ThreadType taskQueue(BlockingQueue<Runnable> taskQueue) { 168 setTaskQueue(taskQueue); 169 return this; 170 } 171 172 public ThreadType threadGroup(ThreadGroup threadGroup) { 173 setThreadGroup(threadGroup); 174 return this; 175 } 176 177 public ThreadType executor(ThreadPoolExecutor executor) { 178 setExecutor(executor); 179 return this; 180 } 181 182 /////////////////////////////////////////////////////////////////// 183 // 184 // Property Accessors 185 // 186 /////////////////////////////////////////////////////////////////// 187 188 public void setCoreSize(int coreSize) { 189 this.coreSize = coreSize; 190 } 191 192 public void setDaemon(boolean daemon) { 193 this.daemon = daemon; 194 } 195 196 public void setKeepAliveTime(long keepAliveTime) { 197 this.keepAliveTime = keepAliveTime; 198 } 199 200 public void setMaxSize(int maxSize) { 201 this.maxSize = maxSize; 202 } 203 204 public void setName(String name) { 205 this.name = name; 206 } 207 208 public void setPriority(int priority) { 209 this.priority = priority; 210 } 211 212 public void setStackSize(long stackSize) { 213 this.stackSize = stackSize; 214 } 215 216 public void setTaskQueue(BlockingQueue<Runnable> taskQueue) { 217 this.taskQueue = taskQueue; 218 } 219 220 public void setThreadGroup(ThreadGroup threadGroup) { 221 this.threadGroup = threadGroup; 222 } 223 224 public ThreadPoolExecutor getExecutor() { 225 return executor; 226 } 227 228 public void setExecutor(ThreadPoolExecutor executor) { 229 this.executor = executor; 230 } 231 }