EJB 3.0 client in J2SE project

Today, I would like to show how you can create client for EJB 3.0 in J2SE project. First, we will need to create a bean with remote interface. Since, we are using EJB 3.0 this is very simple. Create business interface with Remote annotation and then create bean's implementation class of your bean and specify JNDI name of this bean:
  @Stateless(name="ejb/ProcessHello")
  public class ProcessHelloBean implements org.netbeans.ProcessHello {

  public void String getHello(){
   ....
 
Now, we have two ways how to create client. First one is using Application Client Container (ACC) and second one is without that. What is advantage of the ACC? ACC can be seen as lightweight container that is responsible for security, naming, communication with application server and especially for injection. However, the worse of this approach is that you need run your application with appclient launcher. The client that uses ACC can be written like:
    @EJB(name="ejb/ProcessHello")
    private org.netbeans.ProcessHello hello;

    hello.getHello();
  
Then you need to run your client's jar with launcher appclient -client . The client launcher is located in bin directory of your application server. It's cool, but some smart user can ask what's about remote clients that aren't run on same machine as Glassfish? It's trivial task, you need to run package-appclient script that packs the application client container libraries and jar files into an appclient.jar file. This jar you can copied on remote server, unpack, change some properties and run your client. More info about this command is avalaible here. Now, let's develop EJB client without ACC. This client is similar as for EJB 2.1. It means first lookup bean interface and then invoke your business methods. You don't need to call create, ... and other lifecycle methods for the bean.
    InitialContext ctx = new InitialContext();
    Object obj = ctx.lookup("org.netbeans.ProcessHello");
    ProcessHello hello = (TestTableRemote)PortableRemoteObject.narrow(obj, ProcessHello.class);
    hello.getHello();
 
In this sample I used default JNDI name that is fully qualified classname of the remote business interface (3.0) or remote home interface (2.x). If you want to change the JNDI name use mappedName attribute for @Stateless.
Comments:

In the past, using the ACC was pretty painful. But those days are fading...

One very important feature of GlassFish is the ability to distribute your Java EE app client via Java Web Start.

This allows developers to create an application client (using Java EE 5 features like injection) and allows an enterprise to do almost painless application provisioning.

This document has some more information that might be useful.

Posted by guest on December 19, 2005 at 11:50 AM CET #

This Java Web Start support is real cool! After reading the document i think i know a lot, but to read more about in the future would nice. Step for step example with pics and information about the security.

Is such support also in other Application Server?

Posted by Christopher Atlan on December 19, 2005 at 01:17 PM CET #

Thanks for you comments, I will write about Java Web Start clients soon.

Posted by guest on December 20, 2005 at 12:54 AM CET #

I have no comment

Posted by guest on December 22, 2005 at 02:25 AM CET #

You forgot to make the field "hello" as static. Injected field must be a static field of the Main class. So it should have been:
@EJB(name="ejb/ProcessHello")
    private static org.netbeans.ProcessHello hello;

Posted by Sahoo on December 22, 2005 at 07:27 AM CET #

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

pblaha

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
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
Bookmarks