Reputation: 13011
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.
I always begin to acquire the registry:
getContext().getRegistry();
But "Registry" does not have any method to add an object. So I have to try (with debugger) what kind of registry is in use
getContext().getRegistry(some.class)<some method to add something>;
For example in one project (camel blueprint) I have to call
SimpleRegistry registry = new SimpleRegistry();
registry.put("some", bean);
getContext().getRegistry(CompositeRegistry.class).addRegistry(registry);
Now I created a project with same structure (also same maven parent) but now the code from above stops working because for some reason now camel uses a PropertyPlaceholderDelegateRegistry
I am sure there will be code to add my bean but;
Is there code that works with every setup to add something to camels registry?
Upvotes: 9
Views: 13966
Reputation: 33
This is one way to solve:
Create a Camel Listener ( I have a standalone Java Camel App).
Add to beforeConfigure()
method a section similar to below.
CamelContext mainCamelContext = main.getCamelContext();
DefaultRegistry defaultRegistry = mainCamelContext.getRegistry(DefaultRegistry.class);
defaultRegistry.bind("YouBeanName", new YourBeanClassName());
Once this is done - you can reference it in your XML DSL.
Tested using Camel 3.14.x, JDK 8.
Upvotes: 1
Reputation: 446
For Camel 3.x use the code snippet for adding in registry at runtime
DefaultRegistry registry = (DefaultRegistry) camelContext.getRegistry();
registry.bind("key", <Object to bind>);
((org.apache.camel.impl.DefaultCamelContext) camelContext).setRegistry(registry);
This can be referred further, enter link description here
Upvotes: 2
Reputation: 11
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")
Upvotes: 1
Reputation: 867
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
.
Upvotes: 0
Reputation: 3191
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);
Upvotes: 9