简体   繁体   English

EIP / Apache Camel - 如何同时处理消息,但每个组原子地处理?

[英]EIP/Apache Camel - How to process message concurrently, but atomically per group?

I have the following situation: 我有以下情况:

  • There are a fixed number of groups. 有固定数量的组。
  • There is a TCP stream of incoming messages. 有一个传入消息的TCP流。 Each message is related to exactly one group. 每条消息都与一个组完全相关。

I start the Camel route as following: 我启动Camel路线如下:

public class MyMessage implements Runnable {
    public void run() {
        // omitted here
    }
}

from("netty:tcp://localhost:7777?textline=true&sync=false")
   ... // omitted here: parse message to pojo MyMessage, set header "group-identifier"
   .to(seda:process);

This Camel route consumes the TCP stream, parses and converts the payload of each incoming message to a MyMessage pojo and sets the group-identifier header on the exchange which corresponds with a message... 此Camel路由使用TCP流,解析每个传入消息的有效负载并将其转换为MyMessage pojo,并在交换机上设置与消息对应的group-identifier标头...

Now I want to consume seda:process as following: 现在我想使用seda:process如下:

  • Messages belonging the same group cannot be executed concurrently. 属于同一组的消息不能同时执行。
  • Messages belonging to different groups can be executed concurrently. 属于不同组的消息可以同时执行。
  • Each messages should be executed by calling run() . 每个消息都应该通过调用run()来执行。 I want to provide/define an ExecutorService for this, so I can control the number of threads. 我想为此提供/定义ExecutorService ,因此我可以控制线程数。

Which enterprise integration patterns can I apply here? 我可以在这里应用哪些企业集成模式? How can I map these concepts to Camel? 我如何将这些概念映射到Camel?

I learnt that ActiveMQ has the concept of message groups ( http://activemq.apache.org/message-groups.html ). 我了解到ActiveMQ具有消息组的概念( http://activemq.apache.org/message-groups.html )。 This might provide a way to make sure that two messages of the same group will never be executed at the same time. 这可能提供一种方法来确保同一组中的两条消息永远不会同时执行。 Though, I am not sure that introducing ActiveMQ only for this isn't overkill. 虽然,我不确定仅为此引入ActiveMQ并不是一种矫枉过正。 Can this also be achieved with 'core' Camel/Java? 这可以通过'核心'Camel / Java来实现吗?

It is quite easy to do this in ActiveMQ. 在ActiveMQ中很容易做到这一点。 The following code snippet simulates executing messages as required: 以下代码段根据需要模拟执行消息:

  • Messages belonging to the same group are executed sequentially. 属于同一组的消息是顺序执行的。
  • Messages belong to different groups are executed concurrently. 属于不同组的消息同时执行。

This relies on ActiveMQ message groups as explained on http://activemq.apache.org/message-groups.html . 这依赖于http://activemq.apache.org/message-groups.html上解释的ActiveMQ消息组。

final CamelContext context = new DefaultCamelContext();

context.addComponent("activemq", ActiveMQComponent.activeMQComponent("vm://localhost?broker.persistent=false"));
context.addRoutes(new RouteBuilder() {
    @Override
    public void configure() {
        from("activemq:queue:q?concurrentConsumers=5")
                .process(exchange -> {
                    System.out.println(Thread.currentThread() + " - " + exchange.getIn().getBody());
                    Thread.sleep(5000);
                });
    }
});
context.start();

for (int i = 0; i < 1000; ++i) {
    context.createFluentProducerTemplate()
            .withBody("This is a message from group : " + (i % 5))
            .withHeader("JMSXGroupID", "" + (i % 5))
            .to("activemq:queue:q")
            .send();
}

That said, I am (still) wondering if this could be done with pure EIPs/Camel-core. 也就是说,我(仍然)想知道这是否可以用纯EIP / Camel-core完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM