As part of Tikal Fuse Day we (the Java group) decided to get our feet wet with JEE6. Instead of develop a whole application ourselves from scratch, we wanted to “learn by example” by taking an example, understand its code, build and run it on GlassFish and JBoss AS.
We searched for a full stack application, yet small, which includes JPA 2.0 , EJB 3.1 with JSF 2.0. There are quite a few samples out there, including the ones distributed with GlassFish and WELD downloads. Nevertheless, to get the whole picture, we wanted a “full stack” application with including all layers above integrated within it. The only application we found was the pet catalog application.
Its a simple application, yet covers all the three building blocks above – just what we looked for...but...
...If you read the post instruction, you will see they are oriented to Net-Beans, Ant , MySQL and GlassFish. Not our “cup of tea”. I like my application to be Maven based- decouple its build life cycle from any IDE, and I would like it be run on other application servers like JBoss...
So our goals were as follows:
“Mavenize” the project.
Run it within GlassFish
Run it within JBoss
You can “git clone” the project from Tikal-Fuse at github.
Here are the steps we did to build and run the project on GlassFish and JBoss:
1. “Mavenize” the project.
First step was writing a pom.xml file which will build and package the application to a standard JEE6 war (we'll create a JEE6 “web profile” EJB based application). . Have a look at pom.xml file. Note how simple and concise it is : There is only one dependency javaee-web-api artifact that encapsulates the whole JEE6 specs api:
<dependencies> <dependency> <groupId>javax</groupId> <artifactId>javaee-web-api</artifactId> <scope>provided</scope> <version>6.0</version> </dependency> </dependencies>
Since these api are provided by the container, there is no need to package it with our war – keep our war small without any jar in the WEB-INF/lib directory - we declare the dependency with “provided” scope on our pom.xml file. Last , we moved the sources according to Maven defaults directory structure.
You can check out the whole re-factored project from Tikal-Fuse at github. If you look at the code you will see how concise it is, and how simple is the JEE6 compare to previous specs and even to other application frameworks. For example you can see there is no needed interface for the SLSB, and the faces-config file is almost empty without navigation any rules and managed beans declared within it. The view layer uses facelets and xhtml as the view layer etc. In addition, our application is a war application rather than ear file.
All you have to do now is invoke “mvn install”. If everything goes well, you will have a “petcatalog.war” directory (we chose to use exploded war rather than a file) in the “target” directory.
2. Run it within GlassFish
After downloading GlassFish, copy our war to the “autodeploy” directory and run by invoking “asadmin start-domain” from the Glassfish “bin” directory, and make sure the war is deployed succulency by looking at GlassFish log.
Next step is to create a schema in MySQL called “petcatalog” , and copy MySQL driver to Glassfish. Then you need to define a MySQL data-source in Glassfish, and a JNDI entry, “jdbc/petcatalog”. All these is explained at the following link here.
Now we need to populate our database with data. Use “catalog/src/main/resources/catalog.sql” to create “item” table and fill some data within it. Run the following command
mysql petcatalog -uYOUR_USERNAME -pYOUR_PASSWD < 'catalog/src/main/resources/catalog.sql'
Now you can run the application and open your browser at http://localhost:8080/petcatalog.
3. Run it within JBoss
First you have to download JBoss 6. At the time of writing JBoss 6 is in development phase, and the current milestone is M2. You can download it from here.
The only change in the application is the JNDI name of our data-source. You will need to change the persistence.xml file in our petcatalog.war directory. Open “target/petcatalog.war/WEB-INF/classes/META-INF/persistence.xml” and change it as follows (only the jbdi name has been changed)
<?xml version="1.0" encoding="UTF-8"?> <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> <persistence-unit name="catalogPU" transaction-type="JTA"> <jta-data-source>jdbc/petcatalog</jta-data-source> <properties/> </persistence-unit> </persistence>
Copy your petcatalog.war directory to “server/default/deploy" directory of JBoss. As you did in GlassFish, you will need to copy the mysql driver and define a data-source to it in JBoss: Copy the MySQL drive jar to “common/lib” directory, and define the the data-source by creating the following mysql-ds.xml in “server/default/deploy" directory:
<?xml version="1.0" encoding="UTF-8"?> <datasources> <local-tx-datasource> <jndi-name>jdbc/petcatalog</jndi-name> <connection-url>jdbc:mysql://localhost:3306/petcatalog</connection-url> <driver-class>com.mysql.jdbc.Driver</driver-class> <user-name>USERNAME</user-name> <password>PASSWD</password> <metadata> <type-mapping>mySQL</type-mapping> </metadata> </local-tx-datasource> </datasources>
Run JBoss by invoing “bin/run.sh” and browse again to http://localhost:8080/petcatalog
Vuala! You have a runnable JEE6 application which includes MySQL, JPA2, EJB3.1 and JSF 2.0 , built up with Maven to a small war file, which can easily be deployed to both GlassFish and JBoss (and actually to any JEE6 compliance server) with very minor deployment change.
I hope the example and instructions above helped you kick off your own JEE6 application.
A short description of Java Fuse day can be seen here: