1 2 Previous Next 15 Replies Latest reply on Jul 28, 2009 8:50 AM by njiang

    OSGi Pure Service and Camel Route.

    pchandler

      I have created a few OSGi Services that are running in servicemix. I now want to invoke/call them via Camel.

       

      So, is it just:

      <ca:to uri="bean:myService?method=myMethod"/>   ?

       

      My OSGi service is a non-Spring enable service. So can I still used the bean: ?

       

      Question: How to you call an non-Spring enable service via a Camel route? 

       

      Thanks in advance,

       

      Peter.

        • 1. Re: OSGi Pure Service and Camel Route.
          njiang

          Camel will lookup the jndi registry to find the instance of the bean object with the name. If you can put your OSGi service to this registry , then camel can find it without any trouble.

           

          Spring DM makes the Spring beans export as the OSGi service out of box. I don't know if you could import the OSGi service in the spring context, if so , you can define the bean endpoint name with the import OSGi service name , and you can use it in the camel context as the bean's endpoint name.

           

          If not,  we need to do some enhancement to let camel osgi module can look up the OSGi service from the bundle context.

          • 2. Re: OSGi Pure Service and Camel Route.
            pchandler

            I guess I am still a bit confused.

             

            My OSGi Service registers itself (in the start method of the Activator) in the OSGi registry:

                      // register the service

                      serviceReg = context.registerService(

                                MyService.class.getName(),

                                new MyServiceImpl("com.mycompany.package"),

                                null);

             

            So, Camel does not use the OSGi registry but a separate/different jndi registry?

             

            For Camel to use a OSGi Service you must start the service via a bean XML configuration files?

             

            I must create a my-service-bundle-context.xml and a bundle-context-osgi.xml

            in META-INF/spring ?

             

            To use Camel you can't have you own OSGi Activators?

             

            Camel only find Spring enable OSGi bundles?

             

            Sorry for all the question,

             

            Peter.

            • 3. Re: OSGi Pure Service and Camel Route.
              pchandler

              So, there is no way to bind (create an Endpoint) to an OSGi registered service?

               

              http://camel.apache.org/uris.html

               

              Would be nice to have an OSGi URi.

               

              Peter,

              • 4. Re: OSGi Pure Service and Camel Route.
                davsclaus

                Hi Peter

                 

                Feel free to add a ticket for this in JIRA either at fusesource or at apache.

                 

                There is a org.apache.camel.spi.Registry allowing to use a pluggable registry instead of the default that will be spring application context based.

                 

                Having Camel to be able to lookup in both would be nice.

                • 5. Re: OSGi Pure Service and Camel Route.
                  njiang

                  Hi ,

                   

                  I just create a JIRA[1] in apache camel.

                   

                  https://issues.apache.org/activemq/browse/CAMEL-1789

                  • 6. Re: OSGi Pure Service and Camel Route.
                    njiang

                    I just did a quick fix for the CAMEL-1789 today, please feel free to try it out.

                    • 7. Re: OSGi Pure Service and Camel Route.
                      njiang

                      Hi Peter,

                       

                      Spring DM support the reference of OSGi service, if you don't have chance to try my latest fix, you could try to add the below lines into your spring configuration file.

                       

                      <beans xmlns="http://www.springframework.org/schema/beans"

                            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

                            xmlns:osgi="http://www.springframework.org/schema/osgi"                           

                                                                         

                       

                        <osgi:reference id="someService" interface="com.xyz.AService"/>  

                        ...

                         

                      </beans>

                       

                      Then you can use the beans URI to reference this someService.

                       

                      Willem

                      • 8. Re: OSGi Pure Service and Camel Route.
                        pchandler

                        Thanks,

                         

                        Do you have an example (CamelContext.xml) of how one invokes an "pure"(non-spring) OSGi service from camel after the fix?

                         

                        Any chance of getting a osgi URI for camel?

                            <ca:to uri="osgi:MyRegisteredOSGiService?method=methodName"/>

                        • 9. Re: OSGi Pure Service and Camel Route.
                          pchandler

                          Usage example please?

                           

                          Will the below code return the OSGi BundleContext?

                           

                          CamelContextFactoryBean beanFactory = new CamelContextFactoryBean();

                          BundleContext context = beanFactory.getBundleContext();

                           

                          Thanks,

                           

                          Peter.

                          • 10. Re: OSGi Pure Service and Camel Route.
                            oisin

                            Hi Peter

                            After the fix, you should be able to use the bean: URI to get access to the OSGi service.  In the Spring world, Camel checks the Spring Application Context to get the bean by name. What the fix does is add the the OSGi Service Registry before the Spring Application Context for the resolution of bean: components.

                             

                            So if you have a service org.example.MyService, the syntax

                             

                            <to uri="bean:org.example.MyService?method=methodName"/>
                            

                             

                            should resolve and invoke the service you have registered.

                             

                            I have to admit, however, I haven't tried this out yet, but that is how the unit

                            test is designed to work.

                             

                            --oh

                            • 11. Re: OSGi Pure Service and Camel Route.
                              njiang

                              Hi ,

                               

                              You can use it as the bean endpoint like this

                              <ca:to uri="bean:TheOSGiServiceInterfaceName?method=methodName"/>

                              • 12. Re: OSGi Pure Service and Camel Route.
                                njiang

                                Since Spring DM will Inject the bundle context for your, if you use Spring to create that CamelContextFactoryBean, you will get the bundle context, Otherwise you can't get the bundle context.

                                • 13. Re: OSGi Pure Service and Camel Route.
                                  pchandler

                                  META-INF/spring/CamelContext.xml:

                                   

                                      ...

                                   

                                   

                                   

                                  Constructor for MyImpl:

                                       public MyImpl() {

                                              CamelContextFactoryBean beanFactory = new CamelContextFactoryBean();

                                              BundleContext context = beanFactory.getBundleContext();

                                              if (context == null)

                                              {

                                                  log.info("***Bundle Context is null" );

                                                  return;

                                              }

                                   

                                   

                                  Output:

                                  ***Bundle Context is null

                                   

                                   

                                  Can not get a reference to the bundle context from the spring bean? Bug? or not?

                                  • 14. Re: OSGi Pure Service and Camel Route.
                                    davsclaus

                                    Yes you cannot do as you do by creating a new CamelContextFactoryBean and then expect it is initialized with OSGi. You should not really construct this factory bean yourself.

                                     

                                    Its a spring factory bean and thus part of spring doing its initialization.

                                     

                                    I would assume there are more standard ways of getting a bundle context in OSGi land. Eg try look outside Camel and look how to get the bundle context in pure OSGi.

                                     

                                    You can get hold of the camel context though in your bean. Just get the camelContext tag an id, eg

                                     

                                     

                                     

                                    And then I assume you can use it to get the bundle context.

                                    1 2 Previous Next