Search code examples
apache-camelblueprint-osgi

Passing a property value across different camel Context in apache camel


How to pass properties in camel to different camel context's? My current architecture will process 4 different kind of messages (A,B,C,D) and it uses same routes for all of them only while saving it changes the DB table names based on message type, but now I have a requirement that I need to save only few values from the exchange object for a particular message. I'm thinking of setting a property in a route and message type is 'E' I'll direct that to another route. but how do I pass the property value to different camel context


Solution

  • I don't know if you mean application properties (such as in Java property files) or an Exchange property as in Camels Exchange object to wrap a message.

    However, it sounds like the latter since application properties are normally not passed around.

    Exchange properties are just part of the Camel wrapper around a message during processing. If you send a message during route processing to another endpoint as with .to(endpoint), normally only the message is sent and the Exchange is thrown away.

    from(endpoint)
        .setProperty("myProperty", value)
        .to("activemq:queue:myQueue")
        // myProperty is no more available at myQueue
    

    There are of course exceptions, it depends on the endpoint type. For example when sending to direct endpoints (synchronous Camel in-memory endpoint), the Exchange survives. But direct endpoints do not work across different Camel contexts. For other endpoint types like HTTP, JMS etc the properties are lost.

    Therefore, if you want to set a "message variable" that is passed to other endpoints, especially across different Camel contexts, you have to set a message header.

    from(endpoint)
        .setHeader("myHeader", value)
        .to("activemq:queue:myQueue")
        // myHeader is still available at myQueue