How to use Apache Camel to fit this messaging logic? - jms

I'm using the Apache Camel DSL and trying to figure out how to route a message, like so. Here's the logic, without any Camel DSL.
Message enters queue
Call bean
If bean doesn't have exception
Call another bean with original message
My issue is that I'm not sure how to get to step 3.a using the original message. The call to bean changes the message. I can use a multicast, two pipelines, and header variables, but from what I can tell those header variables are in scope within a pipeline.
Any ideas are appreciated! Thanks!

You can use the multicast eip in sequence mode
<from uri="somewhere"/>
<multicast>
<to uri="bean:beanA"/>
<to uri="bean:beanB"/>
</multicast>
The multicast runs in sequence mode by default, so first invoking beanA. And if that is a success, it invokes beanB, but with the original message (a copy of the same message that we called beanA with).
You can read about the multicast EIP here: http://camel.apache.org/multicast.html

from your 2nd bean, just call exchange.getUnitOfWork().getOriginalInMessage()

I ended up storing the content of the message in a variable and setting the message back to that content after it was changed, like so...
<!-- store original content in header variable -->
<camel:setHeader headerName="marc"><camel:simple>${body}</camel:simple></camel:setHeader>
<!-- run some logic, output gets saved as another header variable, message as this point is now that output -->
<camel:bean ref="getPidsForUpdate"/>
<camel:setHeader headerName="pids"><camel:simple>${body}</camel:simple></camel:setHeader>
....set a bunch of variables based on that output, and then
<!-- get original message to run some more logic-->
<camel:setBody><camel:simple>${headers.marc}</camel:simple></camel:setBody>

Related

Spring Integration filter

in my program, there's request to an API and then message in the incoming channel is stored to a file. My problem is I get some kind of error in my payload for some request. I want to store such error file in another directory. How can I add a filter expression, so that such payload is stored in another directory. My code is:
<int:chain input-channel="abcconnect">
<int:filter id="errorout" expression="payload.containsKey('errorCode')"/>
<file:outbound-channel-adapter
id="apiResponseWriter"
directory="${error.path}"
filename-generator-expression="'abc-' + T(java.lang.System).currentTimeMillis() + '.json'"
delete-source-files="false" />
</int:chain>
The error in the payload file is: {"TwitterApi":{"errorCode":400,"errorMsg":"beginTime or endTime is later than now(1472041145)","success":false,"cost":11,"params":
The filter lets the message to pass and go ahead in the flow if its predicate returns true. In your case it looks like messages with the errorCode should go down the flow.
So, I guess you have to negate the expression and also add discard-channel to let the false message to be send to different flow for analyze, processing or storing to a different file like your requirement.

How to have dynamic routing in datapower

Well I am quite new to datapower(IBM Websphere xi50) and i have got struck as to how to route dynamically.Suppose we have configured dynamic backend then a client asks for some service so how to route it .
Is it through XSLT or yhrough certain policy creation I am not getting any clear idea regarding it?
Thanks
Normally you use "var://service/routing-url" and not as Ajitabh answered "dp:url-open".
You can also use the "Set variable" action instead of XSLT.
dp:url-open is used to "fork" a message, not to route it. If you use dp:url-opento route you must also set "skip-backside = 1"!
See sample for XSLT:
<xsl:variable name="destURL" select="concat('http://127.0.0.1:',$Transformer-port,'/', $usrTransformer)" />
<!-- Set backend URL -->
<dp:set-variable name="'var://service/routing-url'" value="$destURL" />
I create an XSLT that looks at the different elements in the incoming message to help determine the destination. I then invoked the DataPower extension method <dp:set-variable('var://service/routing-url') select="yourUrlHere"/>. I put that XSLT into a routing action.
You can do it through XSLT . The steps to do it is as following (logically)
Step 1: Determine the criteria based on which you want routing [it could be URI or any part of message]
Step 2: Use datapower built in function dp:open-url to send traffic to target.
Step 3: Get the response and process it.
There are other ways to do it as well but this is the most straight forward way.

In a blueprint.xml route, call a propertyplaceholder using an xpath returned value

I have a blueprint.xml in which I write some routes for an ESB.
I want to get values from an XML file passed into the route.
I want to then use these values to make up a dynamic property key name and call the properties file and get the matching property (all within the route). I want to avoid having to create a Java processor due to the overhead of instantiating this each time.
Essentially I want to do this:
<from uri="file:C:/myfilelocation?"/>
<to uri= {{<xpath>//company</xpath>+<xpath>//branch</xpath>}}/>
So in blueprint you call a property using {{}}
I am trying to place the xpath values as the property key inside of the property {{}} tags. In my properties file I have a mapping for each company/branch combination like so:
company1branch1=http://thiscompany.com
company2branch2=http://someothercompany.com
Any way to do this, e.g. some sort of escape characters?
The < to > is for static uris, if you want to use dynamic runtime computed uris, then you should use the recipient list EIP: http://camel.apache.org/recipient-list.html that allows to send a message to a recipient calculcated at runtime.
This is also describer in this FAQ: http://camel.apache.org/how-do-i-use-dynamic-uri-in-to.html
Though with the xpath, you would need to set them as headers first.
Something a like:
<setHeader headerName="company">
<xpath resultType="java.lang.String">/xxxx</xpath>
</setHeader>
...
<recipientList><simple>{{${header.company}${header.branch}}}</simple></recipientList>
Also the recipient list can send to 2+ destinations, the separator is by default comma. But you can configure that. See the links above.

Re-marshalling protobuf within Camel route

I'm hoping someone can explain a bit more about how to configure Camel to marshal and unmarshal data. I have a route that calls a bean in order to determine a recipientList. This is based on the contents of the message (a protobuf).
The route config looks like:-
<route id="Splitter">
<from uri="activemq:notification.splitter" />
<unmarshal ref="notificationProto" />
<recipientList>
<method bean="NotificationSplitter" method="splitNotification" />
</recipientList>
</route>
The bean works fine, but the downstream routes complain that:-
org.apache.camel.RuntimeCamelException: java.lang.RuntimeException: Unable to find proto buffer class
The downstream routes have exactly the same protobuf dataFormat config as the route above. If I route directly to the downstream queues (i.e. bypass the bean and hardcode the "to" queues), which means I can also skip the unmarshalling step, it works fine.
I guess that I need to re-marshal the data before Camel puts the messages onto the destination queues but I don't know how to configure this within the XML. I've tried simply adding...
<marshal ref="notificationProto" />
...after the recipientList has been determined but it doesn't do it (I assume because Camel has already dispatched the messages by then).
An alternative could be to do the unmarshalling from within the bean as then the data on the exchange will presumably remain unaltered. I'm not quite sure how to do this. Would it work?
Thanks for any tips.
J.
Yeah the data formats are not an endpoint that's easy to send a message to, as otherwise you can use the Routing Slip EIP pattern instead, and send the message to the data format first, and then the destination. http://camel.apache.org/routing-slip.html
Though you could have a little route
<route>
<from uri="direct:marshalMe"/>
<marshal ref="notificationProto" />
</route>
And then use the routing slip, to set to "direct:marshalMe,whereYouWannaGoNext".
An alternative is to use an interceptor, and intercept sending to endpoints (you can filter by wildcards, or reg exps), and then do the marshal first. http://camel.apache.org/intercept
See that link for more details.

Apache Camel: how store variable for later use

while 'playing around' with Camel using Spring DSL, I came across the following problem. Suppose the expected message flow looks like this:
client sends HTTP POST message with XML body to CAMEL
CAMEL proxies HTTP POST message towards server, with the URI slightly adapted using
info from the received XML body (eg: use XPATH to filter out a certain parameter)
after CAMEL has received a reply, CAMEL sends HTTP PUT message towards server, using parameters out of the XML body received in 1
So something like:
<route>
<from uri="...">
<to uri="...">
<to uri="...">
</route>
Question: how do I store the parameters in Spring DSL in step 1, so that I can use them later in step 3 ?
So, I would like to extract XML parameters out of the XML body of the message received in step 1 and put them into variables, which I then later on can use to compose the message to be sent in step 3.
For extracting the parameters, I was thinking of using XPATH. That looks ok, but I just don't see how to put the output of the XPATH into a variable and then use that variable later on ... (syntax ??)
Note: as you can see, my development knowledge is rather limited ... sorry for that. But it would still be great if someone could help with this :).
you can set store data in the Exchange properties or message headers like this...
.setHeader("ID", XPathBuilder.xpath("/order/#id", String.class))
.setProperty("ID", XPathBuilder.xpath("/order/#id", String.class))
and then retrieve them in a bean/processor from the Exchange like this...
String propId = (String) exchange.getProperty("ID");
String headerId = (String) exchange.getIn().getHeader("ID"); }
I leave you some examples:
<setHeader headerName="token">
<constant>someValue</constant>
</setHeader>
<setHeader headerName="userName">
<simple>${properties:userName}</simple> //from config
</setHeader>
<setProperty propertyName="bodyBkp">
<simple>${in.body}</simple>
</setProperty>
<setProperty propertyName="orderNumber">
<xpath resultType="String">//item[1]/orderNumber/text()</xpath>
</setProperty>
Getter
${exchangeProperty[orderNumber]}
${in.headers.token}
Documentation
Check the simple expression language:
http://camel.apache.org/simple.html
Sometimes looking at the test cases of Camel can be helpful as well, in particular for Spring DSL:
setProperty with Spring DSL
setHeader using XPATH with Spring DSL
simple expression language test

Resources