33

In apache camel, which of those is the best way to pass values from an exchange processor to another (and why) :

  • storing it in the exchange headers
  • using the setProperty method while building the route.
  • another way..
kgautron
  • 7,915
  • 9
  • 39
  • 60

4 Answers4

65

One distinction not mentioned by Ben and Petter is that properties are safely stored for the entire duration of the processing of the message in Camel. In contrast, headers are part of the message protocol, and may not be propagated during routing. For example, JMS has limitations what you can store as headers etc.

You may want to read the free chapter 1 of the Camel in Action book as it covers the Camel concepts with Exchange, Message, etc.

cegas
  • 2,823
  • 3
  • 16
  • 16
Claus Ibsen
  • 56,060
  • 7
  • 50
  • 65
  • 14
    +1 This is an important distinction. In general, if you want to be sure that the data you're setting is going to end up where you want it, then I strongly recommend that you use the properties. I have been burnt by this in the past, where someone added a JMS step into the middle of a route along which I was passing information stored in headers, and the information just stopped getting through. – Dawood ibn Kareem Aug 05 '12 at 22:11
  • In case of JMS, properties are not propagated at all and headers are the the recommended way for passing day. Isn't that correct or I'm missing something? – Bilgin Ibryam May 11 '14 at 23:09
  • 2
    Maybe its the terms. JMS body + properties is the JMS terms. In Camel its body + headers on Message, and properties on Exchange. Camel maps body -> body, and headers -> JMS properties. – Claus Ibsen May 12 '14 at 07:00
  • It seems the JMSProperties does not support Java collection as a type. This should be taken in account if you need multiple values for the same header. The only way we found is to serialize a Map in the body with `marshal().xstream()` before sending to the queue endpoint. – рüффп Aug 26 '14 at 22:50
  • safely? you mean set different properties only see in current processing? – Chao Apr 24 '18 at 02:06
33

Properties and headers are pretty much the same. Headers are, however, converted to/from protocol specific headers on certain components, such as Jms. So,

  • Meta data inside a route: properties
  • Meta data to/from outside: headers
Petter Nordlander
  • 22,053
  • 5
  • 50
  • 84
24

the Exchange is passed between Processors. It contains properties, IN message and optional OUT message. Each of these is capable of storing Object data, but in general:

  • use the Exchange Properties for general meta-data about the message (used less frequently)
  • use the IN message headers to configure endpoint properties or for meta-data about the message body (used often)
  • use the IN message body for the payload of the message (used most often)
  • create an OUT message only if necessary to maintain separate IN vs. OUT messages during processing (by default only IN is used)

That said, it really depends on the component called following your processor. Most have some headers and/or body values that are required to use the endpoint, etc. See the specific component page for these details.

Also, the Exchange/Message are explained in more detail on these pages:

http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/Exchange.html

http://fusesource.com/docs/router/2.8/prog_guide/MsgFormats-Exchanges.html

Ben ODay
  • 20,784
  • 9
  • 45
  • 68
9

Answer is here:

Properties: The properties is a Map and may look like message headers. The main difference is their lifetime: the properties exist during the whole exchange execution, whereas the headers are limited to the message duration (and a message can change a lot during routing, so during the exchange execution). Camel itself may add some properties for some use cases.

Adam Ostrožlík
  • 1,256
  • 1
  • 10
  • 16
  • 1
    Just to add to the point, The exchange is the same for the entire lifecycle of routing, but the messages can change, for instance, if messages are transformed from one format to another (from Camel in Action). – Manu Oct 31 '19 at 05:31