How to refer external jar route from Camel context file - apache-camel

I have project A which has below simple route logrouteTest.xml
<routeContext id="CommonLogRoutes" xmlns="http://camel.apache.org/schema/spring">
<route id="logRoute">
<from id="_NSDL_PAN" uri="seda:MyLogRoute"/>
<convertBodyTo id="_convertBodyTo1" type="java.lang.String"/>
<log message="my received body : ${body}"/>
</route>
</routeContext>
I composed this project into JAR.
In my project B I have imported the above jar and trying to refer the Project A route like this
<import resource="classpath:com/tcl/Testjar/logrouteTest.xml"/>
Error
I am getting the below error
java.io.FileNotFoundException: class path resource [com/tcl/Testjar/logrouteTest.xml] cannot be opened because it does not exist
Is there any way to refer the external project camel route from current project route without deploying project A.
Note : I am looking for option to use Project A as ESB library

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>

Apache Camel getUri of ToDefinition in version >= 2.16.0

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-

InvocationTargetException while hitting rest service in camel route

I am using camel vs2.9.0 in my project and trying to hit a Rest service.
My route is as mentioned below:
<route>
<from uri="direct:restRoute"/>
<bean ref="finalProcess"/>
<setHeader headerName="CamelHttpMethod"><constant>POST</constant></setHeader>
<to uri="http rest service url here"/>
finalProcess is just a bean in which I am doing like this :
System.out.println(exchange.getIn().toString());
I am able to put a json request on the route which gets printed on the above sysout code
, but I am getting following error when it reaches to uri="" step.
WARNING : WebApplicationException has been caught : java.lang.reflect.InvocationTargetException

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