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?
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);
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
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")
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With