I'm newbie to Apache camel and wanted to Implement the toD() which is to dynamically frame the URI and add request params values from Beans..
Code snippet below -
from("quartz2://timer?cron=0+0/1+++*+?")
.noAutoStartup().routeId(ROUTE_ID).log("Route Started")
.toD(http://localhost:3420/contextpath?from=${bean:bean.from} "+ "&size=${bean:bean.size}")
.process(processor)
Seems like, on every hit via Quartz the same URL is being triggered and hence I see duplicate values saved to DB.
Please suggest why Dynamic uri is not working as expected.
Am calling the processor, computing and setting the Bean values which i get from Response of Endpoint. But when the next time Quartz hits the url, the bean values are not updated and takes the default value
. Bean definition is usual getter setter, and registration is I have used Simple registry
SimpleRegistry simpleRegistry = new SimpleRegistry ();
// create CamelContext
context = new DefaultCamelContext (simpleRegistry);
simpleRegistry.put("bean", bean);
Thanks in Advance
In order to use dynamic URI on a camel-route you must include your variable inside a Simple expression.
Since Camel 2.16.0 release endpoint implementation toD() supports the Simple expression language so you can define a dynamic-URI as message-endpoint:
from("quartz2://timer?cron=0+0/1+++*+?")
.noAutoStartup()
.routeId(ROUTE_ID)
.log("Route Started")
.toD( "http://localhost:3420/contextpath?from=${bean:bean.from}&size=${bean:bean.size}" );
So the expressions ${bean:bean.from} and ${bean:bean.size} should get directly interpolated by using Bean language inside your URI-string. This bean-component bean: tells Camel to get the bean registered with your specified name bean and call the specified methods from and size.
Apache Camel: Rest DSL, section Using Dynamic to() has also a note:
Note: we need to use .endRest() to tell Camel where the route ends, so we can go back to the Rest DSL and continue defining REST services.
Otherwise you could implement that dynamic endpoint using simple inside your regular to(). See Apache Camel: How to use a dynamic URI in to().
Related
I need to create an MasterEndpoint from a given (as Endpoint instance in Java) FileEndpoint.
Normally i create an class extending the desired endpoint and call all needed setter (e.g. to set context) from with in constructor or in an init method.
Sometimes i create a method that uses getContext().getEndpoint("name", ClazzOfEndpoint.class) within the route builder.
But how to do this with MasterEndpoint (preferable without using string literals/constants)?
The problem with extending MasterEndpoint is the unusual constructor it uses. The problem with using getEndpoint is: how to connect the returned master endpoint to the FileEndpoint?
You cannot really do this as that master component is not designed for being build programmatically. You get the endpoint via configuring it using a string uri. This is also the recommended way in Camel to setup and define endpoints. Don't program them manually.
I found a way that suits my needs:
First create the master endpoint together with it's child:
masterEndpoint = context.getEndpoint("master:fileLock:file:" + rootFolder, MasterEndpoint.class);
To programmatically configure the child endpoint (in my case FileEndpoint) obtain it from master and configure it:
fileEndpoint = (FileEndpoint) masterEndpoint.getEndpoint();
fileEndpoint.setAutoCreate(false);
fileEndpoint.setAntInclude(ANT_INCLUDE);
fileEndpoint.setMove(doneFolder);
fileEndpoint.setMoveFailed(errorFolder);
It would be extreme cumbersome (and error prone) to configure it with strings.
The documentation for "intercepts" says:
The interceptSendToEndpoint is dynamic hence it will also trigger if a
dynamic URI is constructed that Camel was not aware of at startup
time.
The interceptFrom is not dynamic as it only intercepts input to
routes registered as routes in CamelContext.
Is there an idiomatic way to create something equivalent to a dynamic "from intercept"?
Stepping back, here is what I want to do: intercept every time a message is written to or read from a jms component, where the URI matches a certain wildcard pattern.
Then use something else than interceptFrom, such as event notifier where you can get a notification when sending/sent/received etc.
http://camel.apache.org/advanced-configuration-of-camelcontext-using-spring.html
http://camel.apache.org/eventnotifier-to-log-details-about-all-sent-exchanges.html
i am using cxf as a producer in an apache camel route with WS-Addressing.
I know that it is possible to set the SoapAction Header inside the route via (just as example might be wrong)
...
.setHeader("SoapAction").constant("anysoapactionwanted")
.to("cxf...
is it possible to the same with the WS-Addressing Action field? Because i noticed it is sent with the wrong value. There are 2 WS-Addressing Action values i need to put in and it is decided in the camel route which one to use.
You must be deciding the the required operation based on some value. In that case use Choice-When conditional block to derive correct action.
I'm attempting to use the Recipient List EIP to dynamically generate the consumer endpoint URI during runtime based on configuration entries in a database (http://camel.apache.org/how-to-use-a-dynamic-uri-in-to.html). I've got a number of routes that I want to handle this way so I'd like to build something that can handle multiple routes generically.
Therefore, my idea is to keep an in memory map of these URI values keyed on some type of identifying information (original endpoint URI seems like a logical choice) which would be updated if/when the database is updated to keep the routes in sync, and prevent having to go to the database for every exchange. Using the RouteBuilder, I am setting up the route with the recipient list and Bean expression.
from(endpointUri).recipientList(bean(MyBean.class, "getUri"));
I know that I can capture various objects such as the exchange, body, headers (as long as I know the name), etc using the Bean binding for the getUri method. Is it possible to somehow get the original endpoint URI value so that I can use it as a key to fetch the correct consumer endpoint?
The Exchange interface has getFromEndpoint() method which returns an Endpoint. The Endpoint interface has getEndpointUri() method which returns a String. Perhaps that's what you need? If that's not sufficient, you could set header value(s) at some point and then subsequently retrieve them later in your route.
I have create a camel context; this camel context is having 4 routes. These all routes are related to complete a same feed processing operations. Now I have got a requirement to share a database object within the routes.
This object is supposed to be initialized at the time for context creation and should be available for all routes to validate data from.
So far; I have create a org.apache.camel.StartupListener and registered with the context. This listener will add some properties to the context. But my requirement does not ends with the only string values. I have to put a object in the context.
What should I do to add this object in the Camel Context?
You can always create a normal bean to keep reference to your shared object and get the bean like this in all routes:
MyBean myBean=exchange.getContext().getRegistry().lookup("MyBean",MyBean.class);
Or directly add your shared object as a bean (if possible)