use of org.apache.camel.ThreadPoolRejectedPolicy in project camel by apache.
the class ThreadsDefinition method createProcessor.
@Override
public Processor createProcessor(RouteContext routeContext) throws Exception {
// the threads name
String name = getThreadName() != null ? getThreadName() : "Threads";
// prefer any explicit configured executor service
boolean shutdownThreadPool = ProcessorDefinitionHelper.willCreateNewThreadPool(routeContext, this, true);
ExecutorService threadPool = ProcessorDefinitionHelper.getConfiguredExecutorService(routeContext, name, this, false);
// resolve what rejected policy to use
ThreadPoolRejectedPolicy policy = resolveRejectedPolicy(routeContext);
if (policy == null) {
if (callerRunsWhenRejected == null || callerRunsWhenRejected) {
// should use caller runs by default if not configured
policy = ThreadPoolRejectedPolicy.CallerRuns;
} else {
policy = ThreadPoolRejectedPolicy.Abort;
}
}
log.debug("Using ThreadPoolRejectedPolicy: {}", policy);
// if no explicit then create from the options
if (threadPool == null) {
ExecutorServiceManager manager = routeContext.getCamelContext().getExecutorServiceManager();
// create the thread pool using a builder
ThreadPoolProfile profile = new ThreadPoolProfileBuilder(name).poolSize(getPoolSize()).maxPoolSize(getMaxPoolSize()).keepAliveTime(getKeepAliveTime(), getTimeUnit()).maxQueueSize(getMaxQueueSize()).rejectedPolicy(policy).allowCoreThreadTimeOut(getAllowCoreThreadTimeOut()).build();
threadPool = manager.newThreadPool(this, name, profile);
shutdownThreadPool = true;
} else {
if (getThreadName() != null && !getThreadName().equals("Threads")) {
throw new IllegalArgumentException("ThreadName and executorServiceRef options cannot be used together.");
}
if (getPoolSize() != null) {
throw new IllegalArgumentException("PoolSize and executorServiceRef options cannot be used together.");
}
if (getMaxPoolSize() != null) {
throw new IllegalArgumentException("MaxPoolSize and executorServiceRef options cannot be used together.");
}
if (getKeepAliveTime() != null) {
throw new IllegalArgumentException("KeepAliveTime and executorServiceRef options cannot be used together.");
}
if (getTimeUnit() != null) {
throw new IllegalArgumentException("TimeUnit and executorServiceRef options cannot be used together.");
}
if (getMaxQueueSize() != null) {
throw new IllegalArgumentException("MaxQueueSize and executorServiceRef options cannot be used together.");
}
if (getRejectedPolicy() != null) {
throw new IllegalArgumentException("RejectedPolicy and executorServiceRef options cannot be used together.");
}
if (getAllowCoreThreadTimeOut() != null) {
throw new IllegalArgumentException("AllowCoreThreadTimeOut and executorServiceRef options cannot be used together.");
}
}
ThreadsProcessor thread = new ThreadsProcessor(routeContext.getCamelContext(), threadPool, shutdownThreadPool, policy);
List<Processor> pipe = new ArrayList<Processor>(2);
pipe.add(thread);
pipe.add(createChildProcessor(routeContext, true));
// (recipient list definition does this as well)
return new Pipeline(routeContext.getCamelContext(), pipe) {
@Override
public String toString() {
return "Threads[" + getOutputs() + "]";
}
};
}
Aggregations