简体   繁体   中英

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. 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 .

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? This approach allows me to consume messages using consumer , but i think that there should be another way.

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. Since that call comes after sendBody() it can't complete - just as you see.

You have three options (maybe more):

  1. Use asyncSendBody() as you noted.
  2. Change from direct:out to seda:out which queues the Exchange, allowing the send to complete and the receive to proceed.
  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.

Option 3 seems like what you want to do anyway, and is simpler.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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