java 骆驼:我如何异步发送到端点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11981900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 07:09:31  来源:igfitidea点击:

camel: how can i send to an endpoint asynchronously

javaapache-camel

提问by IttayD

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

如何在不等待端点的路由被处理的情况下向端点发送消息(也就是说,我的路由应该只发送消息并完成)?

采纳答案by IttayD

wireTap(endpoint)is the answer.

wireTap(endpoint)是答案。

回答by BrassyPanache

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:

使用 wireTap 或多播是您所追求的。无论指定什么 ExchangePattern,direct: 端点都会为下一步修改 Exchange。您可以通过使用此失败测试来查看:

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:

现在,如果您将上述内容更改为 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();

回答by cexbrayat

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 documentationor the event message documentation

您可以在路由中使用 inOnly 只将消息发送到端点,而无需等待响应。有关更多详细信息,请参阅请求回复文档事件消息文档

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

回答by Ben ODay

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

您可以使用ProducerTemplate的 asyncSend() 方法将 InOnly 消息发送到端点...

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

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

有关更多详细信息,请参阅http://camel.apache.org/async.html

回答by Petter Nordlander

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.

这可能取决于您使用的端点等,但一种常见的方法是在两者之间放置一个 seda 端点是一种选择。

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.

seda 端点的行为就像一个队列,先进先出。如果您将多个事件分派到 seda 端点的速度比路由完成处理它们的速度快,它们将堆积起来等待处理,这是一种很好的行为。