0

I am storing the list of url's in the Apache camel header,below is the code,

List<String> supplierHotelRefs = new ArrayList();
supplierHotelRefs.add("a.com");
supplierHotelRefs.add("b.com");
supplierHotelRefs.add("c.com");
 exchange.getIn().setBody(supplierHotelRefs);

Now i need to iterate this list present in the header and make a call to url's. This should be the parallel activity. I tried with split(..) which is working fine if we store the list in the body, But due to some constraints i can't store it in body. It will be helpful if i get code to iterate and parallely process the collection present in the Camel Header.

Regards, Raghavan

Raghavan
  • 401
  • 6
  • 21
  • Note that a parallel stream might not be a good idea here, assuming that each thread would be making some kind of service call. – Tim Biegeleisen Sep 25 '18 at 07:28
  • Thanks for your reply tim. But we need to access only single element in list on each thread, That we can't able to achieve that itself. – Raghavan Sep 25 '18 at 07:33
  • @Raghavan Accept any of the answers provided to validate that the solution provided worked for you or not. – XING Aug 05 '20 at 04:40

2 Answers2

2

You could set the list in the header and split on that header.

exchange.getIn().setHeader("supplierHotelRefs",supplierHotelRefs);

In your route definition you could split based on the header property and process them parallely.

from("").....
//split based on the header
split(header("supplierHotelRefs"))
//process every split exchange parallely
.parallelProcessing()
//end split block
.end()
//continue route definition after split
.log("completed split processing")

Note that the caller thread will still wait for all the split messages to be completed.

Srini
  • 420
  • 1
  • 5
  • 17
0

You can use Recipient List EIP, see http://camel.apache.org/recipient-list.html

You have to create a list with all recipients and store that list to a header. I observed in your code that you are not setting that list to a header, but as a body. You have to do something like

List<String> supplierHotelRefs = new ArrayList();
supplierHotelRefs.add("http4://a.com");
supplierHotelRefs.add("http4://b.com");
supplierHotelRefs.add("http4://c.com");
exchange.getIn().setHeader("yourHeaderName", supplierHotelRefs);

As you can notice each element of the list has a valid url of http4 Camel component.This component is used in order to make http calls. You can have anything you want into that list (other Camel's components).

Then you use the recipient list EIP, telling that all recipients are into the previous created header. parallelProcessing = true, call all items in the list in parallel. This is calling that in XML DSL:

<recipientList parallelProcessing="true">
   <header>yourHeaderName</header>
</recipientList>  

or in Java DSL:

from("...")
  ...
  .recipientList(header("yourHeaderName"));
Themis Pyrgiotis
  • 876
  • 5
  • 15