Menachem
Menachem

Reputation: 941

Changing an EJB from 2.1 to 3.0

I have an existing Web Application (Servlet) that remotely accesses an EJB currently written to the EJB 2.1 Specification (EJBHome, EJBObject, SessionBean, all configuration in ejb-jar.xml). The servlet access the EJB via JNDI lookup, the JNDI name being specified in weblogic-ejb-jar.xml.

I would like to update the EJB to EJB3.0, which will (hopefully) make it easier to add to the API.

My issue is that I need to do it with a minimum of change to the existing servlet code. In other words, I still need to:

  1. Access the EJB with a simple, global JNDI name (it is stored in a database, and the servlet can can change the name it looks up on the fly), and
  2. Be able to use the 2.1 style:

    String jndiName = getJndiName();    // e.g. "BeanV2.0"
    BeanHome home = (BeanHome) PortableRemoteObject.narrow(
            jndiContext.lookup(jndiName), BeanHome.class);
    BeanRemote remote = home.create();
    remote.doBusiness();               // call business method
    

I have tried a stripped down version, applying @RemoteHome, but I keep getting errors during deployment.

I am deploying (for development/production) on Weblogic, mostly 10.3.5 (11gR1), and am limited to EJB 3.0. I am using Eclipse (with the Oracle Weblogic Pack) for development.

Thanks.

Upvotes: 0

Views: 1231

Answers (1)

Adheep
Adheep

Reputation: 1585

I would suggest you to first understand the architectural difference between EJB2.1 and EJB3.x (refer here)

You will have some major/minor changes in code based on your bean implementation and client side invocation, because of the removal of Home and Deployment Descriptor in EJB3.x

Refer this example for accessing EJB3.x from a client, in your case Servlet

/**
 * Servlet implementation class SampleServlet
 */
public class SampleServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

public SampleServlet() {
    super();
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
            Context ctx=getInitialContext();
            String jndiName=getJndiName();  // eg. java:global/BeanProject/Bean!demo.BeanRemote
            BeanRemote beanRemote = (BeanRemote)ctx.lookup(jndiName);
            remote.doBusiness();

    } catch(Exception exception) {
        System.out.println(exception.getMessage());
    }
}

private static Context getInitialContext() throws Exception
{
    Properties properties=new Properties();
    properties.put("java.naming.factory.initial","org.jboss.naming.remote.client.InitialContextFactory");
    properties.put("java.naming.provider.url","remote://localhost:4447");

    return new InitialContext(properties);
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}

}

Hope this helps!

Upvotes: 3

Related Questions