Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

camel: how can i send to an endpoint asynchronously

How can I send a message to an endpoint without waiting for that endpoint's route to be process (that is, my route should just dispatch the message and finish)?

like image 677
IttayD Avatar asked Aug 16 '12 06:08

IttayD


People also ask

Are camels asynchronous?

Background. The new Async API in Camel 2.0 leverages in much greater detail the Java Concurrency API and its support for executing tasks asynchronous. Therefore the Camel Async API should be familiar for users with knowledge of the Java Concurrency API.

Is Apache Camel synchronous?

Both producer and consumer are supported The Direct component provides direct, synchronous invocation of any consumers when a producer sends a message exchange.

What is Seda in Apache Camel?

The SEDA component provides asynchronous SEDA behavior, so that messages are exchanged on a BlockingQueue and consumers are invoked in a separate thread from the producer. Note that queues are only visible within a single CamelContext.


1 Answers

Using wireTap or multicast is what you're after. A direct: endpoint will modify the Exchange for the next step no matter what ExchangePattern is specified. You can see by using this failing test:

public class StackOverflowTest extends CamelTestSupport {
    private static final String DIRECT_INPUT = "direct:input";
    private static final String DIRECT_NO_RETURN = "direct:no.return";
    private static final String MOCK_OUTPUT = "mock:output";
    private static final String FIRST_STRING = "FIRST";
    private static final String SECOND_STRING = "SECOND";

    @NotNull
    @Override
    protected RouteBuilder createRouteBuilder() throws Exception {
        return new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from(DIRECT_INPUT)
                        .to(ExchangePattern.InOnly, DIRECT_NO_RETURN)
                        .to(MOCK_OUTPUT)
                        .end();

                from(DIRECT_NO_RETURN)
                        .bean(new CreateNewString())
                        .end();
            }
        };
    }

    @Test
    public void testShouldNotModifyMessage() throws JsonProcessingException, InterruptedException {
        final MockEndpoint myMockEndpoint = getMockEndpoint(MOCK_OUTPUT);
        myMockEndpoint.expectedBodiesReceived(FIRST_STRING);
        template.sendBody(DIRECT_INPUT, FIRST_STRING);
        assertMockEndpointsSatisfied();
    }

    public static class CreateNewString {
        @NotNull
        public String handle(@NotNull Object anObject) {
            return SECOND_STRING;
        }
    }
}

Now if you change the above to a wireTap:

                from(DIRECT_INPUT)
                    .wireTap(DIRECT_NO_RETURN)
                    .to(MOCK_OUTPUT)
                    .end();

and you'll see it works as expected. You can also use multicast:

                from(DIRECT_INPUT)
                    .multicast()
                    .to(DIRECT_NO_RETURN)
                    .to(MOCK_OUTPUT)
                    .end();
like image 152
BrassyPanache Avatar answered Oct 29 '22 05:10

BrassyPanache