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.
Related
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}");
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}.
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'm currently writing some update polling stuff. I try to avoid writing even a simple REST-Interface for this (we're using much REST, still I'm not sure this is necessary here. Why write an interface for functionality already there?)
My idea was to open an HttpUrlConnection and check headers for file's last modified date. Apache obviously sends "Last-Modified" date in UTC. After checking the header I'd close the connection without actually retrieving the body. I only fear that this might bring up errors in Apache log, which would be quite inconvenient. I just wanted to ask for you opinion. Do you think this might work? Better ideas?
(I need system proxy support, so my only option seems to be HttpUrlConnection.)
Regards,
Stev
If you look at the HTTP protocol, you'll see that it has a HEAD request which does just what you need. The default for HTTP requests in the Java runtime is GET and it's not really easy to change that.
Have a look at HttpClient for a framework which allows you to send any kind of request.
You are almost right but your task is even simpler that what you are explaining. There is special HTTP method named HEAD. You just have to create the same request you need to retrieve your data but use HEAD instead of GET
This sounds pretty much, what the HEAD method in HTTP is for.
Citing from Wikipedia:
HEAD
Asks for the response identical to the one that would correspond to a GET request, but without the response body. This is useful for retrieving meta-information written in response headers, without having to transport the entire content.
Try just sending an http HEAD request. See here: http://blog.mostof.it/what-is-a-http-head-request-good-for-some-uses/
http://www.grumet.net/weblog/archives/http-head-example.html
I need to call a very complex service (It's a HL7 service) it's taking a lot of constant information.
To call from java I need to write all those information and its very time consuming.
I have sample generated XMLs, they have been filled with the constant information, I just need to change 1 parameter and send it.
More clearly; can I intercept the axis client before sending the request and change the "Request XML"?
Thanks,
If you're talking about testing, and your service uses SOAP and WSDL, maybe SOAP UI can help you. If you point it at a particular WSDL, SOAP UI can generate all the XML and attendent boilerplate for you. All you have to do is fill in the values you need and send the request. SOAP UI will display the response for you.