camel: how can i send to an endpoint asynchronously

你。 提交于 2019-12-22 08:36:42

问题


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)?


回答1:


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();



回答2:


wireTap(endpoint) is the answer.




回答3:


That might depend on what endpoints etc you are using, but one common method is to put a seda endpoint in between is one option.

from("foo:bar")
  .bean(processingBean)
  .to("seda:asyncProcess") // Async send
  .bean(moreProcessingBean)

from("seda:asyncProcess")
  .to("final:endpoint"); // could be some syncrhonous endpoint that takes time to send to. http://server/heavyProcessingService or what not.

The seda endpoint behaves like a queue, first in - first out. If you dispatch several events to a seda endpoint faster than the route can finish processing them, they will stack up and wait for processing, which is a nice behaviour.




回答4:


you can use a ProducerTemplate's asyncSend() method to send an InOnly message to an endpoint...

template.asyncSend("direct:myInOnlyEndpoint","myMessage");

see http://camel.apache.org/async.html for some more details




回答5:


You can use inOnly in your route to only send your message to an endpoint without waiting for a response. For more details see the request reply documentation or the event message documentation

from("direct:testInOnly").inOnly("mock:result");


来源:https://stackoverflow.com/questions/11981900/camel-how-can-i-send-to-an-endpoint-asynchronously

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!