简体   繁体   English

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

[英]How to correctly use direct component in apache camel?

I have a simple route: 我有一条简单的路线:

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

I want to use this like a function call: 我想像一个函数调用一样使用它:

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

The problem is that when i call producer.sendBody(...) the thread is blocked. 问题是,当我调用producer.sendBody(...) ,线程被阻塞。 Also, due to the thread is blocked, i am not able to use consumer, so in result i got an exception that there is no consumer on direct:out . 另外,由于线程被阻塞,我无法使用使用者,所以结果我得到了一个例外,那就是在direct:out上没有使用者。

I can use another thread for consumer, but my goal is to use camel route as function with input and output. 我可以为使用者使用另一个线程,但是我的目标是使用骆驼路线作为输入和输出的函数。

Also, i can use producer.asyncSendBody(...) but is this a correct way? 另外,我可以使用producer.asyncSendBody(...)但这是正确的方法吗? This approach allows me to consume messages using consumer , but i think that there should be another way. 这种方法使我可以使用consumer来使用消息,但是我认为应该有另一种方法。

Without knowing what steps 1, 2, N are doing it's not possible to definitively say what is happening, but assuming they do not block, then what you are seeing is because the direct:out can't be completed until something consumes that exchange. 在不知道第1、2,N步正在执行的情况下,不可能确切地说出正在发生的事情,但是假设它们没有阻塞,那么您所看到的是,因为direct:out在某些东西消耗了该交换之前无法完成。 Since that call comes after sendBody() it can't complete - just as you see. 由于该调用是在sendBody()之后sendBody() ,因此无法完成-如您所见。

You have three options (maybe more): 您有三种选择(可能更多):

  1. Use asyncSendBody() as you noted. asyncSendBody()使用asyncSendBody()
  2. Change from direct:out to seda:out which queues the Exchange, allowing the send to complete and the receive to proceed. direct:out更改为seda:out ,它将在Exchange中排队,从而使发送完成并且接收继续进行。
  3. Remove the "direct:out" endpoint and change from sendBody(Endpoint, Object) to sendBody(Endpoint, ExchangePattern, Object) which returns the end-result body to the caller. 删除"direct:out"端点,并将其从sendBody(Endpoint, Object)更改为sendBody(Endpoint, ExchangePattern, Object) ,这会将最终结果主体返回给调用者。

Option 3 seems like what you want to do anyway, and is simpler. 选项3似乎仍然是您想要执行的操作,并且更简单。

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

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