narcoticfresh
narcoticfresh

Reputation: 282

How to send only 1 message at once after split() on inOut exchange

I'm using Camel for a while and I'm a huge admirer of its simplicity.

The use case

Given this simple route:

from("mock:some-route")
  // split 1
  .split().method("splitterBean", "split")
  // now we have an ArrayList of n messages (let's say 10)
  .to(ExchangePattern.InOut, "jms:some-thing");

If we assume that we have 10 messages after the split(), this route will immediately send 10 messages to the "to" endpoint. So jms:some-thing will receive all 10 messages at once.

The problem

--> Please note that the "out" endpoint is inOut, so we have timeouts in place when the receiver must acknowledge the message.

The application on the receiving end of jms:some-thing has to do quite some work for each message. As all 10 messages were written at the same time, the same timeout applies for all of them.

So we increased that timeout.

But one day, we will have 1000 messages and the timeout will again be to low.

What i want to archieve

I want to implement a pattern where I'll send only 1 message at once after the split, then sending the next after 1 message is acknowledged by the receiving system.

So instead of sending the 10 messages at once, I want

How to implement such behavior?

I looked at the documentation, but none of the EIP components seem to fulfill that need?

Thanks for any input

Upvotes: 0

Views: 460

Answers (1)

AndyN
AndyN

Reputation: 2105

You can have an intermediate seda queue with only one thread.

from("mock:some-route")
  .split().method("splitterBean", "split")
  .to("seda:your-seda-queue?waitForTaskToComplete=Always&timeout=0");

from("seda:your-seda-queue?waitForTaskToComplete=Always&timeout=0")
  .to(ExchangePattern.InOut, "jms:some-thing");

By default, a seda queue will have a single consuming thread, and will block the calling thread until a consumer becomes available. More on seda details here

Saying that, your sending to a jms topic, which is really what you should be using to queue up your requests instead of a seda queue. You should look into implementing this logic asynchronously, and waiting on a reply topic rather than using a timeout.

Upvotes: 1

Related Questions