Access to an EJB remotely from a different server
Asked Answered
B

1

7

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());
    }
}
Basle answered 13/8, 2013 at 21:34 Comment(1)
I don't know Glassfish and can't answer the question, but a local bean is, by definition, local, and thus can't be accessed remotely.Moonshine
C
6

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

Chlorothiazide answered 14/8, 2013 at 5:31 Comment(3)
Great, I know is out of the main question, but If I want to deploy my project in Websphere Application Server, will the properties be the same?Basle
No the properties would be different, If i am right following are the ones. props.putContext.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory"); props.put(Context.PROVIDER_URL, "corbaloc:iiop:localhost:2809");Chlorothiazide
The phrase: The bean which you have created is a Local , No-Interface bean which can be accessed with same JVM is not correct. A Local/No-interface bean can be accessed only inside the same application (.war or .ear). It cannot be accessed by another application inside the same JVM. You would still need a Remote interface for that.Brolly

© 2022 - 2024 — McMap. All rights reserved.