如何正确使用Apache骆驼中的直接成分?

时间:2019-08-13 12:00:50

标签: java apache-camel

我有一条简单的路线:

direct:in -> step1 -> step2 -> stepN -> direct:out

我想像函数调用一样使用它:

consumer = camelContext.createConsumerTemplate()
producer = camelContext.createProducerTemplate()
producer.sendBody("direct:int", body)
consumer.receiveBody("direct:out", TYPE)

问题在于,当我调用producer.sendBody(...)时,线程被阻塞了。另外,由于线程被阻塞,我无法使用使用者,因此结果是direct:out上没有使用者。

我可以为使用者使用另一个线程,但是我的目标是使用骆驼路线作为输入和输出的功能。

此外,我可以使用producer.asyncSendBody(...),但这是正确的方法吗?这种方法允许我使用consumer使用消息,但是我认为应该有另一种方法。

1 个答案:

答案 0 :(得分:2)

不知道步骤1、2,N在做什么,就不可能确切地说出正在发生什么,但是假设它们没有阻塞,那么您看到的是,因为-replace "_*"直到完成为止有些东西消耗了交换。由于该调用是在direct:out之后发出的,因此无法完成-如您所见。

您有三个选择(可能更多):

  1. 如前所述,使用sendBody()
  2. asyncSendBody()更改为direct:out,这使Exchange排队,从而使发送完成并且接收继续进行。
  3. 删除seda:out端点,然后从"direct:out"更改为sendBody(Endpoint, Object),这会将最终结果正文返回给调用者。

选项3似乎仍然是您想要执行的操作,并且更简单。