2

Hello I am trying to access EJB component using JNDI. For this i made a simple program called HelloWorld.

Remote Interface

public interface HelloWorldEJB extends EJBObject {
    public String sayHello() throws RemoteException;

}

Home Interface

public interface HelloWorldEJBHome extends EJBHome {
    HelloWorldEJB create() throws RemoteException, CreateException;
}

Implmentation

public class HelloWorldEJBBean implements SessionBean {
    private SessionContext _context;

    public void ejbCreate() {
    }

    public void setSessionContext(SessionContext context) throws EJBException {
        _context = context;
    }

    public void ejbRemove() throws EJBException {
    }

    public void ejbActivate() throws EJBException {
    }

    public void ejbPassivate() throws EJBException {
    }

    private Object getEntityByDTO(Object entityDTO) throws FinderException,
                                                           NamingException {
        return null;
    }

    public String sayHello(){
        return "Hello";
     }
}

ejb-jar.xml

<?xml version = '1.0' encoding = 'windows-1252'?>
<ejb-jar xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd"
         version="2.1" xmlns="http://java.sun.com/xml/ns/j2ee">
  <enterprise-beans>
    <session>
      <description>Session Bean ( Stateless )</description>
      <display-name>HelloWorldEJB</display-name>
      <ejb-name>HelloWorldEJB</ejb-name>
      <home>model.HelloWorldEJBHome</home>
      <remote>model.HelloWorldEJB</remote>
      <ejb-class>model.HelloWorldEJBBean</ejb-class>
      <session-type>Stateless</session-type>
      <transaction-type>Container</transaction-type>
    </session>
  </enterprise-beans>
  <assembly-descriptor>
    <container-transaction>
      <method>
        <ejb-name>HelloWorldEJB</ejb-name>
        <method-name>*</method-name>
      </method>
      <trans-attribute>Required</trans-attribute>
    </container-transaction>
  </assembly-descriptor>
</ejb-jar>

Client Lookup program

DataSource dataSource = null;
try {
    String ejburl = "t3://localhost:7101/";
    /**Setup the environment*/
    Hashtable environment = new Hashtable(6);
    /**Turn JNDI on to Weblogic and use oracle db password verification*/
    environment.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
    environment.put(Context.SECURITY_PRINCIPAL, "weblogic");
    environment.put(Context.SECURITY_CREDENTIALS, "weblogic1");
    environment.put(Context.PROVIDER_URL, ejburl);

    Context context = new InitialContext(environment);
   method -1 Object obj = context .lookup("HelloWorld10g");
   method -2  Object obj2 = context .lookup("HelloWorld10gModelEJB_jarHelloWorldEJB_EO");
    System.out.println("JNDI Done");
    method -3 context.lookup("HelloWorldEJB#model.HelloWorldEJB");
} catch (Exception ex) {
    ex.printStackTrace();
}

Now My question is when client program looks ejb bean using method 1 and method 2 then its returning bean but program is not able to lookup bean by method 3. Whats wrong with this method. can someone help me? Please see the below image as well which will tell you more about env.

enter image description here

enter image description here

enter image description here

enter image description here

keepmoving
  • 1,813
  • 8
  • 34
  • 74

1 Answers1

1

Your bean have only one Remote Interface, and in this case the fully- qualified name is not needed. This should work:

HelloWorldEJB helloWorldEJB = (HelloWorldEJB) context.lookup("HelloWorldEJB");

If you have multiple Remote Interfaces, you'll need to lookup a name that contains the part of the global JNDI name of the target EJB and the specific Remote Interface, separated by symbol #

  • Firstly I have n number of remote in my project, i am just testing how to access bean. Second i tried with your solution but giving error. "javax.naming.NameNotFoundException: Unable to resolve 'HelloWorldEJB'. Resolved '' [Root exception is javax.naming.NameNotFoundException: Unable to resolve 'HelloWorldEJB'. Resolved '']; remaining name 'HelloWorldEJB" – keepmoving Nov 04 '15 at 18:06
  • I have pasted everything, can you make this lookup string for me.What will be the global JNDI here. Something like this globalJNDI#RemoteInterface – keepmoving Nov 04 '15 at 20:08