Apache Camel unique property name in two different routes - apache-camel

We have multiple routes running independently on a quartz scheduler, most of the functionalities are similar, so we have created some common routes to call inside parent routes for code reuse.
Is generic route property will be treated as a local variable and will not be shared among two different routes or value of the property will be changed by some other route.
<setProperty propertyName="remoteServerException">
<simple>${exception.message}</simple>
</setProperty>
Above is one of the property used in common route, and called from multiple routes, is it fine to be called like this? Please advice.

The <setProperty> is referring to properties on the Camel Exchange which is the instance, that contains the message being routed - there is one Exchange per message and its not shared - its local for that given message. So if you have some kind of shared route, you call via direct endpoints etc, then calling <setProperty> will not cause harm, its operating only on the Exchange instance.

Related

Is Apache Camel route step scope always singleton?

I mean that if (for example) processor bean is declared in Blueprint XML and included into just one route, then its single instance is used by every thread that executes message routing within this route. So I guess that there is no sense in prototype scope for such bean declaration.
The only right place to use prototype scope for route step declaration is the case when bean is used in several routes. In this case there will be created separate instance of bean for every route.
Am I wright?
No if you refer to the same bean id in multiple routes its the same instance you are using.
You can configure Camel bean component to turn off its cache with cache=false and define the bean to be prototype scoped to have a new instance per call - however its seldom used and also a bad practice. Its better to code your beans as thread-safe.

Sharing state in camel?

It appears I am running into issues sharing information between routes.
What is the camel pattern for passing around information ?
I looked at exchange properties but that does not stick around between routes I think...
eg:
one file has one has some configutations
i have a route to read this file
and several other routes that will act on based on the configs,
how do I accomplish this ?
I thought of puttin the values in a singleton bean, but that seems kind of ugly...
Exchange properties are preserved across routes inside camel (but there are some limitations and special cases when using splitter/aggregator etc.)
Assign ID's to all sub routes which will act on based on the config. Then get the suitable Route or RouteDefinition from camel context and check whether you can advice or share information to the route according.
ModelCamelContext modelContext;
modelContext.getRouteDefinition(String routeId) or modelContext.getRoute(String routeId)

How to access header and property from callee in ASynch Route in Apache Camel

I have Apache Camel Route which listens to ActiveMQ queue. During the processing, at one point, route sets the header and property on the exchange.
Now during the integration testing, we want to check the value of the header and property.
The question is, how do we access these two things ie. header and property.
I have tried using the producerTemplate's asyncRequestBody/asyncRequestBodyAndHeader etc. With Future object I can access Exchange, however, I am not able to access the header and property set on the exchange.
I have made sure that the route is InOut type.
If you can get the exchange, can you not just use exchange.getProperty(name) to get the property you're looking for?
I have set properties in my route:
<setProperty propertyName="sampleProperty">
<simple>${body}</simple>
</setProperty>
and retrieve them later using {property.sampleProperty}

Transacted camel route with auto-startup set to false

I am in the process of developing a message router which has a bunch of routes that are started and stopped at runtime based some certain conditions.
By default all these routes are configured with auto-starup=false
Now I am trying to add transactional support to these routes and it seems that you cannot define a transacted route and control the its startup behavior at the same time. This is because RouteDefinition.transacted() returns a TransactedDefinition instance which does not have an autoStartup(boolean autoStartup) method.
I am sure I am not the only one to need this kind of functionality and just wondering what is the camel way of addressing such requirements.
Thank you in advance for your inputs
Maybe just set autoStartup first, eg
from("direct:start").autoStartup(false)
.transacted()
.to("mock:result");

Start Camel route in suspended state

Is there a way to build Camel routes that starts in suspended mode?
I'm looking so to say "declutch" at start up, then at some stage quickly start processing messages by just calling resumeRoute(routeId)
I could perhaps just create the route and then quickly call suspendRoute(routeId), after the route has been created, but at that stage, it would probably have consumed some messages (for instance in the case of JMS routes or polling consumer routes).
generally, you'd just disable the route by using autostartup(false)...
I assume you are asking though because you need the route started (warmed), but not active. In that case, then you should be able to use a custom route policy and some external variable to get this behavior

Resources