Devoxx 2011 kicks off with a very impressive demo-based talk on jee6. In a time span of approx. 4 hrs Paul Bakker and Bert Ertman hack together a small enterprise CRUD application showing a lot of the new features present in jee6. And foremost demonstrating how easy it is to quickly set up such an app in jee6. Granted, they use a couple of additional tools to accomplish this feat, but nonetheless, the end result is a plain jee6 web app, easily portable across different application servers and adhering to the jee6 specs.
The application
The application being built, is a Duct Tape Store. It’s a simple online web store. A user can log in, browse through the store inventory, put items in a shopping basket and eventually purchase the items. The application is build in an agile manner. Bert tells Paul which features he wants, Paul builds them on the spot, then Bert adds additional feature requirements, which Paul again adds to the application, and so forth.
One of the key aspects of the application is that it’s build according to a layered design. A higher layer can only call code that’s in the layer directly below, and a layer can never call code in a higher layer. Delegation to a lower layer is achieved via Dependency Injection. There are basically 5 layers in the demo application:
- Presentation – implemented by JSF & facelets
- Navigation – implemented by Named Beans
- Business Logic – implemented by Enterprise Java Beans
- Data Access – implemented by JPA (this could also be a JAX-WS layer or a hybrid)
- Data – MySQL (again as an alternative for a relation database, this could be a NoSQL database or even MQ)
For fun, they throw in some AJAX, JMS and REST as well.
Build tools
Their build kit consists of the following tools:
- IntelliJ – IDE
- JBoss Forge – framework for rapid application jee6 development
- Maven – build tool
- Glassfish 3.1 – jee6 application server
- MySQL – database
- Arquillian – framework for integration testing
The demo
Let’s walk through some of the highlights of the demo. The complete source code is available on GitHub. A link to the code is available in the References section.
Creating the basics
Paul uses Jboss Forge to setup the skeleton of a new jee project:
new-project --named ducttape --topLevelPackage ducttape
This creates a basic Maven project with a minimalistic pom file.
After this Paul again uses Forge to add the necessary JSF and CDI dependencies and files (web.xml, faces-config.xml, beans.xml).
He then adds a prebuilt template.xhtml file which will be the template file from which all other facelets will inherit and adds some resources (css, images) for styling.
Again with Forge persistence is setup using Hibernate as the JPA Provider.
persistence setup --provider HIBERNATE --container CUSTOM_JTA
Filling in the layers
Using Forge an Entity Product is created in the data access layer, and using Hibernate this Entity is used to automatically create a database table PRODUCT in the mysql database:
@Entity public class Product
In the Business Logic Layer an EJB named ProductManager is created for interacting with the data layer and doing CRUD operations on the PRODUCT table. For this an EntityManager is injected into the EJB to which the data access operations are delegated.
@Stateless
public class ProductManager {
@PersistenceContext
EntityManager em;
In the Navigation layer the ProductsBean, from where the UI will get his Product information, is created. It gets the following annotations:
@RequestScoped
@Named
public class ProductsBean
@Inject ProductManager productManager;
The Named annotation enables the bean to be used via EL expression in the facelet pages. The ProductManager bean gets injected into the ProductsBean so the ProductBean can delegate Data Access tasks to the Business Logic layer:
Finally in the UI layer the ProductsBean is being injected using EL expressions:
<h:dataTable value="#{productsBean.products}" var="product" id="products">
<h:column>
<f:facet name="header">Name</f:facet>
#{product.name}
</h:column>
</h:dataTable>
In the above example the getProducts method is called on the ProductsBean, which spills out a List of Products.
Extending the basics
After the basics some more advanced features are added to the application:
- The Criteria API is used for building some type-safe queries in the beans in the Business Logic Layer;
- A TestDataInserter class is created (using @Singleton and @Startup annotations) with a @PostConstruct method that inserts the testdata;
- A JUnit test class is created with a @RunWith(Arquillian.class) annotation to make use of Arquillian for doing some integration testing;
- Sorting on displayed colums is implemented using a request parameter, which also makes the JSF sorted page bookmarkable;
- Ajax is added on a search text field to do some filtering on the product list (<f:ajax event=”keyup” render=”products”/>);
- A @ConversationScoped ShoppingBasket class is added. The ShoppingBasket bean will be alive as long as the Conversation it belongs to, is alive. The lifecycle of this Conversation is handled programmatically via an @Injected Conversation class;
- Bean Validation is added to the Customer entity. Hibernate Validator is used for this, which adds some extra validation Annotations, like @NotEmpy and @Email;
- The use of Events is demonstrated. In the Producer class an Event class of a specific type (Generic) is injected, which can then be fired in one of the methods. The Consumer classes use the @Observes annotation on the method that can handle the typed Event .
- The Injected event as well as the Method that consumes it can be annotated with a qualifier annotation, so events of the same type can be handled selectively.
- A method can handle an event asynchronously by annotating it with @Asynchronous;
- Using JAX-RS annotation a class is turned into a Rest Service, which can return a List of Product in XML and/or JSON.
For more information regarding the implementations, check out the source code in the References section.
Conclusion
All in all this was a very impressive demo showing a lot of the powerful features of jee6. With the help of the new CDI annotations it’s very easy to setup a layered application. Also worthy of note are Arquillian and Forge. Theey are definitely productivity and quality boosters and i highly recommend in every jee6 project.
References


Whitehorses is specialized in succesfully implementing Oracle SOA solutions: BPEL, OSB, WebLogic & BPM