CamelContext apache camel - apache-camel

is it necessary to create a camelcontext like this ? and the use the start() and stop() methods ?
CamelContext camelContext = new DefaultCamelContext();
Because even when I don't create a camel context and not use start() and stop(), my route works and the files are copied from one folder to another.
from("file:C:/Users/user1/Desktop/in").process(new MyProcessor()).to("file:C:/Users/user1/Desktop/out");
So why in some tutos, they say that it is necessary to create camelcontext ?

if you're using quarkus vs spring boot you don't need to explicitly start camel routes . If you do it only with java without using any framework, you should add these routes to the context and start them later.These frameworks start and stop for you in the background .

Related

Configure http4 certificate in application.yml

I'm trying to move my http4 certificate configuration away from RouteBuilder class and to application.yml file. My code is exactly like the Java example on this page under the "Setting up SSL for HTTP Client - Programmatic Configuration of the Component": (https://camel.apache.org/http4.html#HTTP4-UsingtheJSSEConfigurationUtility). However, on the website there is no yml example, only the Java solution that I currently have and Spring DSL solution. Does anybody know how to translate the Java code to yml?
#Configuration
public class configureHttps4Certificate extends RouteBuilder {
#Override
public void configure() throws Exception {
KeyStoreParameters ksp = new KeyStoreParameters();
ksp.setResource("pathToResource");
ksp.setPassword("password");
TrustManagersParameters tmp = new TrustManagersParameters();
tmp.setKeyStore(ksp);
SSLContextParameters scp = new SSLContextParameters();
scp.setTrustManagers(tmp);
HttpComponent httpComponent = getContext().getComponent("https4", HttpComponent.class);
httpComponent.setSslContextParameters(scp);
}
}
If you use SpringBoot you can easily create Java configuration classes that automatically import values from property or YAML files just by using one annotation.
#ConfigurationProperties("app.config")
public class MyConfiguration
Check out this section of the SpringBoot documentation that describes this mechanism.

Camel route should be annotated with component or configuration

I have camel route which basically used to move files from source to destination as below
public class SimpleRouteBuilder extends RouteBuilder {
#Override
public void configure() throws Exception {
from("file:C:/inputFolder?noop=true").to("file:C:/outputFolder");
}
}
Question is which annotation(#component or #Configuration) should be used to load this route
If you are using Spring or Spring Boot etc then it should be #Component which ensures the class is enlisted into the spring bean registry, which Camel then scans for RouteBuilder classes and automatic adds to the CamelContext.
Mind that Spring Boot has some classpaths it only scans (I think its the package of the main class and sub packages), so if you put it inside other packages outside that, you may need to configure spring boot to scan for other packages.

How to use same CamelContext in multiple jar on the same war

I'm using the camel 2.16.2 and I need to use the one CamelContext across multiple jars as I need to have all the Camel Routers in to one CamelContext. So my war will have all those jars as maven artifacts.
Please let me know how do I handle above scenario?
Edit
Just to elaborate more on above question.
In my war myApp.war, I have initialized the CamelContext. There are three jars myApp1.jar, myApp2.jar and myApp3.jar. Each jar has it own routers defined separately.
How do I start the routers in each jar ?
Can I use the same CamelContext injected to each routers?
If I cannot handle through jars, is it possible to implement with multiple war (myApp1.war, myApp2.war and myApp3.war) and each war having different camelContext and communicate to those routers from the main war (myApp.war) ?
As other guys said, you can't use the same CamelContext across different Jars. Could you explain a little what you want to do?
IMHO what you want to do is use routes defined in different Jars. So for that you can define a Camel Context and add all the routes from different Jars. Of course your Camel-Context-JAR has to have access to all those jars.
<camel:camelContext id="camel5">
<camel:package>org.apache.camel.spring.example</camel:package>
</camel:camelContext>
Or class by class
<camelContext id="camel5" xmlns="http://camel.apache.org/schema/spring">
<routeBuilder ref="myBuilder" />
</camelContext>
<bean id="myBuilder" class="org.apache.camel.spring.example.test1.MyRouteBuilder"/>
Or if you are using CDI you can follow this great article https://dzone.com/articles/using-camel-routes-in-java-ee-components
Reference: http://camel.apache.org/spring.html
After doing some research found a way to implement this. Infact we can use the same CamelContext across different jars as all jars are in the same war (Web Container).
We can implement easily with Apache Camel 2.16.2 with camel CDI. If you're using wildfly to deploy your war then you may need to add the camel patch. Download the the wildfly 9.0.2 pach
Steps are Given Below.
In your war create a servlet or restService and Inject the camelContext.
#Inject
#ContextName("cdi-context")
private CamelContext camelctx;
Create a router in the jar with below annotation.
#Startup
#ApplicationScoped
public class MyJRouteBuilder extends RouteBuilder {
In Configure method add
#Override
public void configure() throws Exception {
from("direct:startTwo").routeId("MyJRouteBuilder")
.bean(new SomeBeanThree());
}
Create a BootStrap Class in your jar and add the Router
#Singleton
#Startup
public class BootStrap {
private CamelContext camelctx;
#PostConstruct
public void init() throws Exception {
camelctx.addRoutes(new MyJRouteBuilder());
}
Add your jar as a artifact in the war pom.xml. Once you deploy the war you can see MyJRouteBuilder is Registred in the cdi-context CamelContext. So now you can access your Router anywhere you want.
Hope this would useful anyone who has the same issue what I had.

package/packagescan tag in Apache camel

Trying to load the routes from .xml file.Instead of using import resource,or "route context ref".
So i wrote a Routebuilder class with following code
InputStream is = getClass().getResourceAsStream("barRoute.xml");
RoutesDefinition routes = context.loadRoutesDefinition(is);
context.addRouteDefinitions(routes.getRoutes());
and loaded the routes at the time of camelcontext loading.
using
com.***.Loadroutes
I am able to load routes from xml file in standalone.
But when i deploy the bundle to fuse container,routes are not loading from xml file.
How to use package/packagescan in blurprint/spring to run inside fuse
note :made project as osgi specific bundle and normal bundle(mvn camel:run).

How can I configure the JAX-RS base path in TomEE+?

I have a WAR with some JAX-RS services, deployed into TomEE Plus. Given a service annotated with #Path("myservice"), TomEE+ publishes it to localhost:8080/mywebapp/myservice.
However, that also makes accessing a JSP at localhost:8080/mywebapp/index.jsp impossible - JAXRSInInterceptor complains that No root resource matching request path has been found, Relative Path: /index.jsp.
So I would like to configure a path prefix api to all services, which changes the myservice URL to localhost:8080/mywebapp/api/myservice. Doing so would be trivial if I had configured CXF on my own (with or without Spring), because I could simply change the URL pattern of the CXF Servlet - but I am relying on the default settings where I don't configure anything besides the annotations. So how do I do that in this case?
Note that I don't want to alter the #Path annotations to include the prefix, because that does not fix the issue with the JSP.
Create an extension of javax.ws.rs.core.Application and annotate it with #ApplicationPath where value would be api in your case:
#ApplicationPath("/api")
public class MyApplication extends Application {
#Override
public Set<Class<?>> getClasses() {
final Set<Class<?>> classes = new HashSet<Class<?>>();
// register root resource
classes.add(MyServiceResource.class);
return classes;
}
}
This way a Servlet 3 container would find your application and map your resource to /mywebapp/api/myservice while making your web resources (.jsp) available at /mywebapp.
TomEE trunk supports these configurations: cxf.jaxrs.staticSubresourceResolution & cxf.jaxrs.static-resources-list
but the #ApplicationPath is the more relevant solution IMO
Using -Dopenejb.webservice.old-deployment=true can help too in some cases

Resources