We need a generic way of handling exceptions/errors/bad responses from outgoing http calls in camel multicast subroutes by checking which route failed and at error handler dynamically - .toD("routeId) - send back an exchange with the same content to that route periodically until success. I went thru the accessible methods/fields of the Exchange object at aggregation and the only usable object from properties seems to be "CamelStreamCacheUnitOfWork" which has a field "prevRouteContext" which stores the route id of the multicasted route e.g. "direct:cancel-item":
.multicast().aggregationStrategy(new MyAggregationStrategy())
... other routes...
.to("direct:cancel-item"); <-- exception happens inside this route
Problem: there is no public getter on this field but it would be very useful however there are push/pop methods but they are changing the order of contexts so it it not safe to use. Question: is it safe to use reflection to access the "routeId" of the previous context?
public class MyAggregationStrategy implements AggregationStrategy {
@Override
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
DefaultUnitOfWork camelStreamCacheUnitOfWork = newExchange.getProperty("CamelStreamCacheUnitOfWork", DefaultUnitOfWork.class);
if (newExchange.isFailed() || otherCustomErrors) {
Field field = camelStreamCacheUnitOfWork.getClass().getDeclaredField("prevRouteContext");
field.setAccessible(true);
RouteContext value = (RouteContext)field.get(camelStreamCacheUnitOfWork);
String id = value.getRoute().getId();
}
}
}
Aucun commentaire:
Enregistrer un commentaire