Book Image

Java EE 7 Development with WildFly

Book Image

Java EE 7 Development with WildFly

Overview of this book

Table of Contents (21 chapters)
Java EE 7 Development with WildFly
Credits
About the Authors
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 components on your machine in order to run it. The application server itself requires just a JDK environment to be installed.

As far as hardware requirements are concerned, you should be aware that the server distribution, at the time of writing this, requires about 130 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, we will need to go over this checklist:

  • Install JDK where WildFly will run

  • Install WildFly

  • Install the Eclipse development environment

  • Install the Maven build 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 8 environment. The Java SE download site can be found at http://www.oracle.com/technetwork/java/javase/downloads/index.html.

Choose the latest version of Java SE 8 and install it. If you don't know how to install it, please take a look at http://docs.oracle.com/javase/8/docs/technotes/guides/install/install_overview.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:\>java –version
java version "1.8.0_11"
Java(TM) SE Runtime Environment (build 1.8.0_11-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.11-b03, mixed mode)

Installing WildFly

The JBoss WildFly application server can be downloaded for free from http://wildfly.org/downloads/.

As you can see in the following screenshot, at the moment of writing this book, the latest stable release of WildFly is 8.1.0.Final, which features a Certified Java EE 7 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 Application Platform.

Compared to the community version, the EAP has gone through different quality tests and might be different in terms of features/packaging. More information about the differences between EAP and the community version can be found at the beginning of this chapter. However, at the time of writing this book, EAP does not yet support Java EE 7, and no road map is publicly available.

Installing WildFly is a piece of cake; it does not require anything else besides unpacking the wildfly-8.1.0.Final.zip archive.

Windows users can simply use any uncompressing utility, such as built-in compressed folders (in newer Windows releases), WinZip, WinRAR, or 7-Zip taking care to choose a folder name that does not contain empty spaces and white spaces. Unix/Linux should use the $ unzip wildfly-8.1.0.Final.zip unzip shell command to explode the archive.

Note

Security warning

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

Starting WildFly

After you have installed WildFly, it is wise to perform a simple start up 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 (the path to which you have unzipped your application server) directory and issue the following command:

standalone.bat    # Windows users
$ ./standalone.sh   # Linux/Unix users

The following is a screenshot of a sample WildFly start-up console:

The preceding command starts up a WildFly standalone instance, which is equivalent to starting the application server with the run.bat/run.sh script used by earlier JBoss AS releases. You will notice how amazingly fast the new release of the application server is; this is due to the modular architecture introduced in Version 7 of JBoss AS, which only starts up necessary parts of the application server container needed by the loaded applications.

If you need to customize the start-up properties of your application server, open the standalone.conf file (or standalone.conf.bat for Windows users), where the memory requirements of Wildfly 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
       -Djava.net.preferIPv4Stack=true"
    JAVA_OPTS="$JAVA_OPTS
        -Djboss.modules.system.pkgs=$JBOSS_MODULES_SYSTEM_PKGS
        -Djava.awt.headless=true"
else
    echo "JAVA_OPTS already set in environment; overriding default settings with values: $JAVA_OPTS"

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 more robust Java EE applications on it, you will likely require at least 1 GB of heap space or 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 can be accessed from the well-known address, http://localhost:8080. The welcome page of WildFly is shown in the following screenshot:

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 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. This is shown in the following screenshot:

We started an interactive shell session that can also use the command-line completion (by pressing the Tab key) to match partly typed command names. No more searches are needed to find the exact syntax of commands!

Note

In the previous screenshot, we connected to the server using the connect command; it uses the loopback server address and plugs into port 9990 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 WildFly

The easiest way to stop WildFly is by sending an interrupt signal with Ctrl + C.

However, if your WildFly 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:9990
[standalone@localhost:9990 /] :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 as shown in the following command line:

jboss-cli.bat --connect command=:shutdown      # Windows
./jboss-cli.sh --connect command=:shutdown       # Unix / Linux
Stopping WildFly on a remote machine

Shutting down the application server that is running on a remote machine is just a matter of providing the server's remote address to the CLI, and for security reasons, a username and password, as shown in the following code snippet (see the 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:9990
[[email protected]:9990 / ] :shutdown

However, you have to remember that you need access to a given port because often, it may be blocked by a firewall.

Restarting WildFly

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, the reload command can reload the services' configuration:

[disconnected /] connect
Connected to localhost:9990
[standalone@localhost:9990 /] :reload

Installing the Eclipse environment

The development environment used in this book is Eclipse, which is 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 download page of Eclipse, which is located at http://www.eclipse.org.

From here, download the latest Enterprise Edition (at the time of writing this book, it is Version 4.4 and is also known as Luna). The compressed package contains all the Java EE plugins already installed. This is shown in the following screenshot:

Once you have unzipped the previously downloaded file, you will see a folder named eclipse. In this 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 WildFly, 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/development/luna/. This is shown in the following screenshot:

  3. As you can see in the preceding screenshot, you need to check the JBossAS Tools plugin and move forward to the next option to complete the installation process.

    Note

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

  4. Once done, restart the process when prompted.

    Note

    You can also download JBoss Tools as individual zip files for an offline installation. See JBoss Tools Downloads at http://tools.jboss.org/downloads/.

  5. Now, you should be able to see WildFly listed as a server by navigating to New | Server from the upper menu and expanding the JBoss Community option, as shown in the following screenshot:

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 you 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 style or your company standards better. So, another valid alternative is IntelliJ IDEA, which 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 WildFly 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 WildFly by going to File | Settings and choosing the IDE Settings option. Here, you can choose to add new application server environments. This is shown in the following screenshot:

Another development option that is quite popular among developers is NetBeans (http://netbeans.org), which has support for WildFly in its releases 7.4 and 8.0, but requires installation of additional plugins available in the NetBeans plugins registry.

Installing Maven

Besides graphical tools, you are strongly encouraged to learn about Maven, the popular build and release management tool. By using Maven, you will enjoy the following things:

  • 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.1.1-bin.zip for Windows) to the directory in which you wish to install Maven 3.1.0 (or the latest available version), for example, C:\\Programs\apache-maven-3.1.1. Some operating systems such as Linux or OS X offer Maven packages in their application repositories.

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 on the command line.

Some additional Maven learning materials are available on the Sonatype website in the form of free books; refer to http://www.sonatype.com/resources/books.

Testing the installation

Once you have completed your installation, run the mvn version to verify that Maven has been correctly installed. Refer to the following code snippet to verify the correct installation:

> mvn –version
Apache Maven 3.1.1 (0728685237757ffbf44136acec0402957f723d9a; 2013-09-17 17:22:22+0200)
Maven home: C:\Programs\Dev\apache-maven-3.1.1
Java version: 1.8.0_11, vendor: Oracle Corporation
Java home: C:\Programs\Java\jdk1.8.0\jre
Default locale: en_US, platform encoding: Cp1250
OS name: "windows 8.1", version: "6.3", arch: "amd64", family: "dos"

Note

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.