84.9. 使用多个Consumers

在这个示例中,我们定义了两个消费者,并将它们注册为 spring beans。

<!-- define the consumers as spring beans -->
<bean id="consumer1" class="org.apache.camel.spring.example.FooEventConsumer"/>

<bean id="consumer2" class="org.apache.camel.spring.example.AnotherFooEventConsumer"/>

<camelContext xmlns="http://camel.apache.org/schema/spring">
    <!-- define a shared endpoint which the consumers can refer to instead of using url -->
    <endpoint id="foo" uri="disruptor:foo?multipleConsumers=true"/>
</camelContext>

由于在 Disruptor foo 端点上指定了多个Consumers=true,所以我们可以有两人或更多用户收到他们自己的消息副本作为 pub-sub 风格的消息传递。作为 Beans 是单元测试的一部分,只需向模拟端点发送消息,但请注意,我们可以如何使用 @Consume 从 Disruptor 使用。

public class FooEventConsumer {

    @EndpointInject(uri = "mock:result")
    private ProducerTemplate destination;

    @Consume(ref = "foo")
    public void doSomething(String body) {
        destination.sendBody("foo" + body);
    }

}