Apache Camel getUri of ToDefinition in version >= 2.16.0 - apache-camel

i have jsut upgraded my camel version and some functionality that was working before version 2.16.0 is now broken, I used to be able to get the URI for a toDefinition that replaced a placeholder, having just upgraded, this now doesnt replace the placeholder.
Code example is as follows:
<propertyPlaceholder id="properties" location="config.properties" />
<route>
<from uri="direct:input" />
<to uri="mq:queue:{{MY_PLACEHOLDER}}" />
</route>
config.properties
MY_PLACEHOLDER=FOO
Camel version 2.15.5
toDefinition.getUri() // equals mq:queue:FOO
Camel version 2.16.0
toDefinition.getUri() // equals mq:queue:{{MY_PLACEHOLDER}}
Any ideas?

Yes that is how its intended to be. The model is the model as it was designed (in this case with placeholder value).
The resolved uri is when Camel startup and runs the routes. So you can take that uri, and ask Camel to resolve, there is an API on CamelContext for that resolvePropertyPlaceholders
http://static.javadoc.io/org.apache.camel/camel-core/2.18.2/org/apache/camel/CamelContext.html#resolvePropertyPlaceholders-java.lang.String-

Related

Start apache camel route when client request made

I have apache camel application where huge number of routes are available and want to start it only when http request are made for them. For example, have two routes as mentioned in the documentation where autoStartup is set to false
<camelContext id="myCamel" xmlns="http://camel.apache.org/schema/spring"
autoStartup="false">
<route>
<from uri="direct:foo"/>
<to uri="mock:foo"/>
</route>
<route>
<from uri="direct:bar"/>
<to uri="mock:bar"/>
</route>
</camelContext>
I want to start the routes when the request are made for them. For example, when /rest/foo request made, I want to start the corresponding route and allow the corresponding route to serve the request.
The documentation says, we can start the route by using spring ApplicationContext.
ApplicationContext ac = ...
CamelContext camel = ac.getBean("myCamel", CamelContext.class);
// now start all the routes
camel.getRouteController().startAllRoutes();
should I write a filter program to intercept all http request and get hold spring context to start the corresponding route? or where do I have to put the above code to start the route exactly. can someone help please.

How to fix Missing ${ from the text: error - Apache Camel 2.12 to 2.16.4 uprade

We are upgrading from Apache Camel 2.12 to 2.16.4 and running into issues with one of the routes.
Caused by: java.lang.IllegalArgumentException: Missing ${ from the text: file:C:\OnDemandOutput?fileName=RPFPos_L2W.$simple{in.header.accountNum}-${date:now:yyyyMMddHHmmssSSS}.csv
<camelContext xmlns="http://camel.apache.org/schema/spring">
<propertyPlaceholder id="ignoreId" location="classpath:reformMB.properties"
prefixToken="${" suffixToken="}"/>
<route id="sendNotification">
<from uri="jms:queue:queue.sendNotification"/>
<to uri="file:${OnDemand.output.url}?fileName=RPFPos_L2W.$simple{in.header.accountNum}-${date:now:yyyyMMddHHmmssSSS}.csv"/>
</route>
</camelContext>
As per documentation this should have worked. Can someone help me in understanding what is wrong ?
According to the official documentation one should be able to refer to Camel's properties using $simple{...} like this:
Clashing Spring Property Placeholders with Camels Simple Language
Take notice when using Spring bridging placeholder then the spring ${} syntax clashes with the Simple in Camel, and therefore take care.
Example:
<setHeader headerName="Exchange.FILE_NAME">
<simple>{{file.rootdir}}/${in.header.CamelFileName}</simple>
</setHeader>
clashes with Spring property placeholders, and you should use $simple{} to indicate using the Simple language in Camel.
<setHeader headerName="Exchange.FILE_NAME">
<simple>{{file.rootdir}}/$simple{in.header.CamelFileName}</simple
</setHeader>

camel restlet component url matching

I have the following camel route
<camel:route>
<camel:from uri="restlet:/foo/{bar}/model" />
<camel:process ref="dummyProcessor" />
</camel:route>
My problem is why do I have match for these requests (basically I can put anything after model)
GET /foo/hoi/modelbroken
GET /foo/hoi/modelwhyisthisamatch
I expect that these should return 404.
What am I doing wrong?
There is a Matching Mode configuration option in Restlet.
http://restlet.com/learn/guide/2.3/core/routing/
While guide says it should be MODE_EQUALS by default, looks like it's Template.MODE_STARTS_WITH if you check the code:
https://github.com/restlet/restlet-framework-java/blob/master/modules/org.restlet/src/org/restlet/engine/component/InternalRouter.java
Not sure if it's a bug in guide or source, but you can configure it for yourself.
I've encountered something similar, and update to Java 11 and camel solved this issue.

how to create and export camelProxy with apache camel blueprint

I am using camel-blueprint with aries blueprint in osgi container. My configuration is as follows
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:c="http://camel.apache.org/schema/blueprint">
<c:camelContext>
<c:proxy id="myProxySender" serviceInterface="com.cmt.gabs.camel.test.MyInterface" serviceUrl="direct:a" />
<c:route>
<c:from uri="direct:a" />
<c:transform>
<c:simple>Hello ${body}</c:simple>
</c:transform>
<c:to uri="log:org.apache.camel.example?level=ERROR" />
</c:route>
</c:camelContext>
When i start the bundle it does not give any error, but the proxy is also not created as a service. Please tell me if i am doing any wrong.
I Want to use Camel Proxy for endpoint direct:a
edit
Camel version 2.11
OSGI container equinox 3.8
I had found the answer long ago but forgot to respond to it.
The answer is simple i just had to export it as a service.

Translation XML information to Java - Apache Camel

This is the example that is bundled with apache camel binaries
<route>
<!-- incoming requests from the servlet is routed -->
<from uri="servlet:///hello"/>
<choice>
<when>
<!-- is there a header with the key name? -->
<header>name</header>
<!-- yes so return back a message to the user -->
<transform>
<simple>Hello ${header.name} how are you?</simple>
</transform>
</when>
<otherwise>
<!-- if no name parameter then output a syntax to the user -->
<transform>
<constant>Add a name parameter to uri, eg ?name=foo</constant>
</transform>
</otherwise>
</choice>
</route>
How to translate this to Java
Am a beginner in camel, and some how came up to this
CamelContext context = new DefaultCamelContext();
context.addRoutes(new RouteBuilder(){
public void configure(){
from("servlet://hello").transform().....
}
});
But dont know how to proceed further...
If you want to port it over to java without any XML (spring that is) you can't (easily) use the servlet component.
Just porting the route will be like:
from("servlet:///hello")
.choice()
.when()
.header("name")
.transform(simple("Hello ${header.name} how are you?"))
.otherwise()
.transform(constant("Add a name parameter to uri, eg ?name=foo"));
It should work in the spring example (or any spring web app), just replacing the <route> in the <CamelContext> with <routeBuilder ref="demoRoute"> given you have defined your route as a spring bean (<bean id="demoRoute" class="org.example.demo.DemoRoute">).
However, I guess you want to do this in plain java (no spring, no xml, no webapp). You could go with the Jetty component. The difference being that Camel then will start the servlet container, instead of the servlet container starting Camel. No difference for this simple example though.
I suggest you start out with a Maven archetype to get the skeleton up
e.g. mvn archetype:generate then choose org.apache.camel.archetypes:camel-archetype-java (Creates a new Camel project using Java DSL.)
Well, you don't need the maven archetype if you have your own java application and have the thread keep running. Then you should do fine with your approach. The maven archetype is however very good for training purposes.
You then need to add a dependency to Jetty (camel-jetty.jar) (read more here).
The actual route would be exactly the same except the first row: from("jetty:http://localhost:8080/camel/hello")
Nice and easy.
Try this one:
from("servlet://hello")
.choice()
.when(header("name").isNotNull()).transform(simple("Hello ${header.name} how are you?"))
.otherwise().transform(constant("Add a name parameter to uri, eg ?name=foo"));

Resources