I'm sort of struggling with the dynamic routing concept and consumer rules.
So let's say I have a route with exchange data, and then I want to use a header from the exchange in a different route in the "from" endpoint.
I think it would look something like this:
Route 1:
from("file:/dir1")
...
.to ("direct:start");
Route 2:
from("direct: start")//get the old exchange data
.from("file:/dir1/?fileName=${header.myHeader}")//start consuming from a different endpoint using old exchange data
...
.to("direct: end);
So those steps seems right to me, but I feel like Im sort of polluting the exchange.
To me, Im using dynamic routing but Im also creating a new consumer at the same time. That means Im creating a new exchange right? So, how does camel know which exchange to pick and use in the rest of the route?
At first I thought it probably combined them, but I did a bit more digging and found that you actually need to use "enrich" to add to an existing exchange.
Can someone explain how camel handles this sort of scenario? If you have an example that would be great too. I searched for one in the camel package with no success.
You can achieve "dynamic from" with Content Enricher pattern.
Let's say your first route is used to add file name to the header for instance like this:
from("timer:trigger?repeatCount=1")
.routeId("define-file-name")
.setHeader("myHeader", constant("file.txt"))
.to("direct:start");
Then your second route can poll for that file using the information from the exchange header like this.
from("direct:start")
.routeId("poll-file")
.pollEnrich().simple("file://dir1?fileName=${in.header.myHeader}").timeout(10000)
.log("${body}");
Related
I am trying to understand the "best" way to accomplish this goal:
Using FileReadingMessageSource as the source
for each line in the file (using FileSplitter)
augment the row of data and send a REST request to another server and wait for a response.
Now sending the REST request is not a transform, I don't think it is an adapter, not a router.
What is it? what is the right way to design this?
Your question isn't clear, but I'll try my best.
To call REST service you should use HttpRequestExecutingMessageHandler, which essentially is a gateway EI pattern implementation. But from the application perspective it is a service any way. So, to handle message to the HttpRequestExecutingMessageHandler you should use #ServiceActivator. That is a term to proceed.
I have a situation where the client (.js) initiates a REST request and it is processed on the server side (.java). After processing, I would like to return a count to the client, which will be shown in a popup box if it is positive. How can such a construction be done? The idea I had was to set a named parameter on the HttpServletResponse object, but even this object is no where in scope in the .js code. Any ideas? I should also clarify that the primary purpose of the REST call is to download a file to the client. Thanks!
Do you want to send two things to your client - sending a file and also additional data? You haven't mentioned what framework (if any) you are using in backend to do this. You can use response header.
From your question, it seems like you don't have a good general-purpose way of responding to client requests on your server. I'd recommend you decide on a data format you'd like to use for all calls (e.g., JSON, XML, etc.) and stick with that across the board.
Once you've made that decision, you can encode your integer using whatever makes sense in your chosen format. For example, in JSON you might return: {"count":6}.
So, after reading some documentation and getting a lot of help from you guys here, I finally implemented a recipient list that chooses the endpoints dynamically (a dynamic recipient list):
http://camel.apache.org/recipient-list.html
http://camel.apache.org/recipientlist-annotation.html
In my code, MainApp_A generates reports every 10 seconds, and I want it to send the reports to all the servers at the same time, instead of doing it one by one. Thus I have developed the following route.
MainApp_A
main.addRouteBuilder(new RouteBuilder(){
#Override
public void configure() throws Exception {
from("direct:start").multicast().parallelProcessing()
.beanRef("recipientListBean", "route").end()
.log("${body}");
}
});
RecipientListBean
#RecipientList
public Set<String> route(String body) {
return servers; //returns a collection of several severs
}
I also checked the documentation for the Multicast pattern and the Dynamic route:
http://camel.apache.org/multicast.html
http://camel.apache.org/dynamic-router.html
Now I have a few questions and I am confused. So I have a few questions:
Is the recipient dynamic list simply the junction of the Multicast pattern with the Dynamic Route Pattern?
the multicast() call alone is purely sequential right? What is the difference between using multicast() and recipientList()?
For both multicast() and recipientList() to be concorrent I have to use parallelProcessing(). In my case, which is more efficient?
The dynamic router is used to evaluate which endpoints to send a specific message to at runtime, one endpoint at a time. Recipient List is a set of endpoints to send the very same message to.
I recommend you to read EAI patterns by Gregor Hohpe and Bobby Woolf for some background and insight.
http://www.eaipatterns.com/
Multicast allows a hard coded recipient list and recipientList(..) allows endpoints computed at runtime. Both are "recipient lists".
They will likely be equally efficient, if you don't have a lot of logic (say DB lookups) computing the endpoints in the recipient list - the invocation of the endpoints is likely by far the hardest part for Camel. Anyway - a static "multicast" is more readable.
In Camel, a reason for choosing one construct over another is often readability of the route. You should be able to look at a route and follow what it does (given you know EIP). At least, that's a good aim.
Scroll towards the end for the solution to the topic's problem. The original question was asking for a somewhat different thing.
As a part of a larger process, I need to fetch and link two related sets of data together. The way that the data is retrieved(dynamics crm, n:n relationships..) forces us retrieve the second set of the data again so that it will have all the necessary information. During a part of larger transformation of this data, I would like to access the http endpoint that is used to fetch the data from the crm, retrieve the second set of data and process it. I can get the endpoint through DefaultEndPointFactory like so:
DefaultEndpointFactory def = new DefaultEndpointFactory();
def.getInboundEndpoint("uri").getConnector;
But there is no method to actually send the mulemessage.
Solved:
The problem is that you can not set inbound properties on the MuleMessage, and the flow is depending on some of those to function(path, query params etc).
It seems you are able to inbound scoped properties with this:
m.setProperty("test", (Object)"test", PropertyScope.INBOUND);
Is there a way to make this approach work, or an alternative way to access the flow? I tried using mulecontext to get the flow:
muleContext.getRegistry().lookupFlowConstruct("myflow");
But it did not contain anything that looked useful.
Solution:
As David Dossot suggested in a comment of his answer, I was able to solve this with muleClients request method.
muleContext.getClient().request(url, timeout);
Then constructing the url as usual with GET parameters etc.
I'm not 100% sure about what you're trying to achieve but anyway, the correct way of using Mule transports from Java code is to use the MuleClient, which you can access with muleContext.getClient().
For example, the send method allow you to pass a properties map that are automatically added to the inbound scope. Behind the scene, Mule takes care of creating the endpoint needed for the operation.
Regarding the flow: what are you trying to do with it? Invoke it?
I have a situation in which I am generating a list of tasks in my camel producer then I need to send multiple exchanges containing each of these single task so that next component can process it.
Is there any pattern or producer in Camel which can serve my purpose.
I know there is DefaultTemplateProducer but my next part in route is processor which doesnot have an endpoint to point to so its of no use to me.
You could perhaps use the Camel splitter pattern to split the list and send it down the route?
Apache Camel Splitter
Its a bit unclear what you want. Maybe explain your question in more details. Also make sure to understand and read about the Enterprise Integration Patterns that Camel offers: http://camel.apache.org/eip
And as U2one says, the splitter pattern may be a good start accordingly to your question.