gpt4 book ai didi

concurrency - 锁定 Camel 处理器

转载 作者:行者123 更新时间:2023-12-03 18:38:58 30 4
gpt4 key购买 nike

我想知道在 Camel 处理器上获得同步的方法。

我在 docs 找到的唯一相关内容:

Note that there is no concurrency or locking issue when using ActiveMQ, JMS or SEDA by design; they are designed for highly concurrent use. However there are possible concurrency issues in the Processor of the messages i.e. what the processor does with the message?



因此,如果我想锁定 org.apache.camel.Processor.process(Exchange) ,即我希望其他线程等待 流程 方法在忙时完成。那可能吗?

更新 :实际上我试图在进程方法内部进行同步(锁定) - 这在 JVM 端有效。但是我的处理器是事务路由的一部分,这是一个问题 - 对持久层的所有更改只有在退出处理器(甚至可能是路由)后才可见。所以我认为这个问题有一些类似 Camel 的解决方案。

最佳答案

您在 Camel 处理器中实现的业务逻辑必须是线程安全的,因为多个线程将在 Camel 中路由消息期间重用同一个实例。

如果你想使用原型(prototype)作用域(例如为每条消息创建一个新的处理器实例),那么你可以使用 bean 组件,并设置 cache=false,如果你使用 spring,则将 bean 声明为原型(prototype)

   <bean id="myBean" class="com.foo.MyBean" scope="prototype"/>

然后在路由中调用这个bean
   .to("bean:myBean?cache=false")

尽管人们经常使用单例实例。

如果您想要任何类型的锁定,您可以将方法定义为同步并让 JVM 为您锁定它。
public synchronized void process(Exchange exchange) throws Exception {
...
}

关于concurrency - 锁定 Camel 处理器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19869761/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com