Search in sources :

Example 6 with SynchronizationAdapter

use of org.apache.camel.support.SynchronizationAdapter in project camel by apache.

the class OnCompletionContainsTest method createRouteBuilder.

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
    return new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            onCompletion().to("mock:sync");
            from("direct:start").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    SynchronizationAdapter adapter = new SimpleSynchronizationAdapter("mock:sync", "A");
                    exchange.addOnCompletion(adapter);
                    // should not add the adapter again as we already have it
                    if (!exchange.containsOnCompletion(adapter)) {
                        exchange.addOnCompletion(adapter);
                    }
                    adapter = new SimpleSynchronizationAdapter("mock:sync", "B");
                    exchange.addOnCompletion(adapter);
                    // now add the B again as we want to test that this also work
                    if (exchange.containsOnCompletion(adapter)) {
                        exchange.addOnCompletion(adapter);
                    }
                    // add a C that is no a SimpleSynchronizationAdapter class
                    exchange.addOnCompletion(new SynchronizationAdapter() {

                        @Override
                        public void onDone(Exchange exchange) {
                            template.sendBody("mock:sync", "C");
                        }

                        @Override
                        public String toString() {
                            return "C";
                        }
                    });
                }
            }).to("mock:result");
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) SynchronizationAdapter(org.apache.camel.support.SynchronizationAdapter)

Example 7 with SynchronizationAdapter

use of org.apache.camel.support.SynchronizationAdapter in project camel by apache.

the class OnCompletionShouldBeLastTest method createRouteBuilder.

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
    return new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            onCompletion().to("mock:sync");
            from("direct:start").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    exchange.addOnCompletion(new SynchronizationAdapter() {

                        @Override
                        public void onDone(Exchange exchange) {
                            template.sendBody("mock:sync", "A");
                        }

                        @Override
                        public String toString() {
                            return "A";
                        }
                    });
                    exchange.addOnCompletion(new SynchronizationAdapter() {

                        @Override
                        public void onDone(Exchange exchange) {
                            template.sendBody("mock:sync", "B");
                        }

                        @Override
                        public String toString() {
                            return "B";
                        }
                    });
                    exchange.addOnCompletion(new SynchronizationAdapter() {

                        @Override
                        public void onDone(Exchange exchange) {
                            template.sendBody("mock:sync", "C");
                        }

                        @Override
                        public String toString() {
                            return "C";
                        }
                    });
                }
            }).to("mock:result");
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) SynchronizationAdapter(org.apache.camel.support.SynchronizationAdapter)

Example 8 with SynchronizationAdapter

use of org.apache.camel.support.SynchronizationAdapter in project camel by apache.

the class UnitOfWorkSynchronizationAdapterTest method createRouteBuilder.

protected RouteBuilder createRouteBuilder() {
    return new RouteBuilder() {

        public void configure() {
            from("seda:async").to("direct:foo");
            from("direct:foo").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    log.info("Received: " + exchange);
                    exchange.getUnitOfWork().addSynchronization(new SynchronizationAdapter() {

                        @Override
                        public void onComplete(Exchange exchange) {
                            completed = exchange;
                            foo = exchange.getIn().getHeader("foo");
                            doneLatch.countDown();
                        }
                    });
                    exchange.getUnitOfWork().addSynchronization(new SynchronizationAdapter() {

                        @Override
                        public void onFailure(Exchange exchange) {
                            failed = exchange;
                            baz = exchange.getIn().getHeader("baz");
                            doneLatch.countDown();
                        }
                    });
                    String name = getName();
                    if (name.equals("testFail")) {
                        log.info("Failing test!");
                        exchange.getOut().setFault(true);
                        exchange.getOut().setBody("testFail() should always fail with a fault!");
                    } else if (name.equals("testException")) {
                        log.info("Throwing exception!");
                        throw new Exception("Failing test!");
                    }
                }
            });
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) SynchronizationAdapter(org.apache.camel.support.SynchronizationAdapter)

Example 9 with SynchronizationAdapter

use of org.apache.camel.support.SynchronizationAdapter in project camel by apache.

the class DefaultProducerTemplateAsyncTest method testAsyncCallbackBodyInOnlyGetResult.

public void testAsyncCallbackBodyInOnlyGetResult() throws Exception {
    ORDER.set(0);
    getMockEndpoint("mock:result").expectedBodiesReceived("Hello World");
    Future<Object> future = template.asyncCallbackSendBody("direct:start", "Hello", new SynchronizationAdapter() {

        @Override
        public void onDone(Exchange exchange) {
            assertEquals("Hello World", exchange.getIn().getBody());
            ORDER.addAndGet(2);
        }
    });
    ORDER.addAndGet(1);
    Object reply = future.get(10, TimeUnit.SECONDS);
    ORDER.addAndGet(4);
    assertMockEndpointsSatisfied();
    assertEquals(7, ORDER.get());
    // no reply when in only
    assertEquals(null, reply);
}
Also used : Exchange(org.apache.camel.Exchange) SynchronizationAdapter(org.apache.camel.support.SynchronizationAdapter)

Example 10 with SynchronizationAdapter

use of org.apache.camel.support.SynchronizationAdapter in project camel by apache.

the class DefaultProducerTemplateAsyncTest method testAsyncCallbackInOnlyProcessor.

public void testAsyncCallbackInOnlyProcessor() throws Exception {
    ORDER.set(0);
    getMockEndpoint("mock:result").expectedBodiesReceived("Hello World");
    final CountDownLatch latch = new CountDownLatch(1);
    template.asyncCallback("direct:start", new Processor() {

        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setBody("Hello");
        }
    }, new SynchronizationAdapter() {

        @Override
        public void onDone(Exchange exchange) {
            assertEquals("Hello World", exchange.getIn().getBody());
            ORDER.addAndGet(2);
            latch.countDown();
        }
    });
    ORDER.addAndGet(1);
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    ORDER.addAndGet(4);
    assertMockEndpointsSatisfied();
    assertEquals(7, ORDER.get());
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) CountDownLatch(java.util.concurrent.CountDownLatch) RuntimeCamelException(org.apache.camel.RuntimeCamelException) SynchronizationAdapter(org.apache.camel.support.SynchronizationAdapter)

Aggregations

Exchange (org.apache.camel.Exchange)32 SynchronizationAdapter (org.apache.camel.support.SynchronizationAdapter)32 CountDownLatch (java.util.concurrent.CountDownLatch)13 Processor (org.apache.camel.Processor)13 RouteBuilder (org.apache.camel.builder.RouteBuilder)10 ExchangeTimedOutException (org.apache.camel.ExchangeTimedOutException)3 RuntimeCamelException (org.apache.camel.RuntimeCamelException)3 WaitForTaskToComplete (org.apache.camel.WaitForTaskToComplete)3 Message (org.apache.camel.Message)2 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)1 Channel (io.netty.channel.Channel)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 ChannelHandler (io.netty.channel.ChannelHandler)1 EpollDatagramChannel (io.netty.channel.epoll.EpollDatagramChannel)1 EpollSocketChannel (io.netty.channel.epoll.EpollSocketChannel)1 NioDatagramChannel (io.netty.channel.socket.nio.NioDatagramChannel)1 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)1 ReadTimeoutHandler (io.netty.handler.timeout.ReadTimeoutHandler)1 IOException (java.io.IOException)1