Weblog

All | Personal | Sun
« Previous day (Dec 18, 2005) | Main | Next day (Dec 19, 2005) »
20051219 Monday December 19, 2005

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. Posted by pblaha ( Dec 19 2005, 06:11:29 PM CET ) Permalink Comments [5]

Calendar

RSS Feeds

Search

Links

Navigation

Referers

Older blog entries