How can i specify timout in apache camel route - java

I have an apache camel route i want to specify timeout for that route if that route does not get completed in that timeout need to raise timeout exception.
eq:
<route id="id">
<from uri="xyz"/>
//does some calculation call some other routes.
</route>

You may use Camel async API to implement what you want. Take a look at similar question (and solution) here.

Related

Camel Rest DSL Removing another extra Route

I am building a rest route on camel based on rest DSL. For example, based on the configuration, the rest route uses a direct component.
Rest Route
rest("resturi")
.post()
.to(direct:inbox);
resturi is dynamic
from(direct:inbox)
.process(camelprocessor)
.to(queue);
Is there any way we can remove the direct component and combine into one route.
I appreciate any advice regarding the above.
You can do that by adding .route()
.post().route().process(camelprocessor).to(queue);
But think from the feature reusable perspective, direct component will be the best choice.
https://access.redhat.com/documentation/en-us/red_hat_jboss_fuse/6.2/html/apache_camel_development_guide/restservices-restdsl
XML dsl example
<get uri="/some">
<route>
<setBody>
<constant>HW!</constant>
</setBody>
</route>
</get>
You can call your processor in this route.

How to log messages in Camel Spring DSL?

I'm using Camel 2.9.2 and defined OnCompletion route as follow:
<onCompletion onFailureOnly="true">
<log message="Nack On Failure:Status=$simple{header.CXAckStatus}:Error=$simple{header.CXAckError}" loggingLevel="DEBUG"/>
<process ref="ackMessageProcessor" />
<to uri="file://{{file.writer.dir}}"/>
</onCompletion>
Provided in
https://stackoverflow.com/questions/18877562/how-can-i-log-a-header-value-in-camel-using-spring-dsl solution for some reason doesn't work for me.
I can see some Camel traces relating to route definition Line 5309: 2017-07-28 11:14:53,830 DEBUG [Thread-11] (RouteService.java:311) - Starting child service on route: writingRoute -> OnCompletionProcessor[UnitOfWork(UnitOfWork(RouteContextProcessor[Pipeline[[Channel[Log(writingRoute)[Nack On Failure:Status=$simple{header.CXAckStatus}:Error=$simple{header.CXAckError}]], Channel[sendTo(Endpoint[log://'Ack On Success:Status=$simple{header.CXAckStatus}:Error=$simple{header.CXAckError}' ])],
but there is no actual logged message with header values to be found...
Is anything else I need to configure.
As per documentation, http://camel.apache.org/simple.html.
$simple{} is for versions before 2.9.2. and ${} thereafter.

Camel : direct connection the next route, can it be transacted?

I have two camel routes that are connected via a direct: link, not linked via JMS-Queue in this case.
Can I have a transaction between these two routes?
e.g.
<route id="fileRoute">
..
<to uri="direct:start">
</route>
<route id="directStartRoute">
<from uri="direct:start">
<to uri="http://myhost/mypath">
</route>
Yes if the first route starts with a transaction and you use direct between routes then the transaction still apply. The transaction manager requires the work that happens in the transaction happens on the same thread and therefore needs to be synchronous routing, which is what direct do.

REST EndPoint for Apache Camel

I'm trying to create en REST endpoint with Apache Camel. I already have a REST service that return me JSON content and I want this endpoint to get it. My problem is that I don't know what's happening when my Camel route is built.. For the moment, it doesn't do anything. Here is my code :
restConfiguration().component("servlet")
.bindingMode(RestBindingMode.json)
.dataFormatProperty("prettyPrint", "true").host("localhost")
.port(9080);
rest("/ContextServices/rest/contextServices/document")
.consumes("application/json").produces("application/json")
.get("/testContext/557064c8f7f71f29cea0e657").outTypeList(String.class)
.to("bean:processor?method=affiche")
.to(dest.getRouteTo());
I'm running my REST service on a local Tomcat on port 9080, my full URL is
/ContextServices/rest/contextServices/document/{collection}/{id}.
I've tried to read the documentation but there is two syntax and both don't work:
from("rest:get:hello:/french/{me}").transform().simple("Bonjour ${header.me}");
or
rest("/say")
.get("/hello").to("direct:hello")
.get("/bye").consumes("application/json").to("direct:bye")
.post("/bye").to("mock:update");
The first is Java DSL, the second is REST DSL, what's the difference ?
Thanks a lot !
First of all, REST component itself is not a REST implementation.
It just declares language to describe REST endpoints.
You should use actual implementation of REST, something like Restlet (see the full list here)
I can be wrong, but AFAIR, REST endpoint is only for the case when you want to listen for REST requests from another application.
What you need is to make request to REST endpoint and process it.
The question is: when do you want to trigger request?
Is it some event, or may be you want to check external REST service periodically?
For the latter case I use the following pattern:
<route>
<from uri="timer:polling-rest?period=60000"/>
<setHeader headerName="CamelHttpMethod">
<constant>GET</constant>
</setHeader>
<recipientList>
<simple>http://${properties:service.url}/api/outbound-messages/all</simple>
</recipientList>
<unmarshal ref="message-format"/>
<!-- do something with the message, transform it, log,
send to another services, etc -->
<setHeader headerName="CamelHttpMethod">
<constant>DELETE</constant>
</setHeader>
<recipientList>
<simple>http://${properties:service.url}/api/outbound-messages/by-id/${header.id}</simple>
</recipientList>
</route>
Sorry for the example with http component instead of REST.
I simply copy-pasted it from my working project, which uses pure http.
I suppose, rewriting this via something like Restlet or CXF component.

camel generic producer (to be routed via spring xml config)

I want to send a generic message from from java, that is then routed by camel. So far the messages always went to an activemq topic (Example 1) but in future I want to be able to change the route (i.e. sending the message to a rest webservice instead) without modifing the source code (via spring xml configuration). So I expect to do ~something like~ Example 2. How would I do this?
Example 1: (how it's done so far)
#EndpointInject(uri="activemq:topic:IMPORTANTEVENTS")
ProducerTemplate producer;
producer.sendBody("Hello world!");
Example 2: (how it is supposed to look like - more or less)
#EndpointINject(uri="myevents")
... (as above)
XML config:
<route id="SysoutRoute">
<from uri="myevents"/>
<to uri="activemq:topic:IMPORTANTEVENTSS"/>
</route>
You can use property placeholders: http://camel.apache.org/using-propertyplaceholder.html - then the java source code do not need to be changed, but the uri is defined in a .properties file which you can then easily change
Ok got it working. It es actually quite easy by using direct: component(http://camel.apache.org/direct.html)
#EndpointInject(uri="direct:outMessage")
ProducerTemplate producer;
Now I can send messages:
producer.sendBody("Hello world!");
And route them via Spring xml config e.g. like this:
<route id="outMessage">
<from uri="direct:outMessage"/>
<to uri="stream:out"/>
<to uri="activemq:topic:IMPORTANTEVENTS"/>
<to uri="restlet:http://localhost:8888/status/?restletMethod=post"/>
</route>

Categories

Resources