Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Camel how to add something to registry - with java, in general

Tags:

apache-camel

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?

like image 260
dermoritz Avatar asked Nov 09 '16 09:11

dermoritz


4 Answers

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);
like image 172
Souciance Eqdam Rashti Avatar answered Nov 19 '22 08:11

Souciance Eqdam Rashti


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

like image 23
Sharad Avatar answered Nov 19 '22 09:11

Sharad


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")

like image 1
abhi Avatar answered Nov 19 '22 07:11

abhi


This is one way to solve:

  1. Create a Camel Listener ( I have a standalone Java Camel App).

  2. 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.

like image 1
Chirag S Avatar answered Nov 19 '22 07:11

Chirag S