Java is a versatile development platform. It is subdivided in different editions, each having a specific purpose: Java SE for desktop application development, Java ME for mobile applications, Java Card for Smart Cards development, etc. One of these editions is the Enterprise Edition, or Java EE, which is currently in its sixth version. Java EE allows the creation of enterprise and Web applications. By “enterprise application” we mean a software that has the goal of solving a problem of a company/organization as a whole and, for this reason, has particular requirements, such as scalability, security, accessibility, among others.
To be executed, enterprise applications have to be deployed in Java application servers. One of these servers is GlassFish, developed by Oracle and distributed in free (open source) and paid (commercial) version. GlassFish was born in 2005 as a project from Sun, based on their commercial application server, the Sun Java Application Server 9. Its 1.0 version was developed to serve as reference implementation for the Java EE 5 standard (a complete implementation of the standard to show how it would work).
Since the goal of this first version was to provide a reference implementation for Java EE 5, it lacked functionalities that would make GlassFish an interesting alternative for use in production by the industry, like high availability and fault tolerance. Therefore, that was precisely the goal of version 2.0 of GlassFish, which added clustering capabilities to the server.
With the publication, in December 2009, of the new version of the standard for enterprise application development, Java EE 6, GlassFish was again used as reference implementation and history repeated itself: GlassFish 3.0 provided a complete implementation of the Java EE 6 specification, but without functionalities that would make it a scalable and available application server in production. It was only at the end of February 2011 that the old clustering capabilities, plus some other improvements, were added to the server and GlassFish 3.1 was released. Some of these improvements – as published by the GlassFish development team itself – were:
- Dynamic service invocation (services are initialized only when they are needed);
- Extensibility improvements in the server itself and its administration interface;
- Initialization and deployment cycles 29% faster than in version 3.0.1;
- Session, Stateful Session Beans and EJB Timers preserved between redeploys;
- Better tool integration (NetBeans, Eclipse, Maven plug-in for unit testing);
- High availability performance 34% better than version 2.1.1;
- Centralized cluster administration with improved scalability (up to 100 managed instances);
- Support to authentication with PAM;
- Upgradeable from versions 2.x or 3.x to 3.1;
- More extra functionalities available in the commercial version (Oracle GlassFish Server), such as interoperability with WebSphere and WebLogic, backup and recovery, profiler, load balancer, etc.
This article, however, demonstrate with some examples four of the many novelties of GlassFish 3.1, namely:
- Cluster management: creation of multiple server instances (local or remote), with session replication for high availability of applications, other than scalability through load balancing;
- Application versioning: ability to deploy different versions of the same application, adding tags that identify each one, and allowing for the developer to enable one of the versions at any time;
- Application-scoped resources: to simplify deployment of applications, it is possible to declare resources (for instance, database connections) inside the EAR/WAR package, without the need to use the server's administration interface. Such resources will have application scope, being accessible only from within that scope;
- RESTful API for server administration: allows to read and change the server's configuration resources through a RESTful API, making it easier to write programs that integrate with GlassFish.
Despite the focus on these four new features, we start the article detailing how to download, install and integrate GlassFish with the two most popular open source Java IDEs: Eclipse and NetBeans.
Download and Installation
Like previous versions, GlassFish can be used standalone or integrated with an IDE. This section describes the procedures for installing GlassFish in the IDEs Eclipse and NetBeans. See Links for the URLs for downloading these tools.
The most recent version of this IDE, NetBeans 7.0.1, already comes with GlassFish 3.1 if you download the Java EE bundle. If you download this bundle and go through the install wizard, GlassFish 3.1 will be installed together with NetBeans and will be ready to use.
To test this integration, you can create a new Java EE 6 project and deploy it in GlassFish. To do so, click in File > New Project... and, in the Java EE category, choose Enterprise Application and click Next. In the following screen, choose a name for your project (e.g., HelloGlassFish) and click Next again. In the final step of the wizard, check that the GlassFish Server is selected and click Finish.
Following those steps, an enterprise project with a Web page called JSP Page containing the phrase Hello, World! will be created. Therefore, if this application is deployed in the server it will be possible to open this page and, thus, verify that the NetBeans – GlassFish integration works. Right-click your project (HelloGlassFish) and click Run. After some time, NetBeans will have started the GlassFish 3.1 server and opened your standard Web browser at the address http://localhost:8080/HelloGlassFish-war/ (HelloGlassFish might be changed to whatever name you gave to your project).
Administration of the GlassFish server can also be done from inside NetBeans, opening the Services tab and expanding the Servers item, as shown in Figure 1. The NetBeans – GlassFish integration allows you to manage several things, among them: register remote and local domains, start (in regular or debug mode) and stop the server, see the logs from local or remote domains, see JavaDoc documentation for the Java EE 6 API from within the IDE, create and register server resources. We will see some of these features throughout this article.
Figure 1. Administration of GlassFish at the Services tab in NetBeans.
When an administrative task cannot be conducted from within the IDE, the administration console should be opened by right-clicking in GlassFish Server 3.x and choosing the option View Admin Console. Some of the demonstrations done in this article will have to use this administrative interface.
Eclipse (“for Java EE developers” package) does not come with GlassFish installed so the server has to be installed separately (see the next subsection). Moreover, integration with GlassFish (an adapter, as they call it) has to be installed from inside the IDE. To do so, follow these steps:
- Open Eclipse in the Java EE perspective and show the Servers view;
- Right-click the Servers view and select New > Server;
- Click on the Download additional server adapters link and wait for the list to load;
- Select Oracle GlassFish Server Tools and follow the wizard until the installation begins;
- Once the installation finishes, restart the IDE, right-click the Servers view again and open New > Server one more time;
- Select GlassFish 3.1 from the GlassFish folder and click Next;
- Point to the glassfish folder inside the server's installation and follow the wizard to the end.
After that you should see the GlassFish server in the Servers view, as shown in Figure 2. You can now test the integration the same way as suggested for NetBeans in the previous section: creating a new enterprise project and deploying it in GlassFish.
Figure 2. GlassFish integration in Eclipse.
Click in File > New > Enterprise Application Project. Give your project a name (ex.: HelloGlassFish) and click Next. In the following screen, mark the Generate application.xml deployment descriptor checkbox, click the New Module... button, select only the EJB and Web modules, click Finish and finish the wizard so the projects can be created. At this moment three projects should appear in the Project Explorer view: one represents the enterprise application as a whole (HelloGlassFish) and the two others represent its EJB module (HelloGlassFishEJB) and its Web module (HelloGlassFishWeb).
Before testing your application, however, you need to create at least one EJB, otherwise the server will show an error message (Invalid ejb jar [HelloGlassFishEJB.jar]: it contains zero ejb). Therefore, right-click HelloGlassFishEJB > ejbModule and choose New > Session Bean (EJB 3.x). Fill in any values you want for the fields Java package (e.g., hello) and Class name (e.g., HelloBean) and click Finish. Now that the application has one EJB, it can be deployed to test the GlassFish plug-in. In the Project Explorer view, right-click the HelloGlassFish project and select Run As > Run on Server. Next, click Window > Show View > Console to follow the server's startup and, after the deploy is finished, open the URL http://localhost:8080/HelloGlassFishWeb/ using your favorite browser.
Like in NetBeans, in Eclipse is also possible to configure some aspects of the server in the Servers view. The administration console can also be brought up by right-clicking GlassFish 3.1 at localhost (see Figure 2) and choosing GlassFish > View Admin Console.
Installing and executing GlassFish separately
If you want or need to install GlassFish separately, just execute the install wizard available for the Windows, Linux or Mac platforms (see Links for the download URL). The server will be installed in the directory specified during the installation procedure. In the following instructions, the variable $GF_HOME symbolizes this directory and should be replaced by the path to the folder you have chosen for the installation of the server in your system.
After installation, to execute GlassFish, use the administration tool asadmin (the next section talks about some aspects of this tool). Open a terminal (command prompt) and access the directory $GF_HOME/glassfish/bin and execute the following command:
In the commands demonstrated in this article, Linux and Mac users should include the characters ./ in the beginning of the command (e.g., ./asadmin start-domain). These characters are necessary to indicate to the operating system that the user wishes to execute a program that is found in the current directory.
With the server running, it is possible to access the administration console by opening the URL http://localhost:4848/ in a browser, like shown in Figure 3. Notice in the figure that by starting GlassFish manually, the path to the server's log file is informed during the startup procedure in case the user wants to follow it: $GF_HOME/glassfish/domains/domain1/logs/server.log.
Figure 3. Manually running GlassFish and its administration console.
Without an IDE, deploying and testing an application can be done in two ways: the first alternative is to copy the application's EAR (or WAR) file to the $GF_HOME/glassfish/domains/domain1/autodeploy directory and follow the deploy through the server's log; the second is to open the Applications item in the Common Tasks tree in the administration console, click on Deploy... and fill in the form, specifying that same EAR/WAR file.
The asadmin tool
The GlassFish distribution has a program called asadmin (in folder $GF_HOME/glassfish/bin) that can be used via a terminal to start and stop the server, other than change several configuration values (which can also be changed through the administration console and its Web interface).
The asadmin tool can make changes in both local or remote installations of the server through a series of subcommands. Some examples of subcommands are: start-domain, stop-domain, deploy (an application), etc.
The tool can work in command shell invocation mode or in multi-command mode. In the former case, the command asadmin [asadmin parameters] <subcommand> [subcommand parameters] should be typed directly at the terminal and the tool will execute the specified subcommand, returning to the terminal's command prompt immediately afterwards. In the latter case, the asadmin [asadmin parameters] (without a subcommand) is executed in the terminal and the tool will open its own internal command prompt, through which subcommands can be executed directly (using subcommand [subcommand parameters]). The multi-command mode is interesting when many different subcommands have to be executed in sequence.
For further information, you can refer to the help pages of asadmin. The following commands are useful in this case:
- asadmin help: shows the syntax of the tool and its parameters;
- asadmin list-commands: shows a list of existing subcommands;
- asadmin help <subcomando>: shows the syntax and the parameters of a specific subcommand (ex.: asadmin help deploy).
Installing GlassFish components with the Update Tool
GlassFish has a tool called Update Tool which allows you to install server components which have not yet been installed and update components that already have. To execute it, go to the directory $GF_HOME/bin/ and execute the program updatetool (updatetool.bat on Windows). In case the tool has not been installed together with GlassFish during its installation procedures, it will be downloaded and installed the first time you execute it (which should take some time, depending on your Internet connection).
Once it is opened, the existing GlassFish servers in your system will be listed at the left-hand side, under Application Images. For each server, it is possible to check the components available for installation (Available Add-ons), for updating (Available Updates) and the ones already installed (Installed Components). Figure 4 shows the tool with the list of components that can be installed.
Figure 4. GlassFish's Update Tool.
In the next article, we will see clustering features.
Download page for GlassFish 3.1.
Download page for the NetBeans IDE.
Download page for the Eclipse IDE.
GlassFish documentation about remote management via SSH.
Example application for testing clusters, obtained from GlassFish 2 and published in the blog of Arun Gupta.
Instructions for the configuration of load balancing on GlassFish using Apache and mod-jk.
Framework for the implementation of RESTful services and clients.
Download page for the cURL tool.
Source-code of the JSON-java parser.