use of org.apache.camel.processor.Pipeline 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() + "]";
}
};
}
use of org.apache.camel.processor.Pipeline in project camel by apache.
the class RecipientListDefinition method createProcessor.
@Override
public Processor createProcessor(RouteContext routeContext) throws Exception {
final Expression expression = getExpression().createExpression(routeContext);
boolean isParallelProcessing = getParallelProcessing() != null && getParallelProcessing();
boolean isStreaming = getStreaming() != null && getStreaming();
boolean isParallelAggregate = getParallelAggregate() != null && getParallelAggregate();
boolean isShareUnitOfWork = getShareUnitOfWork() != null && getShareUnitOfWork();
boolean isStopOnException = getStopOnException() != null && getStopOnException();
boolean isIgnoreInvalidEndpoints = getIgnoreInvalidEndpoints() != null && getIgnoreInvalidEndpoints();
boolean isStopOnAggregateException = getStopOnAggregateException() != null && getStopOnAggregateException();
RecipientList answer;
if (delimiter != null) {
answer = new RecipientList(routeContext.getCamelContext(), expression, delimiter);
} else {
answer = new RecipientList(routeContext.getCamelContext(), expression);
}
answer.setAggregationStrategy(createAggregationStrategy(routeContext));
answer.setParallelProcessing(isParallelProcessing);
answer.setParallelAggregate(isParallelAggregate);
answer.setStreaming(isStreaming);
answer.setShareUnitOfWork(isShareUnitOfWork);
answer.setStopOnException(isStopOnException);
answer.setIgnoreInvalidEndpoints(isIgnoreInvalidEndpoints);
answer.setStopOnAggregateException(isStopOnAggregateException);
if (getCacheSize() != null) {
answer.setCacheSize(getCacheSize());
}
if (onPrepareRef != null) {
onPrepare = CamelContextHelper.mandatoryLookup(routeContext.getCamelContext(), onPrepareRef, Processor.class);
}
if (onPrepare != null) {
answer.setOnPrepare(onPrepare);
}
if (getTimeout() != null) {
answer.setTimeout(getTimeout());
}
boolean shutdownThreadPool = ProcessorDefinitionHelper.willCreateNewThreadPool(routeContext, this, isParallelProcessing);
ExecutorService threadPool = ProcessorDefinitionHelper.getConfiguredExecutorService(routeContext, "RecipientList", this, isParallelProcessing);
answer.setExecutorService(threadPool);
answer.setShutdownExecutorService(shutdownThreadPool);
long timeout = getTimeout() != null ? getTimeout() : 0;
if (timeout > 0 && !isParallelProcessing) {
throw new IllegalArgumentException("Timeout is used but ParallelProcessing has not been enabled.");
}
// create a pipeline with two processors
// the first is the eval processor which evaluates the expression to use
// the second is the recipient list
List<Processor> pipe = new ArrayList<Processor>(2);
// the eval processor must be wrapped in error handler, so in case there was an
// error during evaluation, the error handler can deal with it
// the recipient list is not in error handler, as its has its own special error handling
// when sending to the recipients individually
Processor evalProcessor = new EvaluateExpressionProcessor(expression);
evalProcessor = super.wrapInErrorHandler(routeContext, evalProcessor);
pipe.add(evalProcessor);
pipe.add(answer);
// (threads definition does this as well)
return new Pipeline(routeContext.getCamelContext(), pipe) {
@Override
public String toString() {
return "RecipientList[" + expression + "]";
}
};
}
use of org.apache.camel.processor.Pipeline in project camel by apache.
the class RouteBuilderTest method testRouteWithInterceptor.
public void testRouteWithInterceptor() throws Exception {
List<Route> routes = buildRouteWithInterceptor();
log.debug("Created routes: " + routes);
assertEquals("Number routes created", 1, routes.size());
for (Route route : routes) {
Endpoint key = route.getEndpoint();
assertEquals("From endpoint", "direct://a", key.getEndpointUri());
EventDrivenConsumerRoute consumer = assertIsInstanceOf(EventDrivenConsumerRoute.class, route);
Pipeline line = assertIsInstanceOf(Pipeline.class, unwrap(consumer.getProcessor()));
assertEquals(3, line.getProcessors().size());
// last should be our seda
List<Processor> processors = new ArrayList<Processor>(line.getProcessors());
Processor sendTo = assertIsInstanceOf(SendProcessor.class, unwrapChannel(processors.get(2)).getNextProcessor());
assertSendTo(sendTo, "direct://d");
}
}
use of org.apache.camel.processor.Pipeline in project camel by apache.
the class DefaultManagementObjectStrategy method getManagedObjectForProcessor.
@SuppressWarnings({ "deprecation", "unchecked" })
public Object getManagedObjectForProcessor(CamelContext context, Processor processor, ProcessorDefinition<?> definition, Route route) {
ManagedProcessor answer = null;
if (definition instanceof RecipientListDefinition) {
// special for RecipientListDefinition, as the processor is wrapped in a pipeline as last
Pipeline pipeline = (Pipeline) processor;
Iterator<Processor> it = pipeline.getProcessors().iterator();
while (it.hasNext()) {
processor = it.next();
}
} else if (definition instanceof ThreadsDefinition) {
// special for ThreadsDefinition, as the processor is wrapped in a pipeline as first
Pipeline pipeline = (Pipeline) processor;
Iterator<Processor> it = pipeline.getProcessors().iterator();
processor = it.next();
}
// unwrap delegates as we want the real target processor
Processor target = processor;
while (target != null) {
// skip error handlers
if (target instanceof ErrorHandler) {
return false;
}
if (target instanceof ConvertBodyProcessor) {
answer = new ManagedConvertBody(context, (ConvertBodyProcessor) target, definition);
} else if (target instanceof ChoiceProcessor) {
answer = new ManagedChoice(context, (ChoiceProcessor) target, definition);
} else if (target instanceof Delayer) {
answer = new ManagedDelayer(context, (Delayer) target, definition);
} else if (target instanceof Throttler) {
answer = new ManagedThrottler(context, (Throttler) target, definition);
} else if (target instanceof DynamicRouter) {
answer = new ManagedDynamicRouter(context, (DynamicRouter) target, (org.apache.camel.model.DynamicRouterDefinition) definition);
} else if (target instanceof RoutingSlip) {
answer = new ManagedRoutingSlip(context, (RoutingSlip) target, (org.apache.camel.model.RoutingSlipDefinition) definition);
} else if (target instanceof FilterProcessor) {
answer = new ManagedFilter(context, (FilterProcessor) target, (ExpressionNode) definition);
} else if (target instanceof LogProcessor) {
answer = new ManagedLog(context, (LogProcessor) target, definition);
} else if (target instanceof LoopProcessor) {
answer = new ManagedLoop(context, (LoopProcessor) target, (org.apache.camel.model.LoopDefinition) definition);
} else if (target instanceof MarshalProcessor) {
answer = new ManagedMarshal(context, (MarshalProcessor) target, (org.apache.camel.model.MarshalDefinition) definition);
} else if (target instanceof UnmarshalProcessor) {
answer = new ManagedUnmarshal(context, (UnmarshalProcessor) target, (org.apache.camel.model.UnmarshalDefinition) definition);
} else if (target instanceof CircuitBreakerLoadBalancer) {
answer = new ManagedCircuitBreakerLoadBalancer(context, (CircuitBreakerLoadBalancer) target, (org.apache.camel.model.LoadBalanceDefinition) definition);
} else if (target instanceof FailOverLoadBalancer) {
answer = new ManagedFailoverLoadBalancer(context, (FailOverLoadBalancer) target, (org.apache.camel.model.LoadBalanceDefinition) definition);
} else if (target instanceof RandomLoadBalancer) {
answer = new ManagedRandomLoadBalancer(context, (RandomLoadBalancer) target, (org.apache.camel.model.LoadBalanceDefinition) definition);
} else if (target instanceof RoundRobinLoadBalancer) {
answer = new ManagedRoundRobinLoadBalancer(context, (RoundRobinLoadBalancer) target, (org.apache.camel.model.LoadBalanceDefinition) definition);
} else if (target instanceof StickyLoadBalancer) {
answer = new ManagedStickyLoadBalancer(context, (StickyLoadBalancer) target, (org.apache.camel.model.LoadBalanceDefinition) definition);
} else if (target instanceof TopicLoadBalancer) {
answer = new ManagedTopicLoadBalancer(context, (TopicLoadBalancer) target, (org.apache.camel.model.LoadBalanceDefinition) definition);
} else if (target instanceof WeightedLoadBalancer) {
answer = new ManagedWeightedLoadBalancer(context, (WeightedLoadBalancer) target, (org.apache.camel.model.LoadBalanceDefinition) definition);
} else if (target instanceof RecipientList) {
answer = new ManagedRecipientList(context, (RecipientList) target, (RecipientListDefinition) definition);
} else if (target instanceof Splitter) {
answer = new ManagedSplitter(context, (Splitter) target, (org.apache.camel.model.SplitDefinition) definition);
} else if (target instanceof MulticastProcessor) {
answer = new ManagedMulticast(context, (MulticastProcessor) target, definition);
} else if (target instanceof SamplingThrottler) {
answer = new ManagedSamplingThrottler(context, (SamplingThrottler) target, definition);
} else if (target instanceof Resequencer) {
answer = new ManagedResequencer(context, (Resequencer) target, definition);
} else if (target instanceof RollbackProcessor) {
answer = new ManagedRollback(context, (RollbackProcessor) target, definition);
} else if (target instanceof StreamResequencer) {
answer = new ManagedResequencer(context, (StreamResequencer) target, definition);
} else if (target instanceof SetBodyProcessor) {
answer = new ManagedSetBody(context, (SetBodyProcessor) target, (org.apache.camel.model.SetBodyDefinition) definition);
} else if (target instanceof RemoveHeaderProcessor) {
answer = new ManagedRemoveHeader(context, (RemoveHeaderProcessor) target, definition);
} else if (target instanceof RemoveHeadersProcessor) {
answer = new ManagedRemoveHeaders(context, (RemoveHeadersProcessor) target, definition);
} else if (target instanceof SetHeaderProcessor) {
answer = new ManagedSetHeader(context, (SetHeaderProcessor) target, (org.apache.camel.model.SetHeaderDefinition) definition);
} else if (target instanceof RemovePropertyProcessor) {
answer = new ManagedRemoveProperty(context, (RemovePropertyProcessor) target, definition);
} else if (target instanceof RemovePropertiesProcessor) {
answer = new ManagedRemoveProperties(context, (RemovePropertiesProcessor) target, definition);
} else if (target instanceof SetPropertyProcessor) {
answer = new ManagedSetProperty(context, (SetPropertyProcessor) target, (org.apache.camel.model.SetPropertyDefinition) definition);
} else if (target instanceof ExchangePatternProcessor) {
answer = new ManagedSetExchangePattern(context, (ExchangePatternProcessor) target, definition);
} else if (target instanceof ScriptProcessor) {
answer = new ManagedScript(context, (ScriptProcessor) target, (org.apache.camel.model.ScriptDefinition) definition);
} else if (target instanceof StopProcessor) {
answer = new ManagedStop(context, (StopProcessor) target, definition);
} else if (target instanceof ThreadsProcessor) {
answer = new ManagedThreads(context, (ThreadsProcessor) target, definition);
} else if (target instanceof ThrowExceptionProcessor) {
answer = new ManagedThrowException(context, (ThrowExceptionProcessor) target, definition);
} else if (target instanceof TransformProcessor) {
answer = new ManagedTransformer(context, (TransformProcessor) target, (org.apache.camel.model.TransformDefinition) definition);
} else if (target instanceof PredicateValidatingProcessor) {
answer = new ManagedValidate(context, (PredicateValidatingProcessor) target, (org.apache.camel.model.ValidateDefinition) definition);
} else if (target instanceof WireTapProcessor) {
answer = new ManagedWireTapProcessor(context, (WireTapProcessor) target, definition);
} else if (target instanceof SendDynamicProcessor) {
answer = new ManagedSendDynamicProcessor(context, (SendDynamicProcessor) target, definition);
} else if (target instanceof SendProcessor) {
SendProcessor sp = (SendProcessor) target;
// special for sending to throughput logger
if (sp.getDestination() instanceof LogEndpoint) {
LogEndpoint le = (LogEndpoint) sp.getDestination();
if (le.getLogger() instanceof ThroughputLogger) {
ThroughputLogger tl = (ThroughputLogger) le.getLogger();
answer = new ManagedThroughputLogger(context, tl, definition);
}
}
// regular send processor
if (answer == null) {
answer = new ManagedSendProcessor(context, (SendProcessor) target, definition);
}
} else if (target instanceof BeanProcessor) {
answer = new ManagedBeanProcessor(context, (BeanProcessor) target, definition);
} else if (target instanceof IdempotentConsumer) {
answer = new ManagedIdempotentConsumer(context, (IdempotentConsumer) target, (org.apache.camel.model.IdempotentConsumerDefinition) definition);
} else if (target instanceof AggregateProcessor) {
answer = new ManagedAggregateProcessor(context, (AggregateProcessor) target, (org.apache.camel.model.AggregateDefinition) definition);
} else if (target instanceof Enricher) {
answer = new ManagedEnricher(context, (Enricher) target, (org.apache.camel.model.EnrichDefinition) definition);
} else if (target instanceof PollEnricher) {
answer = new ManagedPollEnricher(context, (PollEnricher) target, (org.apache.camel.model.PollEnrichDefinition) definition);
} else if (target instanceof org.apache.camel.spi.ManagementAware) {
return ((org.apache.camel.spi.ManagementAware<Processor>) target).getManagedObject(processor);
}
// special for custom load balancer
if (definition instanceof LoadBalanceDefinition) {
LoadBalanceDefinition lb = (LoadBalanceDefinition) definition;
if (lb.getLoadBalancerType() instanceof CustomLoadBalancerDefinition) {
answer = new ManagedCustomLoadBalancer(context, (LoadBalancer) target, (LoadBalanceDefinition) definition);
}
}
if (answer != null) {
// break out as we found an answer
break;
}
// no answer yet, so unwrap any delegates and try again
if (target instanceof DelegateProcessor) {
target = ((DelegateProcessor) target).getProcessor();
} else {
// no delegate so we dont have any target to try next
break;
}
}
if (answer == null && definition instanceof ProcessDefinition) {
answer = new ManagedProcess(context, target, (ProcessDefinition) definition);
} else if (answer == null) {
// fallback to a generic processor
answer = new ManagedProcessor(context, target, definition);
}
answer.setRoute(route);
answer.init(context.getManagementStrategy());
return answer;
}
use of org.apache.camel.processor.Pipeline in project camel by apache.
the class InterceptDefinition method createProcessor.
@Override
public Processor createProcessor(final RouteContext routeContext) throws Exception {
// create the output processor
output = this.createChildProcessor(routeContext, true);
// add the output as a intercept strategy to the route context so its invoked on each processing step
routeContext.getInterceptStrategies().add(new InterceptStrategy() {
private Processor interceptedTarget;
public Processor wrapProcessorInInterceptors(CamelContext context, ProcessorDefinition<?> definition, Processor target, Processor nextTarget) throws Exception {
// store the target we are intercepting
this.interceptedTarget = target;
// remember the target that was intercepted
intercepted.add(interceptedTarget);
if (interceptedTarget != null) {
// wrap in a pipeline so we continue routing to the next
List<Processor> list = new ArrayList<Processor>(2);
list.add(output);
list.add(interceptedTarget);
return new Pipeline(context, list);
} else {
return output;
}
}
@Override
public String toString() {
return "intercept[" + (interceptedTarget != null ? interceptedTarget : output) + "]";
}
});
// remove me from the route so I am not invoked in a regular route path
routeContext.getRoute().getOutputs().remove(this);
// and return no processor to invoke next from me
return null;
}
Aggregations