Apache Camel Pass Parameter to multicust uri's with restlet - apache-camel

The example with GroupedExchangeAggregationStrategy() worked fine with me. But now the challenge is to pass param value to the uri's. below is the code.
from("restlet:http://localhost:8089/createCustomer/{foo}")
.enrich("direct:serviceFacade")
.process(new Processor() {...})
.end();
from("direct:serviceFacade")
.multicast(new GroupedExchangeAggregationStrategy()).parallelProcessing()
.enrich("restlet:http://localhost:8080/CamelRest/rest/restService/addressInfo/${header.foo}").enrich("restlet:http://localhost:8080/CamelRest/rest/restService/accountInfo/${header.foo}")
.end();
I am getting an error message:
Header with key: header.foo not found in Exchange. Exchange[Message:
[Body is null]]

Finally I got it Right.
from("direct:serviceFacade")
.multicast(new GroupedExchangeAggregationStrategy()).parallelProcessing()
// .bean(RecipientListBean.class, "route")
.enrich("restlet:http://localhost:8080/CamelRest/rest/restService/addressInfo/{foo}")
.enrich("restlet:http://localhost:8080/CamelRest/rest/restService/accountInfo/{foo}")
.end();
That would do the magic..

Related

Camel route not continuing after single choice().when()

I have a route that looks like this:
from(getEndpoint())
.id(endpoint)
.filter(notDeleted)
.process(get)
.filter(exists)
.choice()
.when(hasProperty)
.wireTap("direct:" + AlternateRoute.ENDPOINT)
.end()
.log("Got here")
.process(postProcessor)
.bean(dao, "save")
.end();
I've tried a few different combinations of end() and endChoice() but no matter what I do nothing after the .choice()/.wireTap() seems to run. I don't see the "Got here" line logged and I don't see my postProcessor or dao get hit.
What am I doing wrong?
Choices are weird in apache camel when you are using java DSL.
Try to simplify your route processing the property in another route:
from(getEndpoint())
.id(endpoint)
.filter(notDeleted)
.process(get)
.filter(exists)
.wireTap("direct:" + AlternateRoute.ENDPOINT)
.log("Got here")
.process(postProcessor)
.bean(dao, "save")
.end();
from("direct:" + AlternateRoute.ENDPOINT)
.choice()
.when(hasProperty)
.log("Do something")
.endchoice();

Exception from recipientList EIP of camel not caught at route level

Camel Version 2.22.0
Runtime: SpringBoot : 2.0.2.RELEASE
JDK version: 1.8.0_121
EIP: recipientList.
Problem: Exception raised from parallel process of recipientList is not caught at route level onException clause.
Below is the DSL
#Override
public void configure() throws Exception {
restConfiguration().clientRequestValidation(true)
//.contextPath("/pss/v1.0/")
.port("8080").host("0.0.0.0")
.enableCORS(true)
.apiContextPath("/api-doc")
.apiProperty("api.title", "Test REST API")
.apiProperty("api.version", "v1")
.apiContextRouteId("doc-api")
.component("servlet")
.bindingMode(RestBindingMode.json);
rest("/api/").clientRequestValidation(true)
.id("api-route")
.consumes("application/json")
.get("/bean/{name}")
.bindingMode(RestBindingMode.json)
.to("direct:remoteService");
from("direct:remoteService")
.onException(Exception.class).handled(true)
.log("Exception Caught : ${exception.message}")
.end()
.recipientList(constant("direct:route1, direct:route2"), ",").parallelProcessing().aggregationStrategy(new GroupedBodyAggregationStrategy())
.stopOnException()
.end()
.log("The final Exchange data : ${exception.message}");
from("direct:route1")
.setHeader( Exchange.CONTENT_ENCODING, simple("gzip"))
.setBody(simple("RESPONSE - [ { \"id\" : \"bf383eotal length is 16250]]"))
.log("${body}");
from("direct:route2")
.log("${body}")
.process(e-> {
List<String> myList = new ArrayList();
myList.add("A");
myList.add("b");
myList.add("C");
e.getIn().setBody(myList);
})
.split(body())
.parallelProcessing(true)
.aggregationStrategy(new GroupedBodyAggregationStrategy())
.stopOnException()
.log("${body}")
.choice()
.when(simple("${body} == 'b'"))
.throwException(new Exception("jsdhfjkASDf"));
}
Try make onException as global like this:
onException(Exception.class).handled(true)
.log("Exception Caught : ${exception.message}")
.end();
from("direct:remoteService")
.recipientList(constant("direct:route1, direct:route2"), ",").parallelProcessing().aggregationStrategy(new GroupedBodyAggregationStrategy())
.stopOnException()
.end()
.log("The final Exchange data : ${exception.message}")
;
UPD: So you need to disable error handlers in recipient routes. Try like this (can't insert normally code sample)
That's a classical mistake: (exactly like the split EIP) each recipient will process a copy of the original Exchange. Any failure on these copies will not affect (raise an exception on) the route processing the master Exchange, as every single exchange runs in a completely separate unit of work.
If you enable the "shareUnitOfWork" option (on the recipientList), exceptions should be propagated.

using camel http handling CachedOutputStream

hi~ i am using camel http component. and i can't extract body message.
here is my code
.log(LoggingLevel.INFO, "ToUri ===> ${body}")
.toD("${body}")
.log(LoggingLevel.INFO, "Result ===> ${body}")
.process(new Processor() {
public void process(Exchange exchange) throws Exception {
long startTime = System.currentTimeMillis();
Message inboundMessage = exchange.getIn();
Object body = exchange.getIn().getBody();
String msg = inboundMessage.getBody(String.class);
System.out.println("body:"+body);
System.out.println("getInBody msg:"+msg);
System.out.println("getInBody body:"+body.toString());
=======================================================================
body : org.apache.camel.converter.stream.CachedOutputStream$WrappedInputStream#28936ba4
getInBody msg:
getInBody bodybodybody:org.apache.camel.converter.stream.CachedOutputStream$WrappedInputStream#28936ba4
the log is good works. like this
09:56:53.523 INFO route1 - ToUri ===> https://translation.googleapis.com/language/translate/v2?key=tesetKey&source=en&target=ja&q=hi
09:56:54.545 INFO route1 - Result ===> {
"data": {
"translations": [
{
"translatedText": "こんにちは"
}
]
}
}
i want to extract translatedText using camel.
how can i handle CachedOutputStream and what is this?
i search camel doc.and cant understand.please give me a hint to solve my problem.
thanks.
See stream-caching for information about CachedOutputStream: http://camel.apache.org/stream-caching.html
To get the message body as string from the processor, you just do
String body = exchange.getIn().getBody(String.class);
That will tell Camel that you want the message as a String and it will automatic covert the message body from CachedOutputStream to String. Then you can grab that text you want via regular Java code.
Also note there is jsonpath you can use to work with json data and get information, however its syntax can take a little bit to learn: http://camel.apache.org/jsonpath
You already took the stream data (with .log) before calling processor. Stream data can only fetched once apparently. Try remove the log step and you can get in the processor:
.log(LoggingLevel.INFO, "Result ===> ${body}")
.process(new Processor() {
after spend 2 days experimenting
you can use convertBodyTo(Class<?> type) method for that, like this
.log(LoggingLevel.INFO, "Result ===> ${body}")
.convertBodyTo(String.class)
.process(new Processor() { ... }

apache camel idempotent consumer key removal

I have a setup like below.
The issue I have is that the key from the Idempotent Repository is not being removed when an exception is thrown (on line stated below) when using seda component within OnException. When I change to use direct within the OnException the key is removed from the cache. On both trials the email is also being sent correctly.
My queries are:
why is the key not being removed from the repository cache when using seda within the OnException?
is there an issue with using seda within the OnException?
Here is the routes:
MyRouteClass1
onException(Exception.class)
.setHeader("subjectText", simple("failure email!"))
.to("seda:notifySupportOnFailure")
.end();
from("direct:findWorkItems")
.bean(someService, "findWorkItems")
.split(body())
.throttle(1).timePeriodMillis(5000L)
.to("direct:handleWorkItem")
.choice().when(header("resultId").isGreaterThan(0))
.bean(someService, "updateWorkItemToHandled")
.end();
from("direct:handleWorkItem")
.idempotentConsumer(simple("${body.workItemId}"), duplicatesRepo)
.bean(someService, "handleWorkItem") // e.g. exception would be thrown within here
.setHeader("resultId", body())
.end();
MyRouteClass2
from("seda:notifySupportOnFailure")
.setHeader("from", simple("sender#mail.com"))
.setHeader("to", simple("recipient#mail.com"))
.setBody(simple("Failure:\n ${exception.message} \n\ndetail: \n${body}"))
.to("smtp://localhost")
.log("Failure email now sent.")
.end();
I have also attempted another workaround. I have modified the onException clause as per below. This does allow the key to be removed from the Repository when the exception is thrown. I am wondering if this is a correct approach ?
onException(Exception.class)
.setHeader("subjectText", simple("failure email!"))
.multicast().to("seda:notifySupportOnFailure").end()
.end();
A work around would be to put the idempotentConsumer call in your parent route 'findWorkItems' and setting the completionEager flag on it to true.
onException(Exception.class)
.setHeader("subjectText", simple("failure email!"))
.to("seda:notifySupportOnFailure")
.end();
from("direct:findWorkItems")
.idempotentConsumer(simple("${body.workItemId}"), duplicatesRepo).completionEager(true)
.bean(someService, "findWorkItems")
.split(body())
.throttle(1).timePeriodMillis(5000L)
.to("direct:handleWorkItem")
.choice().when(header("resultId").isGreaterThan(0))
.bean(someService, "updateWorkItemToHandled")
.end();
from("direct:handleWorkItem")
.bean(someService, "handleWorkItem") // e.g. exception would be thrown within here
.setHeader("resultId", body())
.end();

Camel - Split() and doCatch(....) does not work

I'm trying to build a route that tries to validate an xml and if everything is correct then has to split this file otherwise an exception is thrown and it has to do something else. So I did the following:
from("file:"+fileOutboxTransformed+"?preMove=inprogress&move="+backupFolderTransformed+"/"+labelMessageType+"_${date:now:yyyyMMddHHmmssSSS}-${file:name.noext}.${file:ext}")
.log(LoggingLevel.INFO, "Got transformed file and sending it to jms queue: "+queue)
.doTry()
.to("validator:classpath:"+validator)
.split(xPathMessageTypeSplit)
.to("jms:"+queue+"?jmsMessageType=Text")
.doCatch(ValidationException.class)
.log(LoggingLevel.INFO, "Validation Exception for message ${body}")
.to("xslt:classpath:"+transformationsErrorAfter)
.split(xPathNotificationSplit)
.to("file:"+fileOutboxInvalid+"?fileName=${file:name.noext}-${date:now:yyyyMMddHHmmssSSS}.err2")
.end();
But it does not compile (if I do not use the split then it compiles and works) and the error is:
The method doCatch(Class<ValidationException>) is undefined for the type ExpressionNode
So I tried the following
from("file:"+fileOutboxTransformed+"?preMove=inprogress&move="+backupFolderTransformed+"/"+labelMessageType+"_${date:now:yyyyMMddHHmmssSSS}-${file:name.noext}.${file:ext}")
.log(LoggingLevel.INFO, "Got transformed file and sending it to jms queue: "+queue)
.doTry()
.to("direct:validate")
.doCatch(ValidationException.class)
.log(LoggingLevel.INFO, "Validation Exception for message ${body}")
.to("xslt:classpath:"+transformationsErrorAfter)
.split(xPathNotificationSplit)
.to("file:"+fileOutboxInvalid+"?fileName=${file:name.noext}-${date:now:yyyyMMddHHmmssSSS}.err2")
.end();
from("direct:validate")
.to("validator:classpath:"+validator)
.to("direct:split_message");
from("direct:split_message")
.split(xPathMessageTypeSplit)
.to("jms:"+queue+"?jmsMessageType=Text");
This time I get the error of duplicate endpoint
org.apache.camel.FailedToStartRouteException: Failed to start route route312 because of Multiple consumers for the same endpoint is not allowed: Endpoint[direct://validate]
do you have any idea on how to solve this problem?
In order to get back to the doTry() block from a split() block (or choice or other nested type), you need to use the endDoTry(). Contrary to its name, this method will end the nested split block and return back to the doTry() DSL.
I'd use the first route you posted with these changes:
from("file:"+fileOutboxTransformed+"?preMove=inprogress&move="+backupFolderTransformed+"/"+labelMessageType+"_${date:now:yyyyMMddHHmmssSSS}-${file:name.noext}.${file:ext}")
.log(LoggingLevel.INFO, "Got transformed file and sending it to jms queue: "+queue)
.doTry()
.to("validator:classpath:"+validator)
.split(xPathMessageTypeSplit)
.to("jms:"+queue+"?jmsMessageType=Text")
.endDoTry()
.doCatch(ValidationException.class)
.log(LoggingLevel.INFO, "Validation Exception for message ${body}")
.to("xslt:classpath:"+transformationsErrorAfter)
.split(xPathNotificationSplit)
.to("file:"+fileOutboxInvalid+"?fileName=${file:name.noext}-${date:now:yyyyMMddHHmmssSSS}.err2")
.endDoTry()
.end();
Your second try seems fine. The error you're getting is caused by two routes beginning with from("direct:validate")
Don't you have an other route in your application consuming from the same endpoint?
Edit : Try to name it differently, maybe validate exists already (in your app or inside camel)

Resources