I route request to method1 of bean myBean1.This bean return some data and i want send it to next "to" to ejb method like dataFromMyBean1. How can i do this? It should look something like this:
<route>
<from uri="netty4:tcp://0.0.0.0:9555?textline=true&sync=true"/>
<to uri="bean:myBean1?method=method1"/>
<to uri="ejb:beanName?method=methodName1(arg1, arg2, arg3, dataFromMyBean1)"/>
</route>
Read about bean parameter binding, how to specify args in method signatures.
http://camel.apache.org/bean-binding.html
There is some limitation as what you can specify. See the section Parameter binding using method option on that link.
If you need something more, then you can call the bean from real java code, or possible use an inlined groovy script etc.
Related
Looks like a weird issue or the docs are missing
Case 1
from("direct:ROUTE1").to("someAPI").to("direct:ROUTE2");
from("direct:ROUTE2").log("${body}"); // BODY is printing
Case 2
from("direct:ROUTE1").to("someAPI").to("direct:ROUTE2").log("${body}");
from("direct:ROUTE2").log("${body}"); // BODY is empty
Does adding log clear the exchange body??
As #Spara and #Claus suggested and to save the hassle on how to enable Stream caching.
Below is the sample code:
Using Java DSL for Single route
from("direct:ROUTER1")
.streamCaching()
.to("direct:ROUTER2");
Using Spring DSL for Single route
<route streamCache="true">
<from uri="direct:ROUTER1"/>
<to uri="direct:ROUTER2"/>
</route>
For global and per route scope using JAVA DSL
context.setStreamCache(true);
from("direct:ROUTER1")
.to("direct:ROUTER2");
For global and per route scope using Spring DSL
<route streamCache="true">
<from uri="direct:ROUTER1"/>
<to uri="direct:ROUTER2"/>
</route>
Note link: Camel Stream Caching
why stream caching
I have two separate Camel routes defined in separate XML files. Call them route A and B.
I would like to direct route B to first call route A, before passing the result to route B.
How should I go about doing so? So far I am looking at the direct DSL.
Thank you.
The solution is to define the secondary route, and link both routes together using vm-direct.
When called, objects stored as variables in secondary route can be referenced by the earlier route, i.e. the routes are executed sequentially and pend on the secondary route's completion.
Like so:
Primary route:
<To uri="direct-vm:....>
<!-- variables if stored by secondary route available here -->
Secondary route:
<Route>
<From uri="direct-vm:.....>
</Route>
I've been trying to create endpoints in java and have those endpoints referenced in my xml routes but have been unsuccessful. I can do this in xml:
<endpoint id="kafkatopic" uri="kafka:..."/>
and have the endpoint referenced in the routes:
<route id="eventflow">
<from ref="kafkatopic" ...>
What i want to do is replace the xml endpoint declaration using java. I've tried something like:
Endpoint kafkaep = camelCtx.getEndpoint(kafkaUri);
however i'm stumped on how i can create a key "kafkatopic" to refer to the endpoint such that the xml route is able to find it. I've checked the EndpointRegistry but doesn't allow me to provide a simple name for the endpoint.
Any help is appreciated. Thanks.
Here's my camelContext:
<camelContext xmlns="http://camel.apache.org/schema/blueprint"
trace="true" id="context">
<routeBuilder ref="myRouteBuilder" />
<route id="eventflow">
<from ref="kafkatopic" ...>
My simplified RouteBuilder.configure() has below. Here i was trying to put the endpoint in the endpointRegistry hoping that it will used by the xml route. There's not a lot of docs on EndpointRegistry so i was shooting in the dark with this.
// endpoint i have formatted to be "someKey=uri"
String endPoint = getConfigs("camel-endpoint");
String [] eptoks = ep.split("=", 2);
EndpointRegistry<String> endpointRegistry = camelContext.getEndpointRegistry();
Endpoint endpoint = camelContext.getEndpoint(eptoks[1]);
endpointRegistry.put(eptoks[0], endpoint);
You could try defining the endpoint in Spring, either explicitly:
<bean id="kafkaComponent" class="org.apache.camel.component.kafka.KafkaComponent"/>
<bean id="kafkaEndpoint" class="org.apache.camel.component.kafka.KafkaEndpoint">
<constructor-arg value="kafka:..."/>
<constructor-arg ref="kafkaComponent"/>
</bean>
or using Spring factory bean and method:
http://www.javabeat.net/create-spring-beans-using-factory-methods/
The easiest way to achieve this would be to create a class which implements the
org.apache.camel.Processor
interface and configure this in the spring xml file, e.g.
<bean id="sp" class="com.mycompany.SimpleProcessor"/>
In your route you can then reference this simply using the id you gave in the bean, e.g.
<to uri="sp" />
I would say that makes it a bit confusing to read although perhaps you have a good reason but when I look at the route I want to clearly know the endpoint details unless they are referenced above somewhere. To have to look up the endpoint in a java class can be cumbersome later on.
I recently found out that you can write your entire RouteBuilder logic in java and simply let the blueprint xml call it just like any other bean? That why your routes are in java and the startup mechanism in xml.
I know it's late but here's what i did to make this work. Basically i programmatically created the endpoints simply as "direct" endpoints based on configuration and reference those direct endpoints in my camel routes xml file. That way i avoided having environment specific values like hostnames, port numbers, etc., in my xml routes file and just have one version for all environments.
I am using doing integration work with Camel and using the HTTP endpoint as a proxy to route certain messages to an HTTP endpoint. I have my route configured to use my custom error handler which places failed messages in a queue that I specified (Dead Letter Channel pattern).
<route>
...
<to uri="direct:MessageTypeGuaranteed"/>
</route>
<route errorHandlerRef="MyCustomErrorHandler">
<from uri="direct:MessageTypeGuaranteed">
<to uri="http://dummyUri?throwExceptionOnFailure=true"/>
</route>
When anything fails to get delivered to my http endpoint, it's being added to my custom queue ("CustomFailedMessageQueue"), and I have a separate route that attempts to retry these messages:
<route>
<from uri="jms:queue:CustomFailedMessageQueue">
<to uri="direct:MessageTypeGuaranteed"/>
</route>
What I'd like to do is to be able to specify that I only want a message to live say for 10 seconds. So I am trying to set time to live on my http destination itself.
For example, I have a processor that does something like this:
exchange.getIn().setHeader(Exchange.HTTP_URI, "http://localhost/nodeserver?timeToLive=10000");
However, I think I have misunderstood the documentation. The timeToLive option is only valid when passing it to the jms component, correct? In other words, if I want to make use of time to live with this end point, I will need to do that handling myself in a processor, correct?
Yes TimeToLive is an option from the JMS spec that the Camel JMS components support. That options has no meaning for other components such as HTTP.
It seems like you may want to use a filter EIP and discard the message if its to old, and you can use a java method etc to implement some code that figures out if its to old or not, and return a boolean
public boolean isNotToOld(Exchange exchange) {
...
return true // to accept and process the message
}
See more about the filter eip here
http://camel.apache.org/message-filter
And you can use it in a route something a like
<from uri="direct:MessageTypeGuaranteed">
<filter>
<method ref="myBean" method="isNotToOld"/>
<to uri="http://dummyUri?throwExceptionOnFailure=true"/>
</filter>
From a logical perspective this is the kind of routing behaviour I wish to implement:
I want to be able to merge the response of an external service with the original request.
I have been able to implement this using multicasting, an aggregator, and a mock endpoint but I was wondering if there is a cleaner way. My current implementation looks like this:
<multicast strategyRef="serviceAggregator" stopOnException="false">
<to uri="mock:foo" />
<to uri="http://0.0.0.0:9999/service/?throwExceptionOnFailure=false" />
</multicast>
<camel:to uri="log:uk.co.company.aggregated?showAll=true" />
<to uri="http://0.0.0.0:9999/anotherService/
The part I particularly don't like is using a mock endpoint but I also don't think that this is a very readable way to express the above diagram. So I was wondering if there was a more elegant way of doing this?
I suggest to read about the EIP patterns, for example the content enricher
http://camel.apache.org/content-enricher.html
Where you can merge the reply message with the request message.
Mind the Content Enricher has 2 modes
- enrich
- pollEnrich
Make sure to notice the difference, from the docs in the link above.
<route>
<from uri="...">
<enrich uri="http://0.0.0.0:9999/service/?throwExceptionOnFailure=false" strategyRef="serviceAggregator"/>
<to uri="log:uk.co.company.aggregated?showAll=true" />
<to uri="http://0.0.0.0:9999/anotherService/>
...
</route>
And yes you diagram is showing splitter, but the sample code is using multicast EIP.
You could simply store the original message in a header or property and later do some merge in a bean. Using the header and the current body.
.setHeader("orig", body())
.to("externalService")
.bean(new MyMergeBean())