Search code examples
jakarta-eeservletsejbglassfish-3ejb-3.1

Access to an EJB remotely from a different server


I'm developing in Eclipse Juno a project that includes EJB 3.1 and a Dynamic Web Project.

The EJB Class is called FirstBean and the Servlet is called EJB31ServletClient, I have achieved only to make it work in the same server. I have read that I need to set up InitialContext.lookup but almost all examples are made for JavaSE apps.

What I want to achieve:

  1. Deploy the EJB Jar in a Glassfish 3.1.2.2 server and deploy the dynamic web project in a different GlassFish server.

  2. Call in the EJB31ServletClient the sayHello() method.

Here is my code:

The EJB is a very simple Stateless Session Bean with No-interface View that only shows a message:

import javax.ejb.LocalBean;
import javax.ejb.Stateless;

@Stateless
@LocalBean
public class FirstBean {
    public FirstBean() {
    }

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

The Dynamic Web Project has only a Servlet with the following code:

import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.theopentutorials.businesslogic.FirstBean;

@WebServlet("/EJB31ServletClient")
public class EJB31ServletClient extends HttpServlet {

    private static final long serialVersionUID = 1L;
    @EJB
    FirstBean bean;

    public EJB31ServletClient() {
        super();
    }

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        out.println(bean.sayHello());
    }
}

Solution

  • You need to create your bean with remote interface. The bean which you have created is a Local , No-Interface bean which can be accessed with same JVM. To make remote invocations to your bean you need to create a bean with remote interface. See below the modified bean code and interface.

    public class FirstBeanRemote{
        public String sayHello() ;
    }
    
    import javax.ejb.LocalBean;
    import javax.ejb.Stateless;
    
    @Stateless
    @LocalBean
    @Remote(FirstBeanRemote.class)
    public class FirstBean {
        public FirstBean() {
        }
    
        public String sayHello() {
            return "Hello";
        }
    }
    

    And you need to lookup your EJB from other server using JNDI lookup.

    Properties props = new Properties();
    props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.enterprise.naming.SerialInitContextFactory");
    props.setProperty("org.omg.CORBA.ORBInitialHost", "*hostname*");
    props.setProperty("org.omg.CORBA.ORBInitialPort", "*3700*");//default port
    InitialContext ctx = new InitialContext(props);
    FirstBeanRemote bean = (FirstBeanRemote) ctx.lookup("java:global/*EARNAME/EJBJARNAME*/FirstBean!*fullyqualifiedpackage*.FirstBeanRemote");
    

    Try this and check if it works. If you couldnt find the JNDI name for the remote bean just crawl your glassfish admin console so that you can find it