Java Code Examples for org.apache.camel.component.reactive.streams.api.CamelReactiveStreams#get()

The following examples show how to use org.apache.camel.component.reactive.streams.api.CamelReactiveStreams#get() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ReactorIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void testToStream() throws Exception {
    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        public void configure() {
            from("reactive-streams:reactive")
                .setBody().constant("123");
        }
    });

    camelctx.start();
    try {
        CamelReactiveStreamsService crs = CamelReactiveStreams.get(camelctx);
        Publisher<Exchange> publisher = crs.toStream("reactive", new DefaultExchange(camelctx));
        Exchange res = Flux.from(publisher).blockFirst();

        Assert.assertNotNull(res);

        String content = res.getIn().getBody(String.class);

        Assert.assertNotNull(content);
        Assert.assertEquals("123", content);
    } finally {
        camelctx.close();
    }
}
 
Example 2
Source File: ReactorIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void testToFunctionWithExchange() throws Exception {
    CamelContext camelctx = createWildFlyCamelContext();
    camelctx.start();
    try {
        CamelReactiveStreamsService crs = CamelReactiveStreams.get(camelctx);
        Set<String> values = Collections.synchronizedSet(new TreeSet<>());
        CountDownLatch latch = new CountDownLatch(3);
        Function<Object, Publisher<Exchange>> fun = crs.to("bean:hello");

        Flux.just(1, 2, 3)
            .flatMap(fun)
            .map(e -> e.getOut())
            .map(e -> e.getBody(String.class))
            .doOnNext(res -> values.add(res))
            .doOnNext(res -> latch.countDown())
            .subscribe();

        Assert.assertTrue(latch.await(2, TimeUnit.SECONDS));
        Assert.assertEquals(new TreeSet<>(Arrays.asList("Hello 1", "Hello 2", "Hello 3")), values);
    } finally {
        camelctx.close();
    }
}
 
Example 3
Source File: ReactorIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void testToFunction() throws Exception {
    CamelContext camelctx = createWildFlyCamelContext();
    camelctx.start();
    try {
        CamelReactiveStreamsService crs = CamelReactiveStreams.get(camelctx);

        /* A TreeSet will order the messages alphabetically regardless of the insertion order
         * This is important because in the Flux returned by Flux.flatMap(Function<? super T, ? extends
         * Publisher<? extends R>>) the emissions may interleave */
        Set<String> values = Collections.synchronizedSet(new TreeSet<>());
        CountDownLatch latch = new CountDownLatch(3);
        Function<Object, Publisher<String>> fun = crs.to("bean:hello", String.class);

        Flux.just(1, 2, 3)
            .flatMap(fun)
            .doOnNext(res -> values.add(res))
            .doOnNext(res -> latch.countDown())
            .subscribe();

        Assert.assertTrue(latch.await(2, TimeUnit.SECONDS));
        Assert.assertEquals(new TreeSet<>(Arrays.asList("Hello 1", "Hello 2", "Hello 3")), values);
    } finally {
        camelctx.close();
    }
}
 
Example 4
Source File: QuickStart.java    From smallrye-reactive-streams-operators with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    CamelContext context = new DefaultCamelContext();
    CamelReactiveStreamsService camel = CamelReactiveStreams.get(context);
    Subscriber<String> subscriber = camel
            .subscriber("file:./target?fileName=values.txt&fileExist=append", String.class);

    ReactiveStreams.of("hello", "from", "smallrye", "reactive", "stream", "operators",
            "using", "Apache", "Camel")
            .map(String::toUpperCase) // Transform the words
            .filter(s -> s.length() > 4) // Filter items
            .peek(System.out::println)
            .map(s -> s + " ")
            .to(subscriber)
            .run();
    context.start();

    // Just wait until it's done.
    Thread.sleep(1000);

    Files.readAllLines(new File("target/values.txt").toPath())
            .forEach(s -> System.out.println("File >> " + s));
}
 
Example 5
Source File: ReactorIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void testTo() throws Exception {
    CamelContext camelctx = createWildFlyCamelContext();
    camelctx.start();
    try {
        CamelReactiveStreamsService crs = CamelReactiveStreams.get(camelctx);

        Set<String> values = Collections.synchronizedSet(new TreeSet<>());
        CountDownLatch latch = new CountDownLatch(3);

        Flux.just(1, 2, 3)
            .flatMap(e -> crs.to("bean:hello", e, String.class))
            .doOnNext(res -> values.add(res))
            .doOnNext(res -> latch.countDown())
            .subscribe();

        Assert.assertTrue(latch.await(2, TimeUnit.SECONDS));
        Assert.assertEquals(new TreeSet<>(Arrays.asList("Hello 1", "Hello 2", "Hello 3")), values);
    } finally {
        camelctx.close();
    }
}
 
Example 6
Source File: CamelInflightBackPressureTest.java    From camelinaction2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testInflightBackPressure() throws Exception {
    CamelReactiveStreamsService rsCamel = CamelReactiveStreams.get(context);

    // create a published that receive from the inbox stream
    Publisher<String> inbox = rsCamel.fromStream("inbox", String.class);

    // use stream engine to subscribe from the publisher
    Flowable.fromPublisher(inbox)
        .doOnNext(c -> {
            log.info("Processing message {}", c);
            Thread.sleep(1000);
        })
        .subscribe();

    // send in 200 messages
    log.info("Sending 200 messages ...");
    for (int i = 0; i < 200; i++) {
        fluentTemplate.withBody("Hello " + i).to("seda:inbox?waitForTaskToComplete=Never").send();
    }
    log.info("Sent 200 messages done");

    // let it run for 250 seconds
    Thread.sleep(250 * 1000L);
}
 
Example 7
Source File: RxJava2IntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void testOnCompleteHeaderNotForwarded() throws Exception {
    CamelContext camelctx = createCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("reactive-streams:numbers")
                .to("mock:endpoint");
        }
    });

    CamelReactiveStreamsService crs = CamelReactiveStreams.get(camelctx);
    Subscriber<Integer> numbers = crs.streamSubscriber("numbers", Integer.class);

    camelctx.start();
    try {
        Flowable.<Integer>empty().subscribe(numbers);

        MockEndpoint endpoint = camelctx.getEndpoint("mock:endpoint", MockEndpoint.class);
        endpoint.expectedMessageCount(0);
        endpoint.assertIsSatisfied(200);
    } finally {
        camelctx.close();
    }
}
 
Example 8
Source File: CamelFilesTest.java    From camelinaction2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testFiles() throws Exception {
    getMockEndpoint("mock:inbox").expectedMessageCount(4);
    getMockEndpoint("mock:camel").expectedMessageCount(2);

    CamelReactiveStreamsService rsCamel = CamelReactiveStreams.get(context);

    // use stream engine to subscribe from the publisher
    // where we filter out the big numbers which is logged
    Flowable.fromPublisher(rsCamel.from("file:target/inbox"))
        // call the direct:inbox Camel route from within this flow
        .doOnNext(e -> rsCamel.to("direct:inbox", e))
        // filter out files which has Camel in the text
        .filter(e -> e.getIn().getBody(String.class).contains("Camel"))
        // let Camel also be subscriber by the endpoint direct:camel
        .subscribe(rsCamel.subscriber("direct:camel"));

    // create some test files
    fluentTemplate.to("file:target/inbox").withBody("Hello World").withHeader(Exchange.FILE_NAME, "hello.txt").send();
    fluentTemplate.to("file:target/inbox").withBody("Hello Camel").withHeader(Exchange.FILE_NAME, "hello2.txt").send();
    fluentTemplate.to("file:target/inbox").withBody("Bye Camel").withHeader(Exchange.FILE_NAME, "bye.txt").send();
    fluentTemplate.to("file:target/inbox").withBody("Bye World").withHeader(Exchange.FILE_NAME, "bye2.txt").send();

    assertMockEndpointsSatisfied();
}
 
Example 9
Source File: CamelNumbersTest.java    From camelinaction2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testNumbers() throws Exception {
    CamelReactiveStreamsService rsCamel = CamelReactiveStreams.get(context);

    // create a published that receive from the numbers stream
    Publisher<Integer> numbers = rsCamel.fromStream("numbers", Integer.class);

    // use stream engine to subscribe from the publisher
    // where we filter out the big numbers which is logged
    Flowable.fromPublisher(numbers)
        .filter(n -> n > 5)
        .doOnNext(c -> log.info("Streaming big number {}", c))
        .subscribe();

    // let it run for 10 seconds
    Thread.sleep(10000);
}
 
Example 10
Source File: RxJava2IntegrationTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void testOnCompleteHeaderForwarded() throws Exception {
    CamelContext camelctx = createCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("reactive-streams:numbers?forwardOnComplete=true")
                .to("mock:endpoint");
        }
    });

    CamelReactiveStreamsService crs = CamelReactiveStreams.get(camelctx);
    Subscriber<Integer> numbers = crs.streamSubscriber("numbers", Integer.class);

    camelctx.start();
    try {
        Flowable.<Integer>empty().subscribe(numbers);

        MockEndpoint endpoint = camelctx.getEndpoint("mock:endpoint", MockEndpoint.class);
        endpoint.expectedMessageCount(1);
        endpoint.expectedHeaderReceived(ReactiveStreamsConstants.REACTIVE_STREAMS_EVENT_TYPE, "onComplete");
        endpoint.expectedBodiesReceived(new Object[]{null});
        endpoint.assertIsSatisfied();
    } finally {
        camelctx.close();
    }
}
 
Example 11
Source File: ReactorIntegrationTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void testFromStreamMultipleSubscriptionsWithDirect() throws Exception {

    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:reactive")
                .to("reactive-streams:direct");
        }
    });

    camelctx.start();
    try {
        CamelReactiveStreamsService crs = CamelReactiveStreams.get(camelctx);

        CountDownLatch latch1 = new CountDownLatch(2);
        Flux.from(crs.fromStream("direct", Integer.class))
            .doOnNext(res -> latch1.countDown())
            .subscribe();

        CountDownLatch latch2 = new CountDownLatch(2);
        Flux.from(crs.fromStream("direct", Integer.class))
            .doOnNext(res -> latch2.countDown())
            .subscribe();

        ProducerTemplate template = camelctx.createProducerTemplate();
        template.sendBody("direct:reactive", 1);
        template.sendBody("direct:reactive", 2);

        Assert.assertTrue(latch1.await(5, TimeUnit.SECONDS));
        Assert.assertTrue(latch2.await(5, TimeUnit.SECONDS));
    } finally {
        camelctx.close();
    }
}
 
Example 12
Source File: ReactorIntegrationTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void testFromStreamTimer() throws Exception {

    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("timer:tick?period=5&repeatCount=30")
                .setBody().header(Exchange.TIMER_COUNTER)
                .to("reactive-streams:tick");
        }
    });

    final int num = 30;
    final CountDownLatch latch = new CountDownLatch(num);
    final AtomicInteger value = new AtomicInteger(0);

    CamelReactiveStreamsService crs = CamelReactiveStreams.get(camelctx);
    Flux.from(crs.fromStream("tick", Integer.class))
        .doOnNext(res -> Assert.assertEquals(value.incrementAndGet(), res.intValue()))
        .doOnNext(n -> latch.countDown())
        .subscribe();

    camelctx.start();
    try {
        latch.await(5, TimeUnit.SECONDS);
        Assert.assertEquals(num, value.get());
    } finally {
        camelctx.close();
    }
}
 
Example 13
Source File: ReactorIntegrationTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void testFromPublisherWithConversion() throws Exception {
    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:source")
                .to("direct:stream")
                .setBody()
                    .simple("after stream: ${body}");
        }
    });

    camelctx.start();
    try {
        CamelReactiveStreamsService crs = CamelReactiveStreams.get(camelctx);

        crs.process("direct:stream",
            Integer.class,
            publisher ->
                Flux.from(publisher).map(Math::negateExact)
        );

        ProducerTemplate template = camelctx.createProducerTemplate();
        for (int i = 1; i <= 3; i++) {
            Assert.assertEquals(
                "after stream: " + (-i),
                template.requestBody("direct:source", i, String.class)
            );
        }
    } finally {
        camelctx.close();
    }
}
 
Example 14
Source File: ReactorIntegrationTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void testFrom() throws Exception {

    CamelContext camelctx = new DefaultCamelContext();

    CamelReactiveStreamsService crs = CamelReactiveStreams.get(camelctx);
    Publisher<Exchange> timer = crs.from("timer:reactive?period=250&repeatCount=3");

    AtomicInteger value = new AtomicInteger(0);
    CountDownLatch latch = new CountDownLatch(3);

    camelctx.start();
    try {

        // [#2936] Reactive stream has no active subscriptions
        Thread.sleep(500);

        Flux.from(timer)
            .map(exchange -> ExchangeHelper.getHeaderOrProperty(exchange, Exchange.TIMER_COUNTER, Integer.class))
            .doOnNext(res -> Assert.assertEquals(value.incrementAndGet(), res.intValue()))
            .doOnNext(res -> latch.countDown())
            .subscribe();

        Assert.assertTrue(latch.await(2, TimeUnit.SECONDS));
    } finally {
        camelctx.close();
    }
}
 
Example 15
Source File: RxJava2IntegrationTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultipleConsumers() throws Exception {
    CamelContext camelctx = createCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("reactive-streams:multipleConsumers?concurrentConsumers=3")
                .process()
                .message(m -> m.setHeader("thread", Thread.currentThread().getId()))
                .to("mock:multipleBucket");
        }
    });

    CamelReactiveStreamsService crs = CamelReactiveStreams.get(camelctx);

    camelctx.start();
    try {
        Flowable.range(0, 1000).subscribe(
            crs.streamSubscriber("multipleConsumers", Number.class)
        );

        MockEndpoint endpoint = camelctx.getEndpoint("mock:multipleBucket", MockEndpoint.class);
        endpoint.expectedMessageCount(1000);
        endpoint.assertIsSatisfied();

        Assert.assertEquals(
            3,
            endpoint.getExchanges().stream()
                .map(x -> x.getIn().getHeader("thread", String.class))
                .distinct()
                .count()
        );
    } finally {
        camelctx.close();
    }
}
 
Example 16
Source File: RxJava2IntegrationTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void testOnNextHeaderForwarded() throws Exception {
    CamelContext camelctx = createCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("reactive-streams:numbers")
                .to("mock:endpoint");
        }
    });

    CamelReactiveStreamsService crs = CamelReactiveStreams.get(camelctx);
    Subscriber<Integer> numbers = crs.streamSubscriber("numbers", Integer.class);

    camelctx.start();
    try {
        Flowable.just(1).subscribe(numbers);

        MockEndpoint endpoint = camelctx.getEndpoint("mock:endpoint", MockEndpoint.class);
        endpoint.expectedHeaderReceived(ReactiveStreamsConstants.REACTIVE_STREAMS_EVENT_TYPE, "onNext");
        endpoint.expectedMessageCount(1);
        endpoint.assertIsSatisfied();

        Exchange ex = endpoint.getExchanges().get(0);
        Assert.assertEquals(1, ex.getIn().getBody());
    } finally {
        camelctx.close();
    }
}
 
Example 17
Source File: ReactiveStreamsProducers.java    From camel-quarkus with Apache License 2.0 4 votes vote down vote up
@Singleton
@Produces
CamelReactiveStreamsService camelReactiveStreamsService() {
    return CamelReactiveStreams.get(camelContext);
}
 
Example 18
Source File: RxJava2IntegrationTest.java    From wildfly-camel with Apache License 2.0 4 votes vote down vote up
@Test
public void testSingleConsumer() throws Exception {
    CamelContext camelctx = createCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("reactive-streams:singleConsumer")
                .process()
                .message(m -> m.setHeader("thread", Thread.currentThread().getId()))
                .to("mock:singleBucket");
        }
    });

    CamelReactiveStreamsService crs = CamelReactiveStreams.get(camelctx);

    camelctx.start();
    try {

        Flowable.range(0, 1000).subscribe(
            crs.streamSubscriber("singleConsumer", Number.class)
        );

        MockEndpoint endpoint = camelctx.getEndpoint("mock:singleBucket", MockEndpoint.class);
        endpoint.expectedMessageCount(1000);
        endpoint.assertIsSatisfied();

        Assert.assertEquals(
            1,
            endpoint.getExchanges().stream()
                .map(x -> x.getIn().getHeader("thread", String.class))
                .distinct()
                .count()
        );

        // Ensure order is preserved when using a single consumer
        AtomicLong num = new AtomicLong(0);

        endpoint.getExchanges().stream()
            .map(x -> x.getIn().getBody(Long.class))
            .forEach(n -> Assert.assertEquals(num.getAndIncrement(), n.longValue()));
    } finally {
        camelctx.close();
    }
}
 
Example 19
Source File: ReactiveStreamsRegistryEngineTest.java    From camel-spring-boot with Apache License 2.0 4 votes vote down vote up
@Test
public void testAutoConfiguration() throws InterruptedException {
    CamelReactiveStreamsService service = CamelReactiveStreams.get(context);
    Assert.assertTrue(service instanceof MyEngine);
    Assert.assertEquals(service, reactiveStreamsService);
}
 
Example 20
Source File: ReactorIntegrationTest.java    From wildfly-camel with Apache License 2.0 4 votes vote down vote up
@Test
public void testMultipleSubscriptionsWithTimer() throws Exception {

    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("timer:tick?period=50")
                .setBody().header(Exchange.TIMER_COUNTER)
                .to("reactive-streams:tick");
        }
    });

    CountDownLatch latch1 = new CountDownLatch(5);
    CamelReactiveStreamsService crs = CamelReactiveStreams.get(camelctx);
    Disposable disp1 = Flux.from(crs.fromStream("tick", Integer.class)).subscribe(res -> latch1.countDown());

    camelctx.start();
    try {
        // Add another subscription
        CountDownLatch latch2 = new CountDownLatch(5);
        Disposable disp2 = Flux.from(crs.fromStream("tick", Integer.class)).subscribe(res -> latch2.countDown());

        Assert.assertTrue(latch1.await(5, TimeUnit.SECONDS));
        Assert.assertTrue(latch2.await(5, TimeUnit.SECONDS));

        // Un subscribe both
        disp1.dispose();
        disp2.dispose();

        // No active subscriptions, warnings expected
        Thread.sleep(60);

        // Add another subscription
        CountDownLatch latch3 = new CountDownLatch(5);
        Disposable disp3 = Flux.from(crs.fromStream("tick", Integer.class)).subscribe(res -> latch3.countDown());

        Assert.assertTrue(latch3.await(5, TimeUnit.SECONDS));
        disp3.dispose();
    } finally {
        camelctx.close();
    }
}