In this book, we will learn how to develop applications on the JBoss Application Server Release 7, which marks a giant leap from previous application server releases. The new application server features a truly modular, blazingly fast container that can be managed either as a standalone process or as part of a domain of servers.
The focus of this book is on application development; therefore, we will need at first to gather all resources required for delivering our applications. More in detail, in this chapter we will cover the following topics:
An overview of Java EE and JBoss AS 7
Preparing your environment for the installation
Downloading and installing JBoss AS 7
Verifying the JBoss AS installation
Installing other resources needed for development
Java EE (formerly called J2EE) embraces a standard set of technologies for server-side Java development. Java EE technologies include servlets, Java Server Pages (JSPs), Java Server Faces (JSF), Enterprise JavaBeans (EJBs), Context Dependency Injection (CDI), Java Messaging Service (JMS), Java Persistence API (JPA), Java API for XML Web Services (JAX-WS), and Java API for RESTful Web Services (JAX-RS), among others.
Several commercial and open source application servers exist that allow developers to run applications compliant with Java EE; JBoss AS is the leading open source solution adopted by developers and, although this is difficult to measure in exact terms, it is likely to be the most widely used application server in the market.
Tip
JBoss AS, the most used application server – myth or fact?
We just threw the stone so we cannot avoid discussing it. There is a common belief that JBoss AS is the favorite application server of developers. Actually, there is no empiric way to measure the popularity of open source software; you may be able to guess it from a number of clues such as the number of downloads and the amount of registered users in the community.
Evaluating each product's community statistics can however be misleading and maybe not even be available to all players in this market. Therefore, if we want to try an approximate comparison, let's move to a neutral field where the world's most used software—Google—rules. A one-minute search on Google trends that includes as search keywords the other big players (Oracle WebLogic, IBM WebSphere, and the open source GlassFish application server) reveals that JBoss AS has the highest trend for 2012 at the time of writing. For more information on this, visit http://www.google.com/trends/?q=jboss,oracle+weblogic,+websphere,glassfish&ctab=0&geo=all&date=2012&sort=0. We will get similar results if we query for 2011.
Another popular instrument of Google is Adwords ; it is used to count the search keywords on a national/worldwide basis. Adwords reveals that JBoss accounts for 1.220.000 monthly searches on Google while WebSphere stops at 1.000.000, Oracle WebLogic stays at 823.000, and Glassfish is around 368.000.
So, although these numbers do not provide the last word on our question (nor do they speak about the quality of the product), they are a good indicator of the developer's sentiment. A word to the wise is enough!
As with all application servers compliant with Java EE, JBoss ships with all the required libraries to allow us to develop and deploy Java applications that comply with Java EE specifications.
Java EE 6, includes several improvements and additions to the specification. The following sections list the major improvements to the specification that are of interest to enterprise application developers.
Java EE 6 includes a new version of JSF. JSF 2.0 includes the following new notable features:
JSF 2.0 adopts Facelets as an official part of the specification. Facelets are an alternative view technology based on pure XML templates that was introduced with Version 2 of the JSF standard. Some of the advantages of Facelets include the ability to define a view in XHTML, the ability to easily create templates, and the ability to develop JSF components using markup, without having to use any Java code.
JSF 2.0 also includes the ability to replace XML configuration elements with annotations, thus greatly speeding up the development of the applications.
EJB 3.1 was designed around the concept of ease of development for users. Now designing an EJB application is much easier and less error-prone than in the past. Some enhancements provided with this application are as follows:
Local interfaces are now optional as an actual bean instance and can be injected into local clients. Singleton session beans can be used to manage application states. Session beans can now be invoked asynchronously, allowing us to use session beans for tasks that were previously reserved for JMS and message-driven beans.
The EJB timer service of the enterprise bean container enables you to schedule timed notifications for your EJBs. You can schedule a timed notification to occur according to a calendar schedule either at a specific time or at timed intervals.
Finally, Enterprise JavaBeans can now be packaged inside a Web ARchive (WAR) file. This feature greatly simplifies EJB packaging, as in the past an Enterprise ARchive (EAR) file was needed to package web functionality and EJB functionality into a single module.
JPA was introduced as a standard part of Java EE in Version 5 of the specification. JPA was intended to replace entity beans as the standard object-relational mapping framework for Java EE. JPA adopted ideas from third-party object-relational frameworks such as Hibernate and JDO and made them part of the standard.
JPA 2.0 improves on JPA 1.0 in a number of areas:
It provides transparent support for bean validation (JSR-303)
It provides enhanced collection support with the introduction of the
@ElementCollection
and@OrderColumn
annotationsJPA queries can now be built through the new Criteria API, reducing the reliance on JPQL
The JPA Query Language (JPQL) has improved; it allows adding support for SQL-like CASE expressions, and NULLIF and COALESCE operators
Contexts and Dependency Injection (CDI) (JSR 299) defines a set of services for the Java EE environment that makes applications much easier to develop. CDI leverages a simpler integration between the Web (JSF) and business logic (EJB) tiers, resulting in a significantly simplified programming model for web-based applications; it also provides a programming model suitable for rapid development of simple, data-driven applications. This is a domain where Java EE has been perceived as overly complex in the past.
Java Servlets API 3.0 provides easier web application development with enhanced annotations and integrated Web 2.0 programming model support, security enhancements, asynchronous support, pluggability, simplified configuration, and other improvements.
Java EE 6 has adopted JAX-RS as an official part of the Java EE specification. JAX-RS is a Java API for developing RESTful web services. A RESTful web service exposes a set of resources that identify the targets of the interaction with its clients. Resources are identified by URIs; they provide a global addressing space for resource and service discovery.
The seventh release of JBoss AS is quite different from all other server releases and, as a matter of fact, has improved on several key points, especially where needed most, that is, in the management area. Some of the most notable improvements include:
The application server can now be part of a Managed Domain , which provides the centralized management of multiple server instances and physical hosts, or it can just be a Standalone Server that allows for a single server instance.
The Management Web Console and Management Command Line Interface (CLI) are brand new interfaces for managing your domain or standalone AS instance. There is no longer any need to edit XML configuration files manually. The Management CLI even offers a batch mode so that you can script and automate management tasks.
The class-loading mechanism has been made completely modular so that modules are loaded and unloaded on demand. This provides performance and security benefits as well as very fast startup and restart times.
JBoss AS 7 starts and stops very quickly, which is especially beneficial to developers. It uses fewer resources and is extremely efficient in its use of system resources.
Behind the scenes, the new application server is designed around a brand new kernel; this is now based on two main projects:
JBoss Modules: This handles the class loading of resources in the container. You can think of JBoss Modules as a thin bootstrap wrapper for executing an application in a modular environment.
Modular Service Container (MSC): This provides a way to install, uninstall, and manage the services used by a container. MSC further enables the injection of resources into services and dependency management between services.
In the next section, we will describe all the required steps for installing and starting a new application server.
The first step in learning about the application server will be to install all the necessary stuff on your machine in order to run it. The application server itself requires just a Java Virtual Machine environment to be installed.
As far as hardware requirements are concerned, you should be aware that the server distribution, at the time of writing, requires about 75 MB of hard-disk space and allocates a minimum of 64 MB and a maximum of 512 MB for a standalone server.
In order to get started, this is our checklist:
Install the Java Development Kit where JBoss AS 7 will run
Install JBoss AS 7.1.1
Install the Eclipse development environment
Install the Maven release management tool
At the end of this chapter, you will have all the instruments to get started with the application server.
The first mandatory requirement is to install a JDK 1.6 / JDK 1.7 environment. The Java SE download site can be found at http://www.oracle.com/technetwork/java/javase/downloads/index.html.
Choose to download either Java SE 6 or Java SE 7, and install it. If you don't know how to install it, please take a look at the following link:
http://docs.oracle.com/javase/7/docs/webnotes/install/index.html
Once you have completed your installation, run the java -version
command from a command prompt to verify that it is correctly installed. Here is the expected output from a Windows machine:
C:\Windows>java -version java version "1.7.0_02" Java(TM) SE Runtime Environment (build 1.7.0_02-b13) Java HotSpot(TM) Client VM (build 22.0-b10, mixed mode, sharing)
The JBoss application server can be downloaded for free from the community site, http://www.jboss.org/jbossas/downloads/.
As you can see from the following screenshot, as I'm writing this book, the latest stable release is the 7.1.1 (Brontes); it features a Certified Java EE 6 Full Profile:

Once you have chosen the appropriate server distribution, you will then be warned that this download is part of a community release and, as such, is not supported.
Note
If you need Enterprise support for your applications, you can opt for the Red Hat Enterprise Platform (EAP). It is now available at the same URL location as the AS 7 release and pick up a trial of the EAP 6.0 that was built from JBoss AS 7. Compared to the community version, the EAP has gone through different quality tests and might be different in terms of features/packaging from the Community version.
Installing JBoss AS is a piece of cake: it does not require anything else besides unpacking the archive jboss-as-7.1.1.Final.zip
.
Windows users can simply use any uncompress utility such as WinZip or WinRAR, taking care to choose a folder name that does not contain empty spaces. Unix /Linux should use the unzip
shell command to explode the archive:
$ unzip jboss-as-7.1.1.Final.zip
After you have installed JBoss, it is wise to perform a simple startup test to validate that there are no major problems with your Java VM/operating system combination. To test your installation, move to the bin
directory of your JBOSS_HOME
directory and issue the following command:
standalone.bat # Windows users $ standalone.sh # Linux/Unix users
Here is the screenshot of a sample JBoss AS 7 startup console:

The preceding command starts up a JBoss standalone instance that's equivalent to starting the application server with the run.bat/run.sh
script used by earlier AS releases. You will notice how amazingly fast-starting the new release of the application server is; this is due to the new modular architecture that only starts up necessary parts of the application server container needed by loaded applications.
If you need to customize the startup properties of your application server, open the standalone.conf
file (or standalone.conf.bat
for Windows users) where the memory requirements of JBoss have been declared. Here is the Linux core section of this file:
if [ "x$JAVA_OPTS" = "x" ]; then JAVA_OPTS="-Xms64m -Xmx512m -XX:MaxPermSize=256m -Dorg.jboss.resolver.warning=true -Dsun.rmi.dgc.client.gcInterval=3600000 - Dsun.rmi.dgc.server.gcInterval=3600000" fi
So, by default, the application server starts with a minimum memory requirement of 64 MB of heap space and a maximum of 512 MB. This will be just enough to get started; however, if you need to run core Java EE applications on it, you will likely require at least 1 GB of heap space or up to 2 GB or more depending on your application type. Generally speaking, 32-bit machines cannot execute a process whose space exceeds 2 GB; however, on 64-bit machines, there is essentially no limit to the process size.
You can verify that the server is reachable from the network by simply pointing your browser to the application server's welcome page, which is reachable by default at the well-known address, http://localhost:8080
.

If you have been using previous releases of the application server, you might have heard about the twiddle command-line utility that queried the MBeans installed on the application server. This utility has been replaced by a more sophisticated interface named the Command Line Interface (CLI); it can be found in JBOSS_HOME/bin
.
Just launch the jboss-cli.bat
script (or jboss-cli.sh
for Linux users), and you will be able to manage the application server via a shell interface.

We have just started an interactive shell session that is also able to use the command-line completion (by pressing the Tab key) to match partly typed command names. No more searches are needed for finding the exact syntax of commands!
Note
In the previous screenshot, we have just connected to the server using the connect
command; it uses the loopback server address and plugs into port 9999 by default.
The command-line interface is discussed in depth in Chapter 9, Managing the Application Server, which is all about server-management interfaces; we will, however, get an initial taste of its basic functionalities in the next sections to get you accustomed to this powerful tool.
Probably the easiest way to stop JBoss is by sending an interrupt signal with Ctrl + C.
However, if your JBoss process was launched in the background or, rather, is running on another machine, you can use the CLI interface to issue an immediate shutdown
command:
[disconnected /] connect Connected to localhost:9999 [localhost:9999 /] :shutdown
There is actually one more option to shut down the application server that is pretty useful if you need to shut down the server from within a script. This option consists of passing the --connect
option to the admin shell, thereby switching off the interactive mode:
jboss-cli.bat --connect command=:shutdown # Windows jboss-cli.sh --connect command=:shutdown # Unix / Linux
Shutting down the application server, which is running on a remote machine, is just a matter of providing the server's remote address to the CLI, and for security reason, a username and password (See next chapter to learn more about user creation):
[disconnected /] connect 192.168.1.10 Authenticating against security realm: ManagementRealm Username: admin1234 Password: Connected to 192.168.1.10:9999 [192.168.1.10:9999 / ] :shutdown
The command-line interface contains many useful commands. One of the most interesting options is the ability to reload the AS configuration or parts of it using the reload
command.
When issued on the root node path of the AS server, it is able to reload the services' configuration:
[disconnected /] connect Connected to localhost:9999 [localhost:9999 /] :reload
The development environment used in this book is Eclipse, known by Java developers worldwide, and it contains a huge set of plugins to expand its functionalities. Besides this, Eclipse is the first IDE that is compatible with the new application server.
So, let's move to the downloading page of Eclipse that is located at http://www.eclipse.org.
From there, download the latest Enterprise Edition (at the time of this writing, it is Version 4.2 and is also known as Juno ). The compressed package contains all the Java EE plugins already installed and requires about 210 MB of disk space:

Once you have unzipped the previously downloaded file, you will see a folder named eclipse
. In that folder, you will find the Eclipse application (a big blue dot). It is recommended that you create a shortcut on the desktop to simplify the launching of Eclipse. Note that, just as with JBoss AS, Eclipse does not have an installation process. Once you have unzipped the file, you are done!
The next step will be installing the JBoss AS plugin that is a part of the suite of plugins named JBoss Tools. Installing new plugins in Eclipse is pretty simple; just follow these steps:
From the menu, navigate to Help | Install New Software.
Then, click on the Add button where you will enter JBoss Tools' download URL (along with a description)
http://download.jboss.org/jbosstools/updates/stable/juno/
:As you can see in the preceding screenshot, you need to check the JBossAS Tools plugin and move forward to the next options to complete the installation process.
Once done, restart it when prompted.
Note
You can also download JBoss Tools as individual zips for offline installation. See JBoss Tools Downloads (http://www.jboss.org/tools/download).
Now, you should be able to see JBoss AS 7 enlisted as a server by navigating to New | Server from the upper menu and expanding the JBoss Community option:
Completing the server installation in Eclipse is quite straightforward as it just requires pointing to the folder where your server distribution is; we will therefore leave this to the reader to implement as a practical exercise.
Since this book is all about development, we should also account for some other alternatives that might suit your programming styles or your company standards better. So, another valid alternative is IntelliJ IDEA that is available at http://www.jetbrains.com/idea/index.html.
IntelliJ IDEA is a code-centric IDE focused on developer productivity. The editor exhibits a nice understanding of your code and makes great suggestions right when you need them, and is always ready to help you shape your code.
Two versions of this product exist—Community edition and Ultimate edition—that require a license. In order to use Java EE and the JBoss AS plugin, you need to download the ultimate edition from http://www.jetbrains.com/idea/download/index.html and then simply install it using the installation wizard.
Once you have installed the Ultimate edition, you will be able to get started with developing applications with JBoss AS 7 by going to File | Settings and choosing the IDE Settings option from there. There you can choose to add new application server environments:

Another development option that is quite popular among developers is NetBeans (http://netbeans.org), which has recently added support for JBoss AS 7 in its development built 7.3.1
Besides graphical tools, you are strongly encouraged to learn about Maven, the popular software and release management tool. By using Maven, you will enjoy:
A standard structure for all your projects
A centralized and automatic management of dependencies
Maven is distributed in several formats, for your convenience, and can be downloaded from http://maven.apache.org/download.html.
Once the download is complete, unzip the distribution archive (for example, apache-maven-3.0.4-bin.zip
) to the directory in which you wish to install Maven 3.0.4 (or the latest available version), for example, C:\apache-maven-3.0.4
.
Once done, add the M2_HOME
environment variable to your system so that it will point to the folder where Maven has been unpacked.
Next, update the PATH
environment variable by adding the Maven binaries to your system path. For example, on the Windows platform, you should include %M2_HOME%/bin
in order to make Maven available in the command line.
Once you have completed your installation, run mvn --version
to verify that Maven has been correctly installed:
mvn --version Apache Maven 3.0.4 (r1075438; 2011-02-28 18:31:09+0100) Maven home: C:\apache-maven-3.0.4\bin\.. Java version: 1.6.0, vendor: Sun Microsystems Inc. Java home: C:\Programmi\Java\jdk1.6.0\jre Default locale: it_IT, platform encoding: Cp1252 OS name: "windows xp", version: "5.1", arch: "x86", family: "windows"
Tip
Downloading the example code
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com . If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
In this chapter, we have run our first mile on the track to application server development. We have introduced the new features of the application server and we had an overview of the Java EE 6 API.
Next, we have discussed the installation of the AS and all the core components that include the Java Development Kit (JDK) and a set of development tools, such as Eclipse and Maven, that will be your companions on this journey.
In the next chapter, we will summarize all the application server features, with a special focus on the components and commands needed to deliver an application, which is the main aim of this book.