sometimes I have to add an object to camel registry (of course with java). In most cases it is a dataSource .
My problem is I can\'t figure out a general working way
If you're using the camel-spring package, you can use Spring to manage the registry. Here's an example of code I used to add a singleton bean in Java through Spring:
MyApplicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:camel="http://camel.apache.org/schema/spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
<bean id="myRouteBuilder" class="com.example.MyRouteBuilder"/>
<camel:camelContext id="my-camel-context">
<camel:routeBuilder ref="myRouteBuilder"/>
</camel:camelContext>
</beans>
MyApplication.java
public class MyApplication {
public static void main(final String[] args) {
final FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("/path/to/MyApplicationContext.xml");
}
}
MyRouteBuilder.java
public void MyRouteBuilder extends RouteBuilder implements ApplicationContextAware {
@Override
public void configure() {
from("ftps://localhost:21/foo?pollStrategy=#myPollingStrategy")
.log("${body}");
}
@Override
public void setApplicationContext(final ApplicationContext applicationContext) {
final ConfigurableApplicationContext context = (ConfigurableApplicationContext)applicationContext;
final DefaultListableBeanFactory registry = (DefaultListableBeanFactory)context.getBeanFactory();
registry.registerSingleton("myPollingStrategy", new MyPollingStrategy());
}
}
The important part here is to implement the ApplicationContextAware interface in the class that you want to use to manually add beans to the registry. This interface can be implemented on any bean that Spring initializes; you don't have to use your RouteBuilder. In this case, the bean is created on startup during the setApplicationContext(...) execution before configure() is called, but you can also store the application context for use later (which is probably a more common usage).
This was tested with Camel 2.19.0 and Spring 4.3.10-RELEASE.
If you are using Spring with camel you can register your bean in Application context and it will be get registered in ApplicationContextRegistry.
eg. ConfigurableListableBeanFactory beanFactory = ((ConfigurableApplicationContext) applicationContext).getBeanFactory(); beanFactory.registerSingleton("beantolookup", bean);
And you can look up camelContext.getRegistry().lookupByName("beantolookup")
Here is one way of adding stuff to the registry in a RouteBuilder class. Below I am adding a TCPServerInitializerFactory which will be used later on. I always use the camel-blueprint archetype but create routes using java dsl. This works fine for me.
TCPServerInitializerFactory serverFactory = new TCPServerInitializerFactory(null);
final CamelContext camelContext = getContext();
final org.apache.camel.impl.SimpleRegistry registry = new org.apache.camel.impl.SimpleRegistry();
final org.apache.camel.impl.CompositeRegistry compositeRegistry = new org.apache.camel.impl.CompositeRegistry();
compositeRegistry.addRegistry(camelContext.getRegistry());
compositeRegistry.addRegistry(registry);
((org.apache.camel.impl.DefaultCamelContext) camelContext).setRegistry(compositeRegistry);
registry.put("spf", serverFactory);