Friday Dec 23, 2005

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.

Wednesday Dec 21, 2005

Web Application/EJB 3.0 "Hello, <USER>!"

This entry covers the "usual" first Java EE 5 application: Hello, World.

The first thing you need to do is create a new project that will be deployed onto the GlassFish instance that you registered earlier.

There are a couple different approaches you could take here. I recommend that you create an Enterprise Application, and let the wizard create the Web Application subproject. Part of the reason for this is lets you be lazy later on. While you may like creating servlets by hand, I like to let the wizards do it. Since the Servlet wizard is associated with Web Application projects, using an approach that doesn't use the Web Application project will mean you need to do more work.

One other thing to notice about the wizard is the fact that I have 'Set Source Level to 1.4' UNCHECKED. This will allow you to use annotation later on, so make sure you have it unchecked, too.

After the wizard finishes, create a java library project. It is going to be the project where you do the EJB 3.0 devlopment.

The Web Application and Java Library project needs to have access to the javaee.jar file, which is in <GlassFish-Install>/lib. It has the definition of the various Java EE 5 annotations that you'll use later on. You use the Project Properties dialog to add this jar file to the Web Application and Java Library projects.


This image is from the Web Application project.

The two Project Properties dialogs are very similar. You will see an additional column in the Web Application project's properties dialog. Make sure 'Package' is unchecked when you add javaee.jar to the Web Application project. Otherwise, the jar will be packaged in your web application's WEB-INF/lib directory, which is redundant....

You need to add the Java Library project to the compile properties of the Web Application, and the packaging properties of the Enterprise Application project. (Don't forget to uncheck the 'Package' attribute when you add the library project to the Web Application project).


Web Application Properties


Enterprise Application Properties

You need to add the Java Library that will implement your EJB 3.0 beans to the application.xml manually. Double clicking on the application.xml node, shown below, will open the file in an XML editor. It is easy to add the following:

    <module><ejb>Greeter.jar</ejb></module>
    
immediately following the module entry for PleasedToMeetYou.war.


Expanded Enterprise Application Project: application.xml visible

At this point the infrastructure is in place and it is time to start writing Java EE 5 code. The coding required to create an EJB 3.0 bean is wonderfully concise (compared to EJB 2.1).

First, we define the interface that says what a bean can do. This the text of the interface that I created.

    package beans;
    
    import javax.ejb.Local;
    
    @Local public interface FriendlyGreeting {   
        public String returnFriendlyGreeting(String name);    
    }
    

After defining the interface we need to create the implementation. That is relatively straight-forward, too.

    package impl;
    
    import beans.FriendlyGreeting;
    import javax.ejb.Local;
    import javax.ejb.Stateless;
    
    @Stateless public class GreetingImpl implements FriendlyGreeting {
        
        public String returnFriendlyGreeting(String name) {
            return "Well, good to see you again, "+name+"!";
        }
        
    }
    
Since NetBeans supports annotation and NetBeans 5.0 has some significant editor improvements, writing this code is easy. Since there are fewer interrelated files, many of the wizards that create EJB 2.1 artifacts seem heavy.
The one exception would probably be in the development of entity beans, which still require a lot of meta-data.

Once the bean is ready, it is time to call it. In my example, I am going to create a servlet to do that. Other folks have demonstrated other methods for calling your EJB 3.0 bean from a Web Application.

The easy way to create the servlet is to use the New item from the Web Application project's right-click menu.

Here is the text of my servlet.

    package lets;
    
    import beans.FriendlyGreeting;
    import java.io.\*;
    import java.net.\*;
    import javax.ejb.EJB;
    
    import javax.servlet.\*;
    import javax.servlet.http.\*; 
    
    public class EJB3Greeting extends HttpServlet {
        
        @EJB FriendlyGreeting bean;
        
        /\*\* Processes requests for both HTTP GET and POST methods.
         \* @param request servlet request
         \* @param response servlet response
         \*/
        protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            //\* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet EJB3Greeting</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet EJB3Greeting at " + request.getContextPath () + "</h1>"+
                    bean.returnFriendlyGreeting(request.getParameter("name")));
            out.println("</body>");
            out.println("</html>");
             //\*/
            out.close();
        }
    
I have left out the boilerplate code that NetBeans has put into a code fold. If you want to see it, click on the boxed-plus icon that appears along the left hand side of the editor (after you use the wizard to create servlet).

At this point we are almost done. There is still one thing that needs to be changed and NetBeans has been finicky lately when I do this edit. You need to change the text of the web.xml, so that it refers to a Java EE 5 schema, instead of the J2EE 1.4 schema. The thing to do is replace the initial web-app element (which will look something like:

<web-app version="2.4" 
         xmlns="http://java.sun. com/xml/ns/j2ee" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
with somehting like this
<web-app version="2.5" 
         xmlns="http://java.sun. com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
The easiest way to make this stick is to copy the text above and paste it over the "old" web-app element in the web.xml file.

At this point you are ready to run the application. The thing to do is make sure that the project SampleOne is the Main Project. You can use the 'Set Main Project' item from the Enterprise Application right click menu to make it so. If it is the main project, then you can use the F6 key, to trigger the 'Run Main Project' item of the Run menu.

The IDE will start the server, deploy the application and open the browser. It will trigger the default welcome-file, index.jsp. If you append "/EJB3Greeting?name=foobar" to the URL, you will see this:

After going through this initial exercise, I would say that the Java EE 5 has done a good job of simplifying 'Hello, World!'. Java EE 5 covers a lot more technologies that what I've used here. I will look at some of these other technologies and see wheher they have the improved their ease of development.

Tuesday Dec 20, 2005

Register a Java EE 5 server

While NetBeans 5 doesn't support Java EE 5, it does allow you to register a Java EE 5 implementation as a deployment target.

I found some instructions for registering a GlassFish instance into the NetBeans 5 Beta environment. Fortunately, they are mostly incorrect now. [Folks had a lot of trouble using the UI that was there in NetBeans 5 Beta, so we fixed it.] One important point from those instructions is still valid though; you must start NetBeans with J2SE 5.0 (aka jdk 1.5). I will walk you through the new screens and choices that are available in NetBeans 5.

Servers are registered in the NetBeans environment on the Runtime explorer (or tab).

Right click on the Servers node to expose the 'Add Server...' item and select it.

Choose 'Sun Java System Application Server' as the Server and provide Name for it.

The next page gives you a number of different options. Most users that install GlassFish will be using a private copy with its default domain. This is the option that is selected by default. Once the user enters the value of the 'Platform Location', the Domain combobox is automatically populated.

If you enter the path to a GlassFish installation, you may see an error message, if you have started the IDE in J2SE 1.4. GlassFish (and all of Java EE 5, really) depends on J2SE 5.0 features. NetBeans needs to load classes from the libraries that ship with GlassFish. The NetBeans FAQ has an entry about sepecifiying the JDK to use.

There are other options that are supported by additional pages of the wizard. The user can register a domain that they have created using 'asadmin create-domain', by choosing 'Register Local Domain'. If the user needs to deploy modules and applications to a remote server, they can register it using 'Register Remote Domain'.

In previous version of the IDE and plugin, there has been the ability to create a personal domain in situations where the user cannot use one of the other local domains. This had been a hidden feature, unlocked by analyzing the application server's installation and default domains. in this release, we expose the feature explicitly. There is a fair bit of data verification that the you need to do, so be prepared.

Once your server is registered, you can right click on it to expose actions to control it, view the log file and open the web-based administration interface, known as the Admin Console. Most of these actions only apply to LOCAL domains, since the plugin depends on various scripts in the insatllation directory to implement the actions.


Expanded Servers Node: GlassFish instance visible.

When you start the server, the server log is exposed. This allows you to diagnose start-up issue. The log is also exposed when you Run/Debug/Deploy projects to a Sun Java System Application Server.

The glyphs on the left side of the log window let you Start, Debug, Restart, Stop and Refresh the server status in a single click.

About


Vince Kraemer writes the entries in this blog.

Search

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
News
Blogroll

No bookmarks in folder

automarks