TomEE: Tomcat with Java EE Extensions

Apache TomEE can be downloaded in various ways, including as a standalone Java EE application server or as a bundled WAR file that can be published with Tomcat7. TomEE adds the OpenEJB container to Tomcat7 but also includes, for example, the Apache CXF implementation of the JAX-RS API and a JMS provider. TomEE, like Tomcat, can be installed anywhere on the filesystem, and the TomEE directory structure is almost identical to that of Tomcat7. TomEE is designed with simplicity in mind. The Java world is awash with options and, with respect to JASes, TomEE is one worth considering.

The two .java files in the predictionsEJB service, Prediction.java (see Example 7-6) and PredictionEJB.java (see Example 7-8), are completely unchanged in the port from GlassFish to TomEE. The configuration files, however, are different. The TomEE configuration files are the persistence.xml file (see Example 7-10), the same name as in GlassFish, and the openejb.xml file (see Example 7-11).

In the persistence.xml document, lines 2 and 3 ensure that the OpenJPA provider automatically generates whatever database tables are required to persist Prediction instances. The configuration document also names the @Entity class Prediction (line 1) whose instances are mapped to table rows.

The second configuration document, openejb.xml, names the DataSource (line 1); specifies the JDBC driver (line 2), in this case the driver for the HSQLDB database, a convenient RDBMS with, by default, in-memory tables; and ends with the JNDI name (line 3) of the database system. The HSQLDB system comes with TomEE. TomEE documentation covers sample Resource configuration for various other popular database systems such as Derby, MySQL, and PostgreSQL. The two configuration files shown here must occur in the META-INF subdirectory of the deployed JAR file. The deployment options and details are laid out in the next section.

TomEE, like GlassFish, provides a JAR file with the required Java EE packages such as javax.persistence. All of the TomEE libraries come as JAR files and most of these are in the TomEE_HOME/lib directory, which contains more than 100 JAR files, including the files for the Apache CXF implementation of JAX-RS. There is also the directory TomEE_HOME/endorsed, which holds a few additional JAR files. The TomEE JAR for the core Java EE packages is named javaee-api-N-tomcat.jar, with a version number where the N occurs. This JAR, roughly the TomEE counterpart of the GlassFish file javaee.jar, can be used for compilation.

TomEE comes with the familiar TomEE_HOME/webapps subdirectory already in place. The subdirectory TomEE_HOME/apps is not present but can be created manually, and the TomEE_HOME/bin/tomee.sh command has deploy and undeploy options that can be used to deploy EAR files whose contents are one or more JAR files, each with its own EJB. The deploy and undeploy options target the apps subdirectory, which TomEE supports for backwards compatibility with versions of Java EE earlier than 6. Under Java EE 6, the same Java class loader can load the contents of servlets and EJBs; hence, the Java EE 6 specification enables EJB deployment in a WAR file. This style of deployment is preferred practice in TomEE. The deployment details for the predictionsEJB service under TomEE can be summarized as follows:

The TomEE endpoint URL for an EJB service differs slightly from the GlassFish endpoint. In particular, the TomEE URI in this example is /pred/webservices/PredictionEJB: the URI begins with the slash and the name of the WAR file (/pred), the term webservices is appended (/pred/webservices), and then the name of the @WebService class is appended to yield /pred/webservices/PredictionEJB. The very same ClientEJB used for the GlassFish deployment of predictionsEJB (see Example 7-9) can be used, once recompiled against the wsimport-generated artifacts to get the correct endpoint URL, against the TomEE deployment. It is a nice TomEE touch to support conventional WAR deployment of even EJBs, including EJB-based web services.