concurrency - 锁定 Camel 处理器

标签 concurrency parallel-processing jms apache-camel

我想知道在 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/

相关文章:

haskell - 如何优化并行排序以提高时间性能?

r - R中的doParallel错误:serialize(data,node $ con)错误:写入连接时出错

multithreading - 同时执行多个线程循环的并发编程?

java - 是否需要 JMX 提供者(或应用程序服务器)才能使用 JMX?

jms - 如果没有可用的临时队列,实现请求/回复模式的最佳方式是什么?

java - 使用 Spring JMS 时编译错误

python - 将 concurrent.futures 与 Tornado 事件循环一起使用?

java - 循环障碍Java,如何验证?

java - 信号量 : Permit acquired in one thread can be released from another thread - Example

c# - 在这种情况下锁定是否保证多个修改在线程间是原子的?