Application Client/EJB 3.0 "Hello, <User>!"

In this entry, I am going to extend the EJB that I created for my entry about creating a web application/EJB 3.0 version of "Hello, world!" to support a remote interface and create an application client that uses that interface.

An application client is an alternative to using a web based front-end. They are similar to plain old J2SE applications, except they typically run in a special environment, an Application Client Container or ACC.

Getting your client and the container to a remote machine hasn't been a trivial task. Project GlassFish includes a very interesting app client deployment feature: Java Web Start integration, that helps eliminate the pain. I will use it in this entry.

Since the app client is going to run in an external JVM, we need to add a remote interface to the EJB in the Greeter project. The code for that is pretty trival (and looks very similar to the code for the Local interface that you created in the previous entry.

    package beans;
    
    import javax.ejb.Remote;
    
    @Remote public interface DistantGreeting {
        String returnPoliteGreeting(String name);    
    }
    

The implementation of the bean needs to be modified, to implement this new additional interface.

    package impl;
    
    import beans.DistantGreeting;
    import beans.FriendlyGreeting;
    import javax.ejb.Local;
    import javax.ejb.Stateless;
    
    @Stateless public class GreetingImpl implements FriendlyGreeting, DistantGreeting {
        
        public String returnFriendlyGreeting(String name) {
            return "Well, good to see you again, "+name+"!";
        }
    
        public String returnPoliteGreeting(String name) {
            return "It is a pleasure to make your aquantance, "+name+".";
        }    
    }
    

At this point we can start to create the project that will be our Java EE 5 app client. It is going to be a standard Java Application Project.

I will call the new project, Client.

You will need to add javaee.jar and the Greeter project as libraries for compilation. You use the Libraries "page" of the Client's Properties dialog to do this.

The code for the client is very straight-forward. You will notice that it looks very similar to the code in the web application module. This is an "added feature" of using a ACC based client, over an RMI/IIOP client that accesses an EJB. For a very quick intro to the difference, you should look at an entry Petr Blaha wrote. The Developer's Guide has a detailed discussion of both flavors of application client.

    package client;
    
    import beans.DistantGreeting;
    import javax.ejb.EJB;
    
    public class Main {
        // NOTE: This bean MUST bean static for injection to work!
        // It is in the spec...
        static @EJB DistantGreeting bean;
        
        public static void main(String[] args) {
            System.out.println(bean.returnPoliteGreeting("Mr. Kraemer"));
        }    
    }
    

Now we need to add this new project to the Enterprise Application. The first step is adding an entry for the client in the application.xml file of the SampleOne project.

    <module><java>Client.jar</java></module>
    

Add the Client project to the Enterprise Application, using the Properties dialog for the SampleOne project.

You need to deploy your modified application, to get the new bits (the client and modified EJB ready for users to access).

After the application deploys successfully, you will be able to access a new URL on your server that will Java Web Start the application client. In my case, the URL is http://localhost:8080/sampleone/Client. If you have Java Web Start enabled, you will see a Java Web Start processing for a few seconds. Java Web Start will ask if you want to run code that has an invalid signature. (You will need to press Run for the rest of the magic to happen...)

Then...

Nothing appears to happen!?!

That's right. You will need to enable the Java Console to see the output of the application client. This is covered in detail as part of this entry in the GlassFish Project's Issue Tracker. I have an entry that talks about enabling the Java Console on Mac OS X. Another source has information about enabling the Java Console on various flavors of Windows.

Another alternative is to create an application client that has a graphical user interface written in Swing.

Comments:

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


Vince Kraemer writes the entries in this blog.

Search

Archives
« July 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
31
  
       
Today
News
Blogroll

No bookmarks in folder

automarks