简体   繁体   中英

How to access EJB using JNDI Weblogic

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.

在此输入图像描述

在此输入图像描述

在此输入图像描述

在此输入图像描述

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 #

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM