Search in sources :

Example 21 with SendProcessor

use of org.apache.camel.processor.SendProcessor in project camel by apache.

the class AggregateProcessorTest method testAggregateBadCorrelationKey.

public void testAggregateBadCorrelationKey() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedBodiesReceived("A+C+END");
    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.start();
    Exchange e1 = new DefaultExchange(context);
    e1.getIn().setBody("A");
    e1.getIn().setHeader("id", 123);
    Exchange e2 = new DefaultExchange(context);
    e2.getIn().setBody("B");
    Exchange e3 = new DefaultExchange(context);
    e3.getIn().setBody("C");
    e3.getIn().setHeader("id", 123);
    Exchange e4 = new DefaultExchange(context);
    e4.getIn().setBody("END");
    e4.getIn().setHeader("id", 123);
    ap.process(e1);
    ap.process(e2);
    Exception e = e2.getException();
    assertNotNull(e);
    assertTrue(e.getMessage().startsWith("Invalid correlation key."));
    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 22 with SendProcessor

use of org.apache.camel.processor.SendProcessor in project camel by apache.

the class AggregateProcessorTest method testAggregateCloseCorrelationKeyOnCompletion.

public void testAggregateCloseCorrelationKeyOnCompletion() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedBodiesReceived("A+B+END");
    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.setCloseCorrelationKeyOnCompletion(1000);
    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("C");
    e4.getIn().setHeader("id", 123);
    ap.process(e1);
    ap.process(e2);
    ap.process(e3);
    ap.process(e4);
    Exception e = e4.getException();
    assertNotNull(e);
    assertTrue(e.getMessage().startsWith("The correlation key [123] has been closed."));
    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 23 with SendProcessor

use of org.apache.camel.processor.SendProcessor in project camel by apache.

the class AggregateProcessorTest method doTestAggregateProcessorCompletionAggregatedSize.

private void doTestAggregateProcessorCompletionAggregatedSize(boolean eager) throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedBodiesReceived("A+B+C");
    mock.expectedPropertyReceived(Exchange.AGGREGATED_COMPLETED_BY, "size");
    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(3);
    ap.setEagerCheckCompletion(eager);
    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);
    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 24 with SendProcessor

use of org.apache.camel.processor.SendProcessor in project camel by apache.

the class AggregateProcessorTimeoutCompletionRestartTest method testAggregateProcessorTwoTimeoutExpressionRestart.

public void testAggregateProcessorTwoTimeoutExpressionRestart() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedBodiesReceived("C+D", "A+B");
    mock.expectedPropertyReceived(Exchange.AGGREGATED_COMPLETED_BY, "timeout");
    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);
    // start with a high timeout so no completes before we stop
    ap.setCompletionTimeoutExpression(header("myTimeout"));
    ap.start();
    Exchange e1 = new DefaultExchange(context);
    e1.getIn().setBody("A");
    e1.getIn().setHeader("id", 123);
    e1.getIn().setHeader("myTimeout", 3000);
    Exchange e2 = new DefaultExchange(context);
    e2.getIn().setBody("B");
    e2.getIn().setHeader("id", 123);
    e2.getIn().setHeader("myTimeout", 3000);
    Exchange e3 = new DefaultExchange(context);
    e3.getIn().setBody("C");
    e3.getIn().setHeader("id", 456);
    e3.getIn().setHeader("myTimeout", 2000);
    Exchange e4 = new DefaultExchange(context);
    e4.getIn().setBody("D");
    e4.getIn().setHeader("id", 456);
    e4.getIn().setHeader("myTimeout", 2000);
    ap.process(e1);
    ap.process(e2);
    ap.process(e3);
    ap.process(e4);
    // shutdown before the 2 sec timeout occurs
    // however we use stop instead of shutdown as shutdown will clear the in memory aggregation repository,
    ap.stop();
    // should be no completed
    assertEquals(0, mock.getReceivedCounter());
    // start aggregator again
    ap.start();
    // the aggregator should restore the timeout condition and trigger timeout
    assertMockEndpointsSatisfied();
    assertEquals(2, mock.getReceivedCounter());
    ap.shutdown();
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) Processor(org.apache.camel.Processor) SendProcessor(org.apache.camel.processor.SendProcessor) 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 25 with SendProcessor

use of org.apache.camel.processor.SendProcessor in project camel by apache.

the class TransformerBuilderTest method testEndpointTransformer.

public void testEndpointTransformer() throws Exception {
    CamelContext ctx = new DefaultCamelContext();
    RouteBuilder builder = new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            transformer().fromType("json:foo").toType("xml:bar").withUri("direct:transformer");
            from("direct:transformer").log("test");
        }
    };
    ctx.addRoutes(builder);
    ctx.start();
    Transformer transformer = ctx.resolveTransformer(new DataType("json:foo"), new DataType("xml:bar"));
    assertNotNull(transformer);
    assertEquals(ProcessorTransformer.class, transformer.getClass());
    ProcessorTransformer pt = (ProcessorTransformer) transformer;
    Field f = ProcessorTransformer.class.getDeclaredField("processor");
    f.setAccessible(true);
    Object processor = f.get(pt);
    assertEquals(SendProcessor.class, processor.getClass());
    SendProcessor sp = (SendProcessor) processor;
    assertEquals("direct://transformer", sp.getEndpoint().getEndpointUri());
}
Also used : DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) CamelContext(org.apache.camel.CamelContext) Field(java.lang.reflect.Field) ProcessorTransformer(org.apache.camel.impl.transformer.ProcessorTransformer) Transformer(org.apache.camel.spi.Transformer) DataFormatTransformer(org.apache.camel.impl.transformer.DataFormatTransformer) DataType(org.apache.camel.spi.DataType) SendProcessor(org.apache.camel.processor.SendProcessor) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) ProcessorTransformer(org.apache.camel.impl.transformer.ProcessorTransformer)

Aggregations

SendProcessor (org.apache.camel.processor.SendProcessor)27 Processor (org.apache.camel.Processor)17 AggregateProcessor (org.apache.camel.processor.aggregate.AggregateProcessor)15 Exchange (org.apache.camel.Exchange)14 Expression (org.apache.camel.Expression)14 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)14 DefaultExchange (org.apache.camel.impl.DefaultExchange)14 BodyInAggregatingStrategy (org.apache.camel.processor.BodyInAggregatingStrategy)14 AggregationStrategy (org.apache.camel.processor.aggregate.AggregationStrategy)14 Endpoint (org.apache.camel.Endpoint)7 Predicate (org.apache.camel.Predicate)5 Channel (org.apache.camel.Channel)4 Route (org.apache.camel.Route)4 EventDrivenConsumerRoute (org.apache.camel.impl.EventDrivenConsumerRoute)4 DeadLetterChannel (org.apache.camel.processor.DeadLetterChannel)4 ProcessorTransformer (org.apache.camel.impl.transformer.ProcessorTransformer)2 Field (java.lang.reflect.Field)1 Iterator (java.util.Iterator)1 ExecutorService (java.util.concurrent.ExecutorService)1 CamelContext (org.apache.camel.CamelContext)1