Search code examples
javaservicejndiprovider

Tutorial about Writing a JNDI Service Provider


Is there any tutorial about Writing a JNDI Service Provider and accessing the objects and methods from another project to it step by step. There are some information on Building a service provider but it is not easy to implement.

Thanks.


Solution

  • Now I solved my problem with annotations and global usage of InitialContext class. Stateless annotation and ejb-jar xml files and accessing the global jndi addresses the keys for usage.

    Access Code:

    this.jndiAddress="ejb/service/IMyInterface";
    Context context = new InitialContext();
    IMyInterface object = (IMyInterface) context.lookup(this.jndiAddress);
    object.doSomething();
    

    Service Code;

    IMyInterface.java:

    public interface IMyInterface {
    
        void doSomething();
    
    }
    

    MyClass.java:

    @Remote(IMyInterface.class)
    @Stateless(mappedName="ejb/service/IMyInterface")
    public class MyClass implements IMyInterface {
    
        @EJB
        private IMyUserDao userDao;
    
        @Override
        public void doSomething() {
            User user = userDao.get(1);
            System.out.println(user.getName());
        }
    
    }
    

    ejb-jar.xml;

    <?xml version="1.0" encoding="UTF-8"?>
    <ejb-jar xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:ejb="http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd" version="3.0">
        <display-name>Service</display-name>
        <enterprise-beans>
            <session>
                <ejb-name>MyClass</ejb-name>
                <ejb-class>package.MyClass</ejb-class>
            </session>
            <session>
                <ejb-name>UserDao</ejb-name>
                <ejb-class>package.dao.UserDao</ejb-class>
            </session>
        </enterprise-beans>
    </ejb-jar>