Search in sources :

Example 6 with AggregateProcessor

use of org.apache.camel.processor.aggregate.AggregateProcessor in project camel by apache.

the class AggregateProcessorTest method testAggregateProcessorCompletionPredicate.

public void testAggregateProcessorCompletionPredicate() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedBodiesReceived("A+B+END");
    mock.expectedPropertyReceived(Exchange.AGGREGATED_COMPLETED_BY, "predicate");
    Processor done = new SendProcessor(context.getEndpoint("mock:result"));
    Expression corr = header("id");
    AggregationStrategy as = new BodyInAggregatingStrategy();
    Predicate complete = body().contains("END");
    AggregateProcessor ap = new AggregateProcessor(context, done, corr, as, executorService, true);
    ap.setCompletionPredicate(complete);
    ap.setEagerCheckCompletion(false);
    ap.start();
    Exchange e1 = new DefaultExchange(context);
    e1.getIn().setBody("A");
    e1.getIn().setHeader("id", 123);
    Exchange e2 = new DefaultExchange(context);
    e2.getIn().setBody("B");
    e2.getIn().setHeader("id", 123);
    Exchange e3 = new DefaultExchange(context);
    e3.getIn().setBody("END");
    e3.getIn().setHeader("id", 123);
    Exchange e4 = new DefaultExchange(context);
    e4.getIn().setBody("D");
    e4.getIn().setHeader("id", 123);
    ap.process(e1);
    ap.process(e2);
    ap.process(e3);
    ap.process(e4);
    assertMockEndpointsSatisfied();
    ap.stop();
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) SendProcessor(org.apache.camel.processor.SendProcessor) Processor(org.apache.camel.Processor) AggregateProcessor(org.apache.camel.processor.aggregate.AggregateProcessor) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Expression(org.apache.camel.Expression) BodyInAggregatingStrategy(org.apache.camel.processor.BodyInAggregatingStrategy) SendProcessor(org.apache.camel.processor.SendProcessor) AggregationStrategy(org.apache.camel.processor.aggregate.AggregationStrategy) Predicate(org.apache.camel.Predicate) AggregateProcessor(org.apache.camel.processor.aggregate.AggregateProcessor)

Example 7 with AggregateProcessor

use of org.apache.camel.processor.aggregate.AggregateProcessor in project camel by apache.

the class AggregateProcessorTest method testAggregateCompletionInterval.

public void testAggregateCompletionInterval() throws Exception {
    // camel context must be started
    context.start();
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedBodiesReceived("A+B+C", "D");
    mock.expectedPropertyReceived(Exchange.AGGREGATED_COMPLETED_BY, "interval");
    Processor done = new SendProcessor(context.getEndpoint("mock:result"));
    Expression corr = header("id");
    AggregationStrategy as = new BodyInAggregatingStrategy();
    AggregateProcessor ap = new AggregateProcessor(context, done, corr, as, executorService, true);
    ap.setCompletionInterval(3000);
    ap.start();
    Exchange e1 = new DefaultExchange(context);
    e1.getIn().setBody("A");
    e1.getIn().setHeader("id", 123);
    Exchange e2 = new DefaultExchange(context);
    e2.getIn().setBody("B");
    e2.getIn().setHeader("id", 123);
    Exchange e3 = new DefaultExchange(context);
    e3.getIn().setBody("C");
    e3.getIn().setHeader("id", 123);
    Exchange e4 = new DefaultExchange(context);
    e4.getIn().setBody("D");
    e4.getIn().setHeader("id", 123);
    ap.process(e1);
    ap.process(e2);
    ap.process(e3);
    Thread.sleep(5000);
    ap.process(e4);
    assertMockEndpointsSatisfied();
    ap.stop();
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) SendProcessor(org.apache.camel.processor.SendProcessor) Processor(org.apache.camel.Processor) AggregateProcessor(org.apache.camel.processor.aggregate.AggregateProcessor) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Expression(org.apache.camel.Expression) BodyInAggregatingStrategy(org.apache.camel.processor.BodyInAggregatingStrategy) SendProcessor(org.apache.camel.processor.SendProcessor) AggregationStrategy(org.apache.camel.processor.aggregate.AggregationStrategy) AggregateProcessor(org.apache.camel.processor.aggregate.AggregateProcessor)

Example 8 with AggregateProcessor

use of org.apache.camel.processor.aggregate.AggregateProcessor 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;
}
Also used : ManagedMarshal(org.apache.camel.management.mbean.ManagedMarshal) ManagedSplitter(org.apache.camel.management.mbean.ManagedSplitter) FilterProcessor(org.apache.camel.processor.FilterProcessor) UnmarshalProcessor(org.apache.camel.processor.UnmarshalProcessor) ManagedResequencer(org.apache.camel.management.mbean.ManagedResequencer) Resequencer(org.apache.camel.processor.Resequencer) StreamResequencer(org.apache.camel.processor.StreamResequencer) ManagedSetExchangePattern(org.apache.camel.management.mbean.ManagedSetExchangePattern) ManagedWeightedLoadBalancer(org.apache.camel.management.mbean.ManagedWeightedLoadBalancer) ManagedPollEnricher(org.apache.camel.management.mbean.ManagedPollEnricher) MarshalProcessor(org.apache.camel.processor.MarshalProcessor) ManagedThrottler(org.apache.camel.management.mbean.ManagedThrottler) ManagedThroughputLogger(org.apache.camel.management.mbean.ManagedThroughputLogger) ChoiceProcessor(org.apache.camel.processor.ChoiceProcessor) ManagedThrottler(org.apache.camel.management.mbean.ManagedThrottler) SamplingThrottler(org.apache.camel.processor.SamplingThrottler) Throttler(org.apache.camel.processor.Throttler) ManagedSamplingThrottler(org.apache.camel.management.mbean.ManagedSamplingThrottler) ManagedTopicLoadBalancer(org.apache.camel.management.mbean.ManagedTopicLoadBalancer) SetHeaderProcessor(org.apache.camel.processor.SetHeaderProcessor) ManagedRemoveProperties(org.apache.camel.management.mbean.ManagedRemoveProperties) ManagedRecipientList(org.apache.camel.management.mbean.ManagedRecipientList) ManagedIdempotentConsumer(org.apache.camel.management.mbean.ManagedIdempotentConsumer) ManagedValidate(org.apache.camel.management.mbean.ManagedValidate) ConvertBodyProcessor(org.apache.camel.processor.ConvertBodyProcessor) ManagedLoop(org.apache.camel.management.mbean.ManagedLoop) MulticastProcessor(org.apache.camel.processor.MulticastProcessor) ManagedRollback(org.apache.camel.management.mbean.ManagedRollback) LogEndpoint(org.apache.camel.component.log.LogEndpoint) WireTapProcessor(org.apache.camel.processor.WireTapProcessor) ManagedWireTapProcessor(org.apache.camel.management.mbean.ManagedWireTapProcessor) ManagedScript(org.apache.camel.management.mbean.ManagedScript) ScriptProcessor(org.apache.camel.processor.ScriptProcessor) ManagedSendDynamicProcessor(org.apache.camel.management.mbean.ManagedSendDynamicProcessor) PredicateValidatingProcessor(org.apache.camel.processor.validation.PredicateValidatingProcessor) WireTapProcessor(org.apache.camel.processor.WireTapProcessor) LogProcessor(org.apache.camel.processor.LogProcessor) ManagedBeanProcessor(org.apache.camel.management.mbean.ManagedBeanProcessor) StopProcessor(org.apache.camel.processor.StopProcessor) TransformProcessor(org.apache.camel.processor.TransformProcessor) BeanProcessor(org.apache.camel.component.bean.BeanProcessor) ThrowExceptionProcessor(org.apache.camel.processor.ThrowExceptionProcessor) RemoveHeadersProcessor(org.apache.camel.processor.RemoveHeadersProcessor) SendProcessor(org.apache.camel.processor.SendProcessor) MarshalProcessor(org.apache.camel.processor.MarshalProcessor) LoopProcessor(org.apache.camel.processor.LoopProcessor) SetHeaderProcessor(org.apache.camel.processor.SetHeaderProcessor) UnmarshalProcessor(org.apache.camel.processor.UnmarshalProcessor) RemoveHeaderProcessor(org.apache.camel.processor.RemoveHeaderProcessor) RemovePropertiesProcessor(org.apache.camel.processor.RemovePropertiesProcessor) MulticastProcessor(org.apache.camel.processor.MulticastProcessor) FilterProcessor(org.apache.camel.processor.FilterProcessor) SetPropertyProcessor(org.apache.camel.processor.SetPropertyProcessor) ThreadsProcessor(org.apache.camel.processor.ThreadsProcessor) ExchangePatternProcessor(org.apache.camel.processor.ExchangePatternProcessor) SetBodyProcessor(org.apache.camel.processor.SetBodyProcessor) SendDynamicProcessor(org.apache.camel.processor.SendDynamicProcessor) ManagedProcessor(org.apache.camel.management.mbean.ManagedProcessor) ChoiceProcessor(org.apache.camel.processor.ChoiceProcessor) RollbackProcessor(org.apache.camel.processor.RollbackProcessor) DelegateProcessor(org.apache.camel.DelegateProcessor) ManagedAggregateProcessor(org.apache.camel.management.mbean.ManagedAggregateProcessor) ManagedWireTapProcessor(org.apache.camel.management.mbean.ManagedWireTapProcessor) RemovePropertyProcessor(org.apache.camel.processor.RemovePropertyProcessor) Processor(org.apache.camel.Processor) ScriptProcessor(org.apache.camel.processor.ScriptProcessor) ConvertBodyProcessor(org.apache.camel.processor.ConvertBodyProcessor) ManagedSendProcessor(org.apache.camel.management.mbean.ManagedSendProcessor) AggregateProcessor(org.apache.camel.processor.aggregate.AggregateProcessor) ThreadsProcessor(org.apache.camel.processor.ThreadsProcessor) LoopProcessor(org.apache.camel.processor.LoopProcessor) SetPropertyProcessor(org.apache.camel.processor.SetPropertyProcessor) ExchangePatternProcessor(org.apache.camel.processor.ExchangePatternProcessor) ManagedRandomLoadBalancer(org.apache.camel.management.mbean.ManagedRandomLoadBalancer) ManagedSendProcessor(org.apache.camel.management.mbean.ManagedSendProcessor) ManagedCustomLoadBalancer(org.apache.camel.management.mbean.ManagedCustomLoadBalancer) RemoveHeadersProcessor(org.apache.camel.processor.RemoveHeadersProcessor) ManagedRemoveHeaders(org.apache.camel.management.mbean.ManagedRemoveHeaders) ManagedRoutingSlip(org.apache.camel.management.mbean.ManagedRoutingSlip) RoutingSlip(org.apache.camel.processor.RoutingSlip) ManagedProcessor(org.apache.camel.management.mbean.ManagedProcessor) ThrowExceptionProcessor(org.apache.camel.processor.ThrowExceptionProcessor) ManagedFilter(org.apache.camel.management.mbean.ManagedFilter) ManagedAggregateProcessor(org.apache.camel.management.mbean.ManagedAggregateProcessor) ManagedRemoveHeader(org.apache.camel.management.mbean.ManagedRemoveHeader) CustomLoadBalancerDefinition(org.apache.camel.model.loadbalancer.CustomLoadBalancerDefinition) ManagedChoice(org.apache.camel.management.mbean.ManagedChoice) RemovePropertiesProcessor(org.apache.camel.processor.RemovePropertiesProcessor) LogProcessor(org.apache.camel.processor.LogProcessor) ManagedEnricher(org.apache.camel.management.mbean.ManagedEnricher) RemovePropertyProcessor(org.apache.camel.processor.RemovePropertyProcessor) ManagedSendDynamicProcessor(org.apache.camel.management.mbean.ManagedSendDynamicProcessor) Pipeline(org.apache.camel.processor.Pipeline) ManagedProcess(org.apache.camel.management.mbean.ManagedProcess) ManagedSetProperty(org.apache.camel.management.mbean.ManagedSetProperty) ExpressionNode(org.apache.camel.model.ExpressionNode) SamplingThrottler(org.apache.camel.processor.SamplingThrottler) ManagedSamplingThrottler(org.apache.camel.management.mbean.ManagedSamplingThrottler) RollbackProcessor(org.apache.camel.processor.RollbackProcessor) ManagedThreads(org.apache.camel.management.mbean.ManagedThreads) SendProcessor(org.apache.camel.processor.SendProcessor) ManagedSendProcessor(org.apache.camel.management.mbean.ManagedSendProcessor) ManagedIdempotentConsumer(org.apache.camel.management.mbean.ManagedIdempotentConsumer) IdempotentConsumer(org.apache.camel.processor.idempotent.IdempotentConsumer) ManagedTransformer(org.apache.camel.management.mbean.ManagedTransformer) LoadBalanceDefinition(org.apache.camel.model.LoadBalanceDefinition) SetBodyProcessor(org.apache.camel.processor.SetBodyProcessor) FailOverLoadBalancer(org.apache.camel.processor.loadbalancer.FailOverLoadBalancer) ProcessDefinition(org.apache.camel.model.ProcessDefinition) ManagedDelayer(org.apache.camel.management.mbean.ManagedDelayer) CircuitBreakerLoadBalancer(org.apache.camel.processor.loadbalancer.CircuitBreakerLoadBalancer) ManagedCircuitBreakerLoadBalancer(org.apache.camel.management.mbean.ManagedCircuitBreakerLoadBalancer) Enricher(org.apache.camel.processor.Enricher) PollEnricher(org.apache.camel.processor.PollEnricher) ManagedPollEnricher(org.apache.camel.management.mbean.ManagedPollEnricher) ManagedEnricher(org.apache.camel.management.mbean.ManagedEnricher) ManagedWireTapProcessor(org.apache.camel.management.mbean.ManagedWireTapProcessor) ManagedRoutingSlip(org.apache.camel.management.mbean.ManagedRoutingSlip) ManagedRandomLoadBalancer(org.apache.camel.management.mbean.ManagedRandomLoadBalancer) RandomLoadBalancer(org.apache.camel.processor.loadbalancer.RandomLoadBalancer) ManagedSamplingThrottler(org.apache.camel.management.mbean.ManagedSamplingThrottler) ManagedSetHeader(org.apache.camel.management.mbean.ManagedSetHeader) TransformProcessor(org.apache.camel.processor.TransformProcessor) RemoveHeaderProcessor(org.apache.camel.processor.RemoveHeaderProcessor) ErrorHandler(org.apache.camel.processor.ErrorHandler) ManagedErrorHandler(org.apache.camel.management.mbean.ManagedErrorHandler) ManagedCircuitBreakerLoadBalancer(org.apache.camel.management.mbean.ManagedCircuitBreakerLoadBalancer) ManagedTopicLoadBalancer(org.apache.camel.management.mbean.ManagedTopicLoadBalancer) ManagedWeightedLoadBalancer(org.apache.camel.management.mbean.ManagedWeightedLoadBalancer) CircuitBreakerLoadBalancer(org.apache.camel.processor.loadbalancer.CircuitBreakerLoadBalancer) StickyLoadBalancer(org.apache.camel.processor.loadbalancer.StickyLoadBalancer) WeightedLoadBalancer(org.apache.camel.processor.loadbalancer.WeightedLoadBalancer) ManagedRandomLoadBalancer(org.apache.camel.management.mbean.ManagedRandomLoadBalancer) ManagedCustomLoadBalancer(org.apache.camel.management.mbean.ManagedCustomLoadBalancer) RoundRobinLoadBalancer(org.apache.camel.processor.loadbalancer.RoundRobinLoadBalancer) TopicLoadBalancer(org.apache.camel.processor.loadbalancer.TopicLoadBalancer) ManagedFailoverLoadBalancer(org.apache.camel.management.mbean.ManagedFailoverLoadBalancer) FailOverLoadBalancer(org.apache.camel.processor.loadbalancer.FailOverLoadBalancer) LoadBalancer(org.apache.camel.processor.loadbalancer.LoadBalancer) ManagedStickyLoadBalancer(org.apache.camel.management.mbean.ManagedStickyLoadBalancer) ManagedCircuitBreakerLoadBalancer(org.apache.camel.management.mbean.ManagedCircuitBreakerLoadBalancer) RandomLoadBalancer(org.apache.camel.processor.loadbalancer.RandomLoadBalancer) ManagedRoundRobinLoadBalancer(org.apache.camel.management.mbean.ManagedRoundRobinLoadBalancer) ManagedWeightedLoadBalancer(org.apache.camel.management.mbean.ManagedWeightedLoadBalancer) WeightedLoadBalancer(org.apache.camel.processor.loadbalancer.WeightedLoadBalancer) ManagedRemoveProperty(org.apache.camel.management.mbean.ManagedRemoveProperty) ManagedAggregateProcessor(org.apache.camel.management.mbean.ManagedAggregateProcessor) AggregateProcessor(org.apache.camel.processor.aggregate.AggregateProcessor) ManagedStickyLoadBalancer(org.apache.camel.management.mbean.ManagedStickyLoadBalancer) StreamResequencer(org.apache.camel.processor.StreamResequencer) ManagedThroughputLogger(org.apache.camel.management.mbean.ManagedThroughputLogger) ThroughputLogger(org.apache.camel.processor.ThroughputLogger) PredicateValidatingProcessor(org.apache.camel.processor.validation.PredicateValidatingProcessor) ManagedResequencer(org.apache.camel.management.mbean.ManagedResequencer) ManagedLog(org.apache.camel.management.mbean.ManagedLog) RoundRobinLoadBalancer(org.apache.camel.processor.loadbalancer.RoundRobinLoadBalancer) ManagedRoundRobinLoadBalancer(org.apache.camel.management.mbean.ManagedRoundRobinLoadBalancer) ManagedRecipientList(org.apache.camel.management.mbean.ManagedRecipientList) RecipientList(org.apache.camel.processor.RecipientList) ManagedConvertBody(org.apache.camel.management.mbean.ManagedConvertBody) PollEnricher(org.apache.camel.processor.PollEnricher) ManagedPollEnricher(org.apache.camel.management.mbean.ManagedPollEnricher) Iterator(java.util.Iterator) ManagedMulticast(org.apache.camel.management.mbean.ManagedMulticast) ManagedStop(org.apache.camel.management.mbean.ManagedStop) RecipientListDefinition(org.apache.camel.model.RecipientListDefinition) ManagedSplitter(org.apache.camel.management.mbean.ManagedSplitter) Splitter(org.apache.camel.processor.Splitter) StopProcessor(org.apache.camel.processor.StopProcessor) ManagedFailoverLoadBalancer(org.apache.camel.management.mbean.ManagedFailoverLoadBalancer) ManagedBeanProcessor(org.apache.camel.management.mbean.ManagedBeanProcessor) BeanProcessor(org.apache.camel.component.bean.BeanProcessor) ThreadsDefinition(org.apache.camel.model.ThreadsDefinition) StickyLoadBalancer(org.apache.camel.processor.loadbalancer.StickyLoadBalancer) ManagedStickyLoadBalancer(org.apache.camel.management.mbean.ManagedStickyLoadBalancer) ManagedDynamicRouter(org.apache.camel.management.mbean.ManagedDynamicRouter) DynamicRouter(org.apache.camel.processor.DynamicRouter) ManagedDelayer(org.apache.camel.management.mbean.ManagedDelayer) Delayer(org.apache.camel.processor.Delayer) ManagedSendDynamicProcessor(org.apache.camel.management.mbean.ManagedSendDynamicProcessor) SendDynamicProcessor(org.apache.camel.processor.SendDynamicProcessor) ManagedBeanProcessor(org.apache.camel.management.mbean.ManagedBeanProcessor) ManagedTopicLoadBalancer(org.apache.camel.management.mbean.ManagedTopicLoadBalancer) TopicLoadBalancer(org.apache.camel.processor.loadbalancer.TopicLoadBalancer) ManagedDynamicRouter(org.apache.camel.management.mbean.ManagedDynamicRouter) ManagedUnmarshal(org.apache.camel.management.mbean.ManagedUnmarshal) DelegateProcessor(org.apache.camel.DelegateProcessor) ManagedThrowException(org.apache.camel.management.mbean.ManagedThrowException) ManagedRoundRobinLoadBalancer(org.apache.camel.management.mbean.ManagedRoundRobinLoadBalancer) ManagedSetBody(org.apache.camel.management.mbean.ManagedSetBody)

Example 9 with AggregateProcessor

use of org.apache.camel.processor.aggregate.AggregateProcessor in project camel by apache.

the class AggregateDefinition method createAggregator.

protected AggregateProcessor createAggregator(RouteContext routeContext) throws Exception {
    Processor childProcessor = this.createChildProcessor(routeContext, true);
    // wrap the aggregate route in a unit of work processor
    CamelInternalProcessor internal = new CamelInternalProcessor(childProcessor);
    internal.addAdvice(new CamelInternalProcessor.UnitOfWorkProcessorAdvice(routeContext));
    Expression correlation = getExpression().createExpression(routeContext);
    AggregationStrategy strategy = createAggregationStrategy(routeContext);
    boolean parallel = getParallelProcessing() != null && getParallelProcessing();
    boolean shutdownThreadPool = ProcessorDefinitionHelper.willCreateNewThreadPool(routeContext, this, parallel);
    ExecutorService threadPool = ProcessorDefinitionHelper.getConfiguredExecutorService(routeContext, "Aggregator", this, parallel);
    if (threadPool == null && !parallel) {
        // executor service is mandatory for the Aggregator
        // we do not run in parallel mode, but use a synchronous executor, so we run in current thread
        threadPool = new SynchronousExecutorService();
        shutdownThreadPool = true;
    }
    AggregateProcessor answer = new AggregateProcessor(routeContext.getCamelContext(), internal, correlation, strategy, threadPool, shutdownThreadPool);
    AggregationRepository repository = createAggregationRepository(routeContext);
    if (repository != null) {
        answer.setAggregationRepository(repository);
    }
    if (getAggregateController() == null && getAggregateControllerRef() != null) {
        setAggregateController(routeContext.mandatoryLookup(getAggregateControllerRef(), AggregateController.class));
    }
    // this EIP supports using a shared timeout checker thread pool or fallback to create a new thread pool
    boolean shutdownTimeoutThreadPool = false;
    ScheduledExecutorService timeoutThreadPool = timeoutCheckerExecutorService;
    if (timeoutThreadPool == null && timeoutCheckerExecutorServiceRef != null) {
        // lookup existing thread pool
        timeoutThreadPool = routeContext.getCamelContext().getRegistry().lookupByNameAndType(timeoutCheckerExecutorServiceRef, ScheduledExecutorService.class);
        if (timeoutThreadPool == null) {
            // then create a thread pool assuming the ref is a thread pool profile id
            timeoutThreadPool = routeContext.getCamelContext().getExecutorServiceManager().newScheduledThreadPool(this, AggregateProcessor.AGGREGATE_TIMEOUT_CHECKER, timeoutCheckerExecutorServiceRef);
            if (timeoutThreadPool == null) {
                throw new IllegalArgumentException("ExecutorServiceRef " + timeoutCheckerExecutorServiceRef + " not found in registry or as a thread pool profile.");
            }
            shutdownTimeoutThreadPool = true;
        }
    }
    answer.setTimeoutCheckerExecutorService(timeoutThreadPool);
    answer.setShutdownTimeoutCheckerExecutorService(shutdownTimeoutThreadPool);
    // set other options
    answer.setParallelProcessing(parallel);
    if (getOptimisticLocking() != null) {
        answer.setOptimisticLocking(getOptimisticLocking());
    }
    if (getCompletionPredicate() != null) {
        Predicate predicate = getCompletionPredicate().createPredicate(routeContext);
        answer.setCompletionPredicate(predicate);
    } else if (strategy instanceof Predicate) {
        // if aggregation strategy implements predicate and was not configured then use as fallback
        log.debug("Using AggregationStrategy as completion predicate: {}", strategy);
        answer.setCompletionPredicate((Predicate) strategy);
    }
    if (getCompletionTimeoutExpression() != null) {
        Expression expression = getCompletionTimeoutExpression().createExpression(routeContext);
        answer.setCompletionTimeoutExpression(expression);
    }
    if (getCompletionTimeout() != null) {
        answer.setCompletionTimeout(getCompletionTimeout());
    }
    if (getCompletionInterval() != null) {
        answer.setCompletionInterval(getCompletionInterval());
    }
    if (getCompletionSizeExpression() != null) {
        Expression expression = getCompletionSizeExpression().createExpression(routeContext);
        answer.setCompletionSizeExpression(expression);
    }
    if (getCompletionSize() != null) {
        answer.setCompletionSize(getCompletionSize());
    }
    if (getCompletionFromBatchConsumer() != null) {
        answer.setCompletionFromBatchConsumer(getCompletionFromBatchConsumer());
    }
    if (getEagerCheckCompletion() != null) {
        answer.setEagerCheckCompletion(getEagerCheckCompletion());
    }
    if (getIgnoreInvalidCorrelationKeys() != null) {
        answer.setIgnoreInvalidCorrelationKeys(getIgnoreInvalidCorrelationKeys());
    }
    if (getCloseCorrelationKeyOnCompletion() != null) {
        answer.setCloseCorrelationKeyOnCompletion(getCloseCorrelationKeyOnCompletion());
    }
    if (getDiscardOnCompletionTimeout() != null) {
        answer.setDiscardOnCompletionTimeout(getDiscardOnCompletionTimeout());
    }
    if (getForceCompletionOnStop() != null) {
        answer.setForceCompletionOnStop(getForceCompletionOnStop());
    }
    if (getCompleteAllOnStop() != null) {
        answer.setCompleteAllOnStop(getCompleteAllOnStop());
    }
    if (optimisticLockRetryPolicy == null) {
        if (getOptimisticLockRetryPolicyDefinition() != null) {
            answer.setOptimisticLockRetryPolicy(getOptimisticLockRetryPolicyDefinition().createOptimisticLockRetryPolicy());
        }
    } else {
        answer.setOptimisticLockRetryPolicy(optimisticLockRetryPolicy);
    }
    if (getAggregateController() != null) {
        answer.setAggregateController(getAggregateController());
    }
    return answer;
}
Also used : SynchronousExecutorService(org.apache.camel.util.concurrent.SynchronousExecutorService) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) CamelInternalProcessor(org.apache.camel.processor.CamelInternalProcessor) Processor(org.apache.camel.Processor) AggregateProcessor(org.apache.camel.processor.aggregate.AggregateProcessor) AggregateController(org.apache.camel.processor.aggregate.AggregateController) AggregateProcessor(org.apache.camel.processor.aggregate.AggregateProcessor) AsPredicate(org.apache.camel.spi.AsPredicate) Predicate(org.apache.camel.Predicate) CamelInternalProcessor(org.apache.camel.processor.CamelInternalProcessor) Expression(org.apache.camel.Expression) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) SynchronousExecutorService(org.apache.camel.util.concurrent.SynchronousExecutorService) AggregationRepository(org.apache.camel.spi.AggregationRepository) GroupedExchangeAggregationStrategy(org.apache.camel.processor.aggregate.GroupedExchangeAggregationStrategy) AggregationStrategy(org.apache.camel.processor.aggregate.AggregationStrategy)

Example 10 with AggregateProcessor

use of org.apache.camel.processor.aggregate.AggregateProcessor in project camel by apache.

the class AggregateProcessorTest method testAggregateUseBatchSizeFromConsumer.

public void testAggregateUseBatchSizeFromConsumer() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedBodiesReceived("A+B", "C+D+E");
    mock.expectedPropertyReceived(Exchange.AGGREGATED_COMPLETED_BY, "consumer");
    Processor done = new SendProcessor(context.getEndpoint("mock:result"));
    Expression corr = header("id");
    AggregationStrategy as = new BodyInAggregatingStrategy();
    AggregateProcessor ap = new AggregateProcessor(context, done, corr, as, executorService, true);
    ap.setCompletionSize(100);
    ap.setCompletionFromBatchConsumer(true);
    ap.start();
    Exchange e1 = new DefaultExchange(context);
    e1.getIn().setBody("A");
    e1.getIn().setHeader("id", 123);
    e1.setProperty(Exchange.BATCH_INDEX, 0);
    e1.setProperty(Exchange.BATCH_SIZE, 2);
    e1.setProperty(Exchange.BATCH_COMPLETE, false);
    Exchange e2 = new DefaultExchange(context);
    e2.getIn().setBody("B");
    e2.getIn().setHeader("id", 123);
    e2.setProperty(Exchange.BATCH_INDEX, 1);
    e2.setProperty(Exchange.BATCH_SIZE, 2);
    e2.setProperty(Exchange.BATCH_COMPLETE, true);
    Exchange e3 = new DefaultExchange(context);
    e3.getIn().setBody("C");
    e3.getIn().setHeader("id", 123);
    e3.setProperty(Exchange.BATCH_INDEX, 0);
    e3.setProperty(Exchange.BATCH_SIZE, 3);
    e3.setProperty(Exchange.BATCH_COMPLETE, false);
    Exchange e4 = new DefaultExchange(context);
    e4.getIn().setBody("D");
    e4.getIn().setHeader("id", 123);
    e4.setProperty(Exchange.BATCH_INDEX, 1);
    e4.setProperty(Exchange.BATCH_SIZE, 3);
    e4.setProperty(Exchange.BATCH_COMPLETE, false);
    Exchange e5 = new DefaultExchange(context);
    e5.getIn().setBody("E");
    e5.getIn().setHeader("id", 123);
    e5.setProperty(Exchange.BATCH_INDEX, 2);
    e5.setProperty(Exchange.BATCH_SIZE, 3);
    e5.setProperty(Exchange.BATCH_COMPLETE, true);
    ap.process(e1);
    ap.process(e2);
    ap.process(e3);
    ap.process(e4);
    ap.process(e5);
    assertMockEndpointsSatisfied();
    ap.stop();
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) SendProcessor(org.apache.camel.processor.SendProcessor) Processor(org.apache.camel.Processor) AggregateProcessor(org.apache.camel.processor.aggregate.AggregateProcessor) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Expression(org.apache.camel.Expression) BodyInAggregatingStrategy(org.apache.camel.processor.BodyInAggregatingStrategy) SendProcessor(org.apache.camel.processor.SendProcessor) AggregationStrategy(org.apache.camel.processor.aggregate.AggregationStrategy) AggregateProcessor(org.apache.camel.processor.aggregate.AggregateProcessor)

Aggregations

Processor (org.apache.camel.Processor)16 AggregateProcessor (org.apache.camel.processor.aggregate.AggregateProcessor)16 Expression (org.apache.camel.Expression)15 SendProcessor (org.apache.camel.processor.SendProcessor)15 AggregationStrategy (org.apache.camel.processor.aggregate.AggregationStrategy)15 Exchange (org.apache.camel.Exchange)14 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)14 DefaultExchange (org.apache.camel.impl.DefaultExchange)14 BodyInAggregatingStrategy (org.apache.camel.processor.BodyInAggregatingStrategy)14 Predicate (org.apache.camel.Predicate)6 Iterator (java.util.Iterator)1 ExecutorService (java.util.concurrent.ExecutorService)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 DelegateProcessor (org.apache.camel.DelegateProcessor)1 BeanProcessor (org.apache.camel.component.bean.BeanProcessor)1 LogEndpoint (org.apache.camel.component.log.LogEndpoint)1 ManagedAggregateProcessor (org.apache.camel.management.mbean.ManagedAggregateProcessor)1 ManagedBeanProcessor (org.apache.camel.management.mbean.ManagedBeanProcessor)1 ManagedChoice (org.apache.camel.management.mbean.ManagedChoice)1 ManagedCircuitBreakerLoadBalancer (org.apache.camel.management.mbean.ManagedCircuitBreakerLoadBalancer)1