You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Gert Villemos <gv...@gmail.com> on 2013/03/18 17:16:13 UTC

Programatically adding beans to a registry in a RouteBuilder

How can I in the 'configure' method of a RouteBuilder add beans to the
registry?

Im in a RouteBuilder creating a route using Netty. The Netty URL reference
encoder/decoder beans. I need to create and insert these beans in the
registry.

The Netty description (last code example) indicates that you can simply do;

Registry registry = camelContext.getRegistry();
...
registry.bind("spf", serverPipelineFactory);

However the 'bind' method is not a method of the Registry interface (I think
its a method only of the JNDI registry). Im using Camel 2.10.2.

What am I doing wrong?



--
View this message in context: http://camel.465427.n5.nabble.com/Programatically-adding-beans-to-a-registry-in-a-RouteBuilder-tp5729358.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Programatically adding beans to a registry in a RouteBuilder

Posted by Chris Wolf <cw...@gmail.com>.
Are you running this code in web container?  If so, which?  Tomcat,
Jetty, JBoss?

If you are just running as a standalone Java app, you can just use
"SimpleRegistry", which is basically a Map.

        CamelContext context = new DefaultCamelContext(new SimpleRegistry());

        Registry registry = context.getRegistry();
        if (registry instanceof PropertyPlaceholderDelegateRegistry)
            registry =
((PropertyPlaceholderDelegateRegistry)registry).getRegistry();


Then, instead of JndiRegistry.bind(...), just use Map's put(..) method:

((SimpleRegistry)registry).put("bean-name", new SomeBean());



On Mon, Mar 18, 2013 at 2:49 PM, Gert Villemos <gv...@gmail.com> wrote:
> I meant that I in the RouteBuilder::configure have;
>
> JndiRegistry registry = (JndiRegistry) getContext().getRegistry(); // DO NOT
> WORK!
>
>
> -----------
>
> Thanks for the suggestion. The code
>
> PropertyPlaceholderDelegateRegistry registry =
> (PropertyPlaceholderDelegateRegistry) getContext().getRegistry();
> JndiRegistry jndiRegistry = (JndiRegistry) registry.getRegistry();
>
> Gives me a reference to the JndiRegistry, but on first bind() I get an
> exception;
>
> javax.naming.NoInitialContextException: Need to specify class name in
> environment or system property, or as an applet parameter, or in an
> application resource file:  java.naming.factory.initial
>
> If I need to manually initialize the JNDI context, then this smells like Im
> doing something I should not be doing, i.e. bypassing the 'right' way of
> doing things...
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Programatically-adding-beans-to-a-registry-in-a-RouteBuilder-tp5729358p5729370.html
> Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Programatically adding beans to a registry in a RouteBuilder

Posted by Gert Villemos <gv...@gmail.com>.
I meant that I in the RouteBuilder::configure have;

JndiRegistry registry = (JndiRegistry) getContext().getRegistry(); // DO NOT
WORK!


-----------

Thanks for the suggestion. The code

PropertyPlaceholderDelegateRegistry registry =
(PropertyPlaceholderDelegateRegistry) getContext().getRegistry();
JndiRegistry jndiRegistry = (JndiRegistry) registry.getRegistry();

Gives me a reference to the JndiRegistry, but on first bind() I get an
exception;

javax.naming.NoInitialContextException: Need to specify class name in
environment or system property, or as an applet parameter, or in an
application resource file:  java.naming.factory.initial

If I need to manually initialize the JNDI context, then this smells like Im
doing something I should not be doing, i.e. bypassing the 'right' way of
doing things...



--
View this message in context: http://camel.465427.n5.nabble.com/Programatically-adding-beans-to-a-registry-in-a-RouteBuilder-tp5729358p5729370.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Programatically adding beans to a registry in a RouteBuilder

Posted by Chris Wolf <cw...@gmail.com>.
It may, or may not be wrapped in PropertyPlaceholderDelegateRegistry,
in that case, I would try this:

        Registry registry = context.getRegistry();
        if (registry instanceof PropertyPlaceholderDelegateRegistry)
            registry =
((PropertyPlaceholderDelegateRegistry)registry).getRegistry();


Then cast to implementation-specific Registry type to access bind()
(or put) method.

(Pretty much what Donald was saying...)

On Mon, Mar 18, 2013 at 2:14 PM, Donald Whytock <dw...@gmail.com> wrote:
> On Mon, Mar 18, 2013 at 2:02 PM, Gert Villemos <gv...@gmail.com> wrote:
>> Hmmm... tried that but doesnt work.
>>
>> getContext() returns a
>> org.apache.camel.impl.PropertyPlaceholderDelegateRegistry object. Which cant
>> be cast to JndiRegistry.
>>
>> My setup is the following; In a Spring based route I have bean A. Bean As
>> @Handler will create an instance of bean B, which is a RouteBuilder, and add
>> bean B's routes to the context. Bean A thus 'starts' bean B.
>
> Apologies...I was looking at the DefaultCamelContext source and seeing
> that by default it creates a JndiRegistry.
>
> I'm assuming you meant getRegistry() above instead of getContext()?
> Such that camelContext.getRegistry() returns the
> PropertyPlaceholderDelegateRegistry?
>
> According to the current javadoc, PropertyPlaceholderDelegateRegistry
> is "A Registry which delegates to the real registry."  The object has
> its own getRegistry() method, which returns a Registry.
>
> So...I'm sorry to say this, but...have you tried
>
> JndiRegistry registry = (JndiRegistry) camelContext.getRegistry().getRegistry();
>
> ?

Re: Programatically adding beans to a registry in a RouteBuilder

Posted by Donald Whytock <dw...@gmail.com>.
On Mon, Mar 18, 2013 at 2:02 PM, Gert Villemos <gv...@gmail.com> wrote:
> Hmmm... tried that but doesnt work.
>
> getContext() returns a
> org.apache.camel.impl.PropertyPlaceholderDelegateRegistry object. Which cant
> be cast to JndiRegistry.
>
> My setup is the following; In a Spring based route I have bean A. Bean As
> @Handler will create an instance of bean B, which is a RouteBuilder, and add
> bean B's routes to the context. Bean A thus 'starts' bean B.

Apologies...I was looking at the DefaultCamelContext source and seeing
that by default it creates a JndiRegistry.

I'm assuming you meant getRegistry() above instead of getContext()?
Such that camelContext.getRegistry() returns the
PropertyPlaceholderDelegateRegistry?

According to the current javadoc, PropertyPlaceholderDelegateRegistry
is "A Registry which delegates to the real registry."  The object has
its own getRegistry() method, which returns a Registry.

So...I'm sorry to say this, but...have you tried

JndiRegistry registry = (JndiRegistry) camelContext.getRegistry().getRegistry();

?

Re: Programatically adding beans to a registry in a RouteBuilder

Posted by Gert Villemos <gv...@gmail.com>.
Hmmm... tried that but doesnt work.

getContext() returns a
org.apache.camel.impl.PropertyPlaceholderDelegateRegistry object. Which cant
be cast to JndiRegistry.

My setup is the following; In a Spring based route I have bean A. Bean As
@Handler will create an instance of bean B, which is a RouteBuilder, and add
bean B's routes to the context. Bean A thus 'starts' bean B.



--
View this message in context: http://camel.465427.n5.nabble.com/Programatically-adding-beans-to-a-registry-in-a-RouteBuilder-tp5729358p5729365.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Programatically adding beans to a registry in a RouteBuilder

Posted by Donald Whytock <dw...@gmail.com>.
On Mon, Mar 18, 2013 at 12:16 PM, Gert Villemos <gv...@gmail.com> wrote:
> How can I in the 'configure' method of a RouteBuilder add beans to the
> registry?
>
> Im in a RouteBuilder creating a route using Netty. The Netty URL reference
> encoder/decoder beans. I need to create and insert these beans in the
> registry.
>
> The Netty description (last code example) indicates that you can simply do;
>
> Registry registry = camelContext.getRegistry();
> ...
> registry.bind("spf", serverPipelineFactory);
>
> However the 'bind' method is not a method of the Registry interface (I think
> its a method only of the JNDI registry). Im using Camel 2.10.2.
>
> What am I doing wrong?

DefaultCamelContext creates and returns a JNDI registry as default.
So assuming you're using a DefaultCamelContext, you'll need to

JndiRegistry registry = (JndiRegistry) camelContext.getRegistry();

Don