Search in sources :

Example 56 with RouteBuilder

use of org.apache.camel.builder.RouteBuilder in project camel by apache.

the class BeanCallTest method beanCallHeaderMappingTest.

@Test
public void beanCallHeaderMappingTest() throws Exception {
    new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            onException(Throwable.class).to("direct:handle").handled(true);
            from("direct:num").bean(BeanCallTest.this, "processHeader").process(// Can be removed?
            new UnwrapStreamProcessor()).to("mock:endpoint");
            from("direct:handle").setBody().constant("ERR").to("mock:endpoint");
        }
    }.addRoutesToCamelContext(context);
    MockEndpoint mock = getMockEndpoint("mock:endpoint");
    mock.expectedMessageCount(1);
    context.start();
    template.sendBodyAndHeader("direct:num", 1, "myheader", 2);
    mock.assertIsSatisfied();
    Exchange exchange = mock.getExchanges().get(0);
    assertEquals("HelloHeader 2", exchange.getIn().getBody());
}
Also used : Exchange(org.apache.camel.Exchange) UnwrapStreamProcessor(org.apache.camel.component.reactive.streams.util.UnwrapStreamProcessor) RouteBuilder(org.apache.camel.builder.RouteBuilder) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Test(org.junit.Test)

Example 57 with RouteBuilder

use of org.apache.camel.builder.RouteBuilder in project camel by apache.

the class BackpressureStrategyTest method testBackpressureBufferStrategy.

@Test
public void testBackpressureBufferStrategy() throws Exception {
    new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("timer:gen?period=20&repeatCount=20").setBody().header(Exchange.TIMER_COUNTER).to("reactive-streams:integers");
        }
    }.addRoutesToCamelContext(context);
    Flowable<Integer> integers = Flowable.fromPublisher(CamelReactiveStreams.get(context).fromStream("integers", Integer.class));
    ConcurrentLinkedQueue<Integer> queue = new ConcurrentLinkedQueue<>();
    CountDownLatch latch = new CountDownLatch(1);
    Flowable.interval(0, 50, TimeUnit.MILLISECONDS).zipWith(integers, (l, i) -> i).timeout(2000, TimeUnit.MILLISECONDS, Flowable.empty()).doOnComplete(latch::countDown).subscribe(queue::add);
    context().start();
    assertTrue(latch.await(5, TimeUnit.SECONDS));
    assertEquals(20, queue.size());
    int num = 1;
    for (int i : queue) {
        assertEquals(num++, i);
    }
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) TestSubscriber(org.apache.camel.component.reactive.streams.support.TestSubscriber) CountDownLatch(java.util.concurrent.CountDownLatch) CamelReactiveStreams(org.apache.camel.component.reactive.streams.api.CamelReactiveStreams) Flowable(io.reactivex.Flowable) RouteBuilder(org.apache.camel.builder.RouteBuilder) Exchange(org.apache.camel.Exchange) Test(org.junit.Test) CamelTestSupport(org.apache.camel.test.junit4.CamelTestSupport) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) RouteBuilder(org.apache.camel.builder.RouteBuilder) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 58 with RouteBuilder

use of org.apache.camel.builder.RouteBuilder in project camel by apache.

the class BackpressureStrategyTest method testBackpressureDropStrategy.

@Test
public void testBackpressureDropStrategy() throws Exception {
    ReactiveStreamsComponent comp = (ReactiveStreamsComponent) context().getComponent("reactive-streams");
    comp.setBackpressureStrategy(ReactiveStreamsBackpressureStrategy.DROP);
    new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("timer:gen?period=20&repeatCount=20").setBody().header(Exchange.TIMER_COUNTER).to("reactive-streams:integers");
        }
    }.addRoutesToCamelContext(context);
    ConcurrentLinkedQueue<Integer> queue = new ConcurrentLinkedQueue<>();
    final CountDownLatch latch = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(2);
    TestSubscriber<Integer> subscriber = new TestSubscriber<Integer>() {

        @Override
        public void onNext(Integer o) {
            queue.add(o);
            latch.countDown();
            latch2.countDown();
        }
    };
    subscriber.setInitiallyRequested(1);
    CamelReactiveStreams.get(context).fromStream("integers", Integer.class).subscribe(subscriber);
    context().start();
    assertTrue(latch.await(5, TimeUnit.SECONDS));
    // wait for all numbers to be generated
    Thread.sleep(1000);
    subscriber.request(19);
    assertTrue(latch2.await(1, TimeUnit.SECONDS));
    // add other time to ensure no other items arrive
    Thread.sleep(200);
    assertEquals(2, queue.size());
    int sum = queue.stream().reduce((i, j) -> i + j).get();
    // 1 + 2 = 3
    assertEquals(3, sum);
    subscriber.cancel();
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) TestSubscriber(org.apache.camel.component.reactive.streams.support.TestSubscriber) CountDownLatch(java.util.concurrent.CountDownLatch) CamelReactiveStreams(org.apache.camel.component.reactive.streams.api.CamelReactiveStreams) Flowable(io.reactivex.Flowable) RouteBuilder(org.apache.camel.builder.RouteBuilder) Exchange(org.apache.camel.Exchange) Test(org.junit.Test) CamelTestSupport(org.apache.camel.test.junit4.CamelTestSupport) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) RouteBuilder(org.apache.camel.builder.RouteBuilder) TestSubscriber(org.apache.camel.component.reactive.streams.support.TestSubscriber) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 59 with RouteBuilder

use of org.apache.camel.builder.RouteBuilder in project camel by apache.

the class BackpressureStrategyTest method testBackpressureDropStrategyInEndpoint.

@Test
public void testBackpressureDropStrategyInEndpoint() throws Exception {
    new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("timer:gen?period=20&repeatCount=20").setBody().header(Exchange.TIMER_COUNTER).to("reactive-streams:integers?backpressureStrategy=DROP");
        }
    }.addRoutesToCamelContext(context);
    ConcurrentLinkedQueue<Integer> queue = new ConcurrentLinkedQueue<>();
    final CountDownLatch latch = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(2);
    TestSubscriber<Integer> subscriber = new TestSubscriber<Integer>() {

        @Override
        public void onNext(Integer o) {
            queue.add(o);
            latch.countDown();
            latch2.countDown();
        }
    };
    subscriber.setInitiallyRequested(1);
    CamelReactiveStreams.get(context).fromStream("integers", Integer.class).subscribe(subscriber);
    context().start();
    assertTrue(latch.await(5, TimeUnit.SECONDS));
    // wait for all numbers to be generated
    Thread.sleep(1000);
    subscriber.request(19);
    assertTrue(latch2.await(1, TimeUnit.SECONDS));
    // add other time to ensure no other items arrive
    Thread.sleep(200);
    assertEquals(2, queue.size());
    int sum = queue.stream().reduce((i, j) -> i + j).get();
    // 1 + 2 = 3
    assertEquals(3, sum);
    subscriber.cancel();
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) TestSubscriber(org.apache.camel.component.reactive.streams.support.TestSubscriber) CountDownLatch(java.util.concurrent.CountDownLatch) CamelReactiveStreams(org.apache.camel.component.reactive.streams.api.CamelReactiveStreams) Flowable(io.reactivex.Flowable) RouteBuilder(org.apache.camel.builder.RouteBuilder) Exchange(org.apache.camel.Exchange) Test(org.junit.Test) CamelTestSupport(org.apache.camel.test.junit4.CamelTestSupport) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) RouteBuilder(org.apache.camel.builder.RouteBuilder) TestSubscriber(org.apache.camel.component.reactive.streams.support.TestSubscriber) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 60 with RouteBuilder

use of org.apache.camel.builder.RouteBuilder in project camel by apache.

the class BackpressureStrategyTest method testBackpressureLatestStrategy.

@Test
public void testBackpressureLatestStrategy() throws Exception {
    ReactiveStreamsComponent comp = (ReactiveStreamsComponent) context().getComponent("reactive-streams");
    comp.setBackpressureStrategy(ReactiveStreamsBackpressureStrategy.LATEST);
    new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("timer:gen?period=20&repeatCount=20").setBody().header(Exchange.TIMER_COUNTER).to("reactive-streams:integers");
        }
    }.addRoutesToCamelContext(context);
    ConcurrentLinkedQueue<Integer> queue = new ConcurrentLinkedQueue<>();
    final CountDownLatch latch = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(2);
    TestSubscriber<Integer> subscriber = new TestSubscriber<Integer>() {

        @Override
        public void onNext(Integer o) {
            queue.add(o);
            latch.countDown();
            latch2.countDown();
        }
    };
    subscriber.setInitiallyRequested(1);
    CamelReactiveStreams.get(context).fromStream("integers", Integer.class).subscribe(subscriber);
    context().start();
    assertTrue(latch.await(5, TimeUnit.SECONDS));
    // wait for all numbers to be generated
    Thread.sleep(1000);
    subscriber.request(19);
    assertTrue(latch2.await(1, TimeUnit.SECONDS));
    // add other time to ensure no other items arrive
    Thread.sleep(200);
    assertEquals(2, queue.size());
    int sum = queue.stream().reduce((i, j) -> i + j).get();
    // 1 + 20 = 21
    assertEquals(21, sum);
    subscriber.cancel();
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) TestSubscriber(org.apache.camel.component.reactive.streams.support.TestSubscriber) CountDownLatch(java.util.concurrent.CountDownLatch) CamelReactiveStreams(org.apache.camel.component.reactive.streams.api.CamelReactiveStreams) Flowable(io.reactivex.Flowable) RouteBuilder(org.apache.camel.builder.RouteBuilder) Exchange(org.apache.camel.Exchange) Test(org.junit.Test) CamelTestSupport(org.apache.camel.test.junit4.CamelTestSupport) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) RouteBuilder(org.apache.camel.builder.RouteBuilder) TestSubscriber(org.apache.camel.component.reactive.streams.support.TestSubscriber) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

RouteBuilder (org.apache.camel.builder.RouteBuilder)1759 Exchange (org.apache.camel.Exchange)628 Processor (org.apache.camel.Processor)545 Test (org.junit.Test)476 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)341 CamelExecutionException (org.apache.camel.CamelExecutionException)135 FailedToCreateRouteException (org.apache.camel.FailedToCreateRouteException)119 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)104 File (java.io.File)68 CamelContext (org.apache.camel.CamelContext)64 IOException (java.io.IOException)61 ResolveEndpointFailedException (org.apache.camel.ResolveEndpointFailedException)42 HashMap (java.util.HashMap)35 Path (org.apache.hadoop.fs.Path)34 CountDownLatch (java.util.concurrent.CountDownLatch)32 Configuration (org.apache.hadoop.conf.Configuration)32 Endpoint (org.apache.camel.Endpoint)30 ArrayFile (org.apache.hadoop.io.ArrayFile)30 SequenceFile (org.apache.hadoop.io.SequenceFile)30 RuntimeCamelException (org.apache.camel.RuntimeCamelException)26