Using controlBus from spring DSL with parameters - java

I need to control my routes and I am using spring DSL for Camel.
I need to exposed a service which will perform thoses actions to the routeId given in paramaters.
The following code does not work (the body contain the routeId)
<route id="stopRoute">
<from uri="direct:stopRoute"/>
<log message="about to stop a route"/>
<to uri="controlbus:route?routeId=${body}&action=stop"/>
<to uri="controlbus:route?routeId=${body}&action=status"/>
</route>
I also tried with simple language but I can't figure out the correct syntax

See this FAQ
http://camel.apache.org/how-to-use-a-dynamic-uri-in-to.html
Use <toD> to make the to dynamic.

Related

Camel CXF REST: Setup then poll

The problem is simple but the implementation seems elusive. I want to send only once a few setup POSTs to a REST server and then begin polling every 5 seconds with GETs right afterward the POSTs was successful. What would the implementation for this look like in Camel Spring XML using the Camel CXFRS component? I don't want to write new code or a camel endpoint and would like to do this with the existing camel tools.
You could try something like below. For details on camel components refer to Apache camel documentation
<camelContext xmlns="http://camel.apache.org/schema/spring"
<route id="abc" shutdownRoute="Default" streamCache="true">
<from uri="timer://foo?fixedRate=true&period=100000" />
<setHeader headerName="CamelHttpMethod">
<constant>POST</constant>
</setHeader>
--setheader for Content-Type
<recipientList>
<simple>https4://post url</simple>
</recipientList>
<log message="After Transmission " loggingLevel="DEBUG"
logName="com.domain" />
<recipientList>
<simple>https4://get url</simple>
</recipientList>
--unmarshall
</route>
</camelContext>

Apache Camel WebService consumer

I have created a route
cxf:cxfEndpoint id="testEndpoint" address="http://localhost:9003/ws"
serviceClass="pl.test.ws.testImpl"
wsdlURL="/META-INF/wsdl/test.wsdl"
endpointName="s:testSoap"
serviceName="s:testService"
xmlns:s = "https://test.pl/wsdl"/>
<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
<route>
<from uri="direct:sendToTest" />
<to uri="cxf:bean:testEndpoint" />
</route>
</camelContext>
How can i call this webservice by putting object in the direct:sendToTest route?
I would like to be able to make a soap request some criteria will be met however I do not knew how can I put from java class message on the route.
can anyone give me a hint?
You can use a ProducerTemplate to send message to any Camel endpoint from Java code.
A little example from the getting started guide
http://camel.apache.org/walk-through-an-example.html
And to get more familiar with Apache Camel I recommend people to read this article
http://java.dzone.com/articles/open-source-integration-apache

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>

Camel...Evaluating a java expression

I have a camel flow which routes from an activemq to another activemq. However, I need to evaluate an expression and set it as a header. How do I achieve that.
<from uri="jms:queue:Q.activemq1"/>
<setHeader headerName="EVENT_KEY">
<simple>${java.util.UUID.randomUUID().toString()}</simple>
</setHeader>
<to uri="jms:queue:Q.activemq2"/>
But the header is not being set correctly?
How do I set java.util.UUID.randomUUID().toString() value to the header?
pls advise
Use the Groovy expression language for that. The simple language is ok for concatenating strings and comparing parts of the payload, but for more logic, groovy is a swiss army knife.
<from uri="jms:queue:Q.activemq1"/>
<setHeader headerName="EVENT_KEY">
<groovy>java.util.UUID.randomUUID().toString()</groovy>
</setHeader>
<to uri="jms:queue:Q.activemq2"/>
You need to add a dependency to camel-groovy to make it work.

Dynamic to(URI) in Camel

I'd like configure a Camel route where the to(uri) can be specified at runtime.
I tried the following:
public class Foo extends RouteBuilder {
#Override
public void configure() {
// the URI can point to different hosts
from("direct:start").to(${someUri}");
}
}
and then
ProducerTemplate pt = camelContext.createProducerTemplate();
pt.requestBodyAndHeader("direct:start", "someUri", "http://example.com");
However the above doesn't work (Camel complains about not having a default Endpoint).
What's the best way to go about this?
Although this question has already been answered, I wanted to share this other option to achieve what you're looking for, in case someone else still wonders how to do it:
There is a new method since Camel 2.16 called "toD" wich basically means a "dynamic to". Here is the official reference documentation.
from("direct:start")
.toD("${someUri}");
In this case, the toD method resolves the argument using the Simple language wich means you can use any property that the language supports.
You can also take a look at this other StackOverflow answer, about that same topic.
see these links for reference:
http://camel.apache.org/how-do-i-use-dynamic-uri-in-to.html
http://camel.apache.org/recipient-list.html
for an example, see this unit test
https://svn.apache.org/repos/asf/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/RecipientListTest.java
I simply used curly braces without the '$' before it. Here is what I did:
{headers.reQueueName} instead of ${headers.reQueueName} for the uri and it worked :
<to id="requeue" uri="jmsamq:queue:{headers.reQueueName}"/> here is my implementation :
<route id="_throttleRoute">
<from id="throttleRouteStarter" uri="direct:throttleRouteService"/>
<log id="_Step_5" message="Camel throttle Route Started"/>
<log id="_Step_5_1" message="Camel throttle Route is ${headers.next}"/>
<to id="restThrottleCall" uri="restlet:http://host:port/path"/>
<process id="throttleRouteProcess" ref="throttleServiceProcessor"/>
<choice id="_choice2">`enter code here`
<when id="_when3">
<simple>${headers.next} == 'requeue'</simple>
<to id="requeue" uri="jmsamq:queue:{headers.reQueueName}"/>
<log id="_Step_wait1" message="ReQueue sending to ${headers.reQueueName}"/>
</when>
<when id="_when4">
<simple>${headers.next} == 'process'</simple>
<log id="_logNext" message="Invoking Next Work Unit ${headers.next}"/>
<process id="jBPMRouteProcess" ref="jBPMRouteProcessor"/>
</when>
<otherwise id="_otherwise2">
<log id="_log5" loggingLevel="WARN" message="Next for orderId: ${headers.orderid} not found"/>
</otherwise>
</choice>
</route>

Categories

Resources