Book Image

JBoss AS 7 Development - Second Edition

By : Francesco Marchioni
Book Image

JBoss AS 7 Development - Second Edition

By: Francesco Marchioni

Overview of this book

JBoss Application Server meets high standards of reliability, efficiency, and robustness, and is used to build powerful and secure Java EE applications. It supports the most important areas of Java Enterprise programming including EJB 3.1, Contexts and Dependency Injection, JAX-WS and JAX-RS web services, the security framework, and more. Getting started with JBoss application server development can be challenging; however, with the right approach and guidance, you can easily master it and this book promises that.Written in an easy-to-read style, this book will take you from the basics of JBoss AS—such as installing core components and plugins—to the skills that will make you a JBoss developer to be reckoned with, covering advanced topics such as developing applications with the JBoss messaging service, JBoss web services, clustered applications, and more.You will learn the necessary steps to install a suitable environment for developing enterprise applications on JBoss AS. You will also learn how to design Enterprise applications using Eclipse, JBoss plugins, and Maven to build and deploy your applications. Readers will learn how to enable distributed communication using JMS. Storing and retrieving objects will be made easier using the Java Persistence API. The core section of the book will take you into the programming arena with tested, real-world examples. The example programs have been carefully crafted to be easy to understand and useful as starting points for your applications. This practical guide will show you how to gain hands-on experience rapidly on Java EE development using JBoss AS with easy-to-understand and practical programming examples.
Table of Contents (19 chapters)
JBoss AS 7 Development
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Installing the server and client components


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.

Installing Java SE

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

Testing the installation

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)

Installing JBoss AS 7

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

Tip

Security warning

Unix/Linux users should be aware that JBoss AS does not require root privileges as none of the default ports used by JBoss are below the privileged port range of 1024. To reduce the risk of users gaining root privileges through JBoss AS, install and run JBoss as a non-root user

Starting up JBoss AS

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.

Connecting to the server with the Command Line Interface

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.

Stopping JBoss

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
Locating the shutdown script

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
Stopping JBoss on a remote machine

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

Restarting JBoss

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

Installing the Eclipse environment

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!

Installing JBoss Tools

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:

  1. From the menu, navigate to Help | Install New Software.

  2. 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/:

  3. 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.

    Tip

    Enter JBossAS into the filter field to quickly find out which is the JBoss AS Tools plugin among the large set of JBoss Tools.

  4. 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).

  5. 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.

Alternative development environments

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

Installing Maven

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.

Testing the installation

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.