[ Camel: exception with Camel and Multicast EIP ]
I have a simple case where I use multicast with 3 beans. The thirs beans returns a RuntimeException but that's fine because it does not prevent the response from being returned back to the client (servlet call). However if I add a dynamicRouter between the from and the multicast then no more response is returned and I have Camel trying to handle the exception differently as before by resending the message to the multicast. This leads to conversion exceptions because in my aggregator I modify the IN message.
How can I keep the same behaviour and use a dynamicRouter?
Thanks
Below are some extract of the files I'm using:
camel.xml
<!-- Generic route listening to all the the requests -->
<route id="generic">
<from uri="servlet:///services?httpBindingRef=rsaHttpBinding"/>
<!-- <dynamicRouter>
<method ref="rsaDynamicRouter" method="route"/>
</dynamicRouter>
-->
<choice>
<when>
<simple>${property.rsaHeader.action} == 'getAccounts'</simple>
<to uri="direct:getAccounts#1.0" />
</when>
<when>
<simple>${property.rsaHeader.action} == 'createAccount'</simple>
<to uri="direct:createAccount#1.0" />
</when>
</choice>
</route>
<!-- Process getAccounts requests -->
<route>
<from uri="direct:getAccounts#1.0" />
<multicast strategyRef="accountAggregator">
<to uri="bean:accountBackend1?method=getAccounts" />
<to uri="bean:accountBackend2?method=getAccounts" />
<to uri="bean:accountBackend3?method=getAccounts" />
</multicast>
</route>
RsaDynamicRouter.java
public class RsaDynamicRouter {
public String route(Object body, @Properties Map<String, Object> properties) {
RsaHeader rsaHeader = (RsaHeader)properties.get("rsaHeader");
return "direct://" + rsaHeader.getAction() + "#" + rsaHeader.getVersion();
}
}
If I uncomment the dynamicRouter and comment the choice then I receive response is not sent back to client and I have the following exception which occurs 3 times:
Caused by: org.apache.camel.InvalidPayloadException: No body available of type: lu.bgl.example.mycamel.account.SearchCriteria but has value: [Account: number= 1314-1516-1718-1920, name=Larry Page, balance=30, Account: number= 2122-2324-2526-2728, name=Steve Jobs, balance=40] of type: java.util.ArrayList on: Message: [Account: number= 1314-1516-1718-1920, name=Larry Page, balance=30, Account: number= 2122-2324-2526-2728, name=Steve Jobs, balance=40]. Caused by: No type converter available to convert from type: java.util.ArrayList to the required type: lu.bgl.example.mycamel.account.SearchCriteria with value [Account: number= 1314-1516-1718-1920, name=Larry Page, balance=30, Account: number= 2122-2324-2526-2728, name=Steve Jobs, balance=40]. Exchange[Message: [Account: number= 1314-1516-1718-1920, name=Larry Page, balance=30, Account: number= 2122-2324-2526-2728, name=Steve Jobs, balance=40]]. Caused by: [org.apache.camel.NoTypeConversionAvailableException - No type converter available to convert from type: java.util.ArrayList to the required type: lu.bgl.example.mycamel.account.SearchCriteria with value [Account: number= 1314-1516-1718-1920, name=Larry Page, balance=30, Account: number= 2122-2324-2526-2728, name=Steve Jobs, balance=40]]
at org.apache.camel.impl.MessageSupport.getMandatoryBody(MessageSupport.java:101) ~[camel-core-2.11.0.jar:2.11.0]
at org.apache.camel.builder.ExpressionBuilder$38.evaluate(ExpressionBuilder.java:934) ~[camel-core-2.11.0.jar:2.11.0]
... 131 common frames omitted
Caused by: org.apache.camel.NoTypeConversionAvailableException: No type converter available to convert from type: java.util.ArrayList to the required type: lu.bgl.example.mycamel.account.SearchCriteria with value [Account: number= 1314-1516-1718-1920, name=Larry Page, balance=30, Account: number= 2122-2324-2526-2728, name=Steve Jobs, balance=40]
at org.apache.camel.impl.converter.BaseTypeConverterRegistry.mandatoryConvertTo(BaseTypeConverterRegistry.java:181) ~[camel-core-2.11.0.jar:2.11.0]
at org.apache.camel.impl.MessageSupport.getMandatoryBody(MessageSupport.java:99) ~[camel-core-2.11.0.jar:2.11.0]
... 132 common frames omitted
Answer 1
OK found it!
Dynamic router must return null otherwise it keeps on retrying to execute the route! I was expecting only one routing not multiple routing. Dynamic router is more like a coordinator.
Anyway here is my code for the Dynamic router which works:
public class RsaDynamicRouter {
public String route(Object body, @Properties Map<String, Object> properties) {
RsaHeader rsaHeader = (RsaHeader)properties.get("rsaHeader");
if (properties.get("endDynamicRouting") == null) {
properties.put("endDynamicRouting", "dummy");
return "direct://" + rsaHeader.getAction() + "#" + rsaHeader.getVersion();
} else {
return null;
}
}
}