How to configure and looking up OSGi service via JNDI

2.1k views Asked by At

I have created a OSGi bundle using bundle activator and deployed in karaf, but in karaf if you give jndi:bind-id service-name then it will not exist after karaf console shutdown because its not persistent.

I want to register service in OSGi using JNDi which is persistent, which is I can access using JNDI.

I am registering service as under,

 bundleContext.registerService(myFramework.class.getName(),FrmFactory.initFramework(ctx), null);
Context ctx = null;
        try
        {
            ctx = new InitialContext();
            ctx.bind("service/myframework", frmFactory.initFramework(ctx));
        }

Please suggest is this the right way,if not Please suggest, because I am getting exception as

errorMessage = jndiobjectBindError, errorMessage = Failed to bind local object in jndi. Object Name:service/portframework, cause = javax.naming.NotContextException: The intermediate context Myfr amework does not exist

1

There are 1 answers

6
Christian Schneider On

The simplest way to access services using jndi in karaf is to use the "jndi" feature. It provides access to all OSGi services using jndi.

For your case the jndi name of your service would be:

osgi:service/<FQN of myFramework>

So you just have to register your service and do not need to care about also registering in jndi.