Welcome!

Java Authors: Maureen O'Gara, Bruce Armstrong, Liz McMillan, Walter H. Pinson, III, Yakov Werde

Related Topics: Java, ColdFusion, Adobe Flex, Open Source, AJAX & REA, Silverlight, Open Web

Java: Article

A New Weapon in Developing Rich Client Applications

Communicating JavaFX with EJB 3.0 Technology

In this article we'll try to build a simple client application using JavaFX technology using the Netbean 5.5 editor, and this scripting client will communicate with EJB 3.0 technology that resides in the Sun Java Application Server 9 to provide the business functionality.

Prerequisite
You'll need Netbean 5.5 and the Sun Java Application Server 9 SDK installed on your operating system. You can download the first at http://www.netbeans.org/download/ and the other at http://java.sun.com/javaee/downloads/.

You can get the information on installing the JavaFX plug-in at https://openjfx.dev.java.net/javafx-nb55-plugin-install.html.

Server Scenario
Now that you've installed the software required, we'll try to build the EJB module with the Netbean Editor. If you're already familiar with EJB 2.1, you'll see that EJB 3.0 will cut the time window for coding process. Follow the steps bellow to develop and deploy the EJB component:

1.  Create an EJB Module Project
Click File -> New Project, choose the Enterprise category, and select the EJB Module under the projects selection list (see Figure 1).

Click Next -> assign the project name ServerModule, and don't forget to select the server for the Sun Java Application Server with Java EE Version 5.0 (see Figure 2).

Click Finish -> you'll see the new project created under the left pane of the Netbean 5.5 editor.

2.  Implement the business process code in the EJB component
In this step we'll create an EJB component called ServerUtility; this class will provide two EJB methods. The first method is getServerDate, which is serve the client to get the server date and formats the date in a "dd MMMM yyyy" pattern, and the second method is currentAge with birthdays as an input parameter; this method will provide the functionality to calculate your current age based on your birthday against the server date.

To create the EJB component you can select the Source Packages on the ServerModule project -> Right Click -> New -> Session Bean, Netbean Editor will prompt you with a form to fill in the specification parameters for the new EJB component that will be created such as EJB Name, packages, Session Type, and Create Interface. In our example we'll use the parameters:

a.  EJB Name -> ServerUtility
b.  Package -> org.sofu.ejb
c.  Session Type -> Stateless
d.  Create Interface -> Remote

The code in Listing 1 below will be automatically generated by the Netbean 5.5 Editor, and shows the EJB 3.0 capable of using the annotation to define the session type and the other definition parameters that were previously defined only by the ejb-jar.xml file. EJB 3.0 also offers some other features to ease the development effort; this is a list of the enhancements in EJB 3.0 in general:

  •   Simplified bean classes
  •   Environment access by using annotation or XML
  •   Dependency injection capability
  •   Dynamic environment lookup

If you're interested in finding out more about EJB 3.0 development you can visit Sun Microsystems' Web site at http://java.sun.com. There are a lot of articles there regarding the EJB 3.0 technology.

Now we'll write our method inside the ServerUtilityBean Java code as mention above by right-clicking the ServerUtilityBean code à EJB Method à Add Business Method à. You'll be prompted to supply the method specification such as method name, return type, method parameters, and exceptions. This utility for creating EJB Methods using Netbean will simplify writing the business method; it automatically generates the method skeleton in the remote interface. Two business method implemented in the EJB Bean class are shown in Listing 2.

3.  Deploy ServerModule to the Sun Java Application Server 9.0 Platform Edition
Right-click the ServerModule project -> Deploy Project ->. This process will automatically start the Sun Java Application Server 9.0 and deploy the EJB module to the application server. To verify whether the module deployed successfully, you can move to the runtime tab and check the ServerModule entity under the EJB modules folder (see Figure 3).

Albert Einstein said, "Things should be made as simple as possible - but no simpler." That statement in mind, system designers and developers tend to avoid the complexity of the software development process lifecycle. They think that simplicity is the most elegant way of doing things. In my opinion the fundamental measure of technology innovation is making things simpler. The enhanced EJB Framework and automated utility in the Netbeans Editor makes the perfect match of collaboration that push to simplicity of development process lifecycle.

Client-side Scenario
The implementation of an EJB component on the server platform has been done; now we'll concentrate on developing the client user interface by using JavaFX scripting technology. The following steps will guide you in developing the clients:

1.  Create a JavaFX Project
Click File -> New Project, choose JavaFX, and select JavaFX Application under the projects selection list (see Figure 4).

Click Next -> set the project name as JavaFXTeller (see Figure 5).

Click Finish -> you'll see the new JavaFX project created.

2.  Create gateway code to communicate between JavaFX and the EJB module
To keep the client code simple in accessing the EJB component we'll have to develop simple Java code that acts as a helper or gateway between the client and the server. The code is shown in Listing 3.

3.  Implement client script code
The simplicity of integrating the Java library using JavaFX is one of the advantages offered by this technology. The code example in Listing 4 shows us how to import the Java library and create a class in JavaFX script client code.

In this code you'll see that we put all the client user interfaces that attach to the frame in the attribute client window such as Text, View, FlowPanel, and Canvas. The purpose is to make the script codes more systematic and easy to read rather than put the script code in a continuous form.

The code in Listing 5 shows us how to create an instance of the Command object, that is an object written in Java code as a gateway to access the EJB component residing on the server platform. These lines of code also describe how to create a window instance and how to set the attribute from the window object such as the frame width and height.

As we observe those lines of code we have done build button component and assign it to the clientBtnAttribute on window object, and we implement the action should be done when the button pressed. The cmdCurrentAge will proxy the currentAge method under the ServerUtility EJB component on the server platform. And, as you'll notice from the Command.java code implemented in Listing 6, we need to define the server environment on the gateway code such as PROVIDER_URL, URL_PKG_PREFIXES, and so on, which provides basic information for accessing the EJB module on the server platform.

In Listing 7 we try composing the UI component in the canvas attribute and put the canvas into the FlowPanel (clientPanel). The clientPanel attribute will be assign to the content of the Frame that's the main window in displaying the client form.

4.  Run the JavaFX application
Right-click the ClientModule Project -> Run Project ->. You'll see the window client in Figure 6.

You'll see the EJB module to get the server date invoked from the JavaFX client script via the Command object. If we fill up the textbox with the "dd/MM/yyyy" format and press the "Calculate My Age" button you'll see how old are you now has been stayed on Planet Earth (see Figure 7).

Summary
JavaFX's capacity for interacting with Java code makes client script technology perfect for developing rich client applications that communicate with any existing Java application either as a server platform component or as purely a Java library utility. The diagram below shows the flexibility of accessing a EJB module on the server platform directly or through a custom proxy library (see Figure 8).

Generally speaking JavaFX script client technology provides a new weapon for developing rich client applications that are fully integrated with Java, and in my opinion push this technology to the next level by focusing on the human side of code development such as code pattern and user-friendly UI building.

More Stories By Sonny Hastomo

Sonny Hastomo is an IT architect at Sun Microsystems, Indonesia, for the telecommunication industry division. His currently is focusing on providing solution design, sizing, implementation, consulting services, and quality support to customers in their evaluation of their IT challenges.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.