Accessing EJB 3 Session Beans from a Java SE Client



I was asked a couple of times by various people how can one access session beans from code which is not managed by the Glassfish application server. I didn't know the answer so I went and investigated. I decided to write results of my research down because it seems to be quite a common question.

First of all, there is a FAQ entry available which covers this matter. My experience is a bit different from the FAQ though.

I create a new Enterprise application with an EJB module and a web module. To demonstrate accessing of the session bean from a Java SE client I at first create the session bean in the EJB module (demo/HelloBean.java).

package demo;
import javax.ejb.Stateless;
@Stateless
public class HelloBean implements HelloRemote {
    
    public HelloBean() {
    }

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


It has a remote interface (demo/HelloRemote.java):

package demo;
import javax.ejb.Remote;

@Remote
public interface HelloRemote {
    String sayHello(String text);
    
}


To test accessing the session bean I can use a servlet which I create in the web module (this is code managed by the application server so no issues here - I can use injection):

package demo;
import java.io.\*;
import java.net.\*;
import javax.ejb.EJB;
import javax.servlet.\*;
import javax.servlet.http.\*;
public class NewServlet extends HttpServlet {

    @EJB
    private HelloRemote helloBean;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.println("<h1>"+helloBean.sayHello("world")+"</h1>");
        out.close();
    }
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
}


By accessing the servlet using the web browser I get an obvious response "Hello world". Now comes the tricky part - how to access the session bean from a Java SE client? Obviously it is not possible to use injection outside of the application server context, but JNDI still works. So I create a new Java SE project and use this code for the main class:

package ejbtestclient;

import demo.HelloRemote;
import java.util.Properties;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class Main {
    
    public Main() {
    }
    
    public static void main(String[] args) {
        try {
            InitialContext ic = new InitialContext();
            HelloRemote hb = (HelloRemote) ic.lookup("demo.HelloRemote");
            System.out.println(hb.sayHello("world"));
        } catch (NamingException ex) {
            ex.printStackTrace();
        }
    }
}


This won't work yet - to make it work I need to add two libraries: javaee.jar and appserv-rt.jar. Both of these libraries are available in the lib subdirectory of Glassfish. Finally I need to declare dependence on the EJB module - obviously I need to access the HelloRemote interface.

When I run the main class I get a "Hello world" message. The thing which really surprised me was that I didn't have to use any application server-specific JNDI code. My guess is that it's because my application server runs on localhost and listens on the default port 3700.

Anyway I was surprised that calling a session bean from outside of the container managed code is quite easy. Honestly I was expecting some JNDI hell :)
Comments:

ACC (Application Client Container) allows you to use injection in Java SE code. Injection happens "on the server" and GlassFish makes it nice to send to client code "back" using Java Web Start. The alternate solution is to start the client application using a GF-provided script (appclient) which is harder to deploy. See here for more details: http://docs.sun.com/app/docs/doc/819-3659/6n5s6m5a9?a=view

Posted by Alexis MP on leden 05, 2007 at 11:17 dop. CET #

Thanks Alexis.

Posted by Roumen on leden 05, 2007 at 12:02 odp. CET #

Post a Comment:
Comments are closed for this entry.
About

Roman Strobl

Search

Archives
« duben 2014
PoÚtStČtSoNe
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    
       
Today