Developing EJBs

From
Revision as of 21:01, 22 November 2004 by JanHegewald (talk | contribs)
Jump to navigation Jump to search

Developing EJBs

An introduction to the development cycle of Enterprise Java Beans

Jan Hegewald

The original presentation can be downloaded here. File:Developing EJBs.pdf


Abstract

As with every software development process, one has to consider many aspects of the development cycle before starting. The following text is intended to give an overview of the aspects to consider especially when developing Enterprise Java Beans.

For setting up the software development environment, firstly a J2EE-Server has to be choosen. The second thing to find is an appropriate IDE which simplifies the development process as much as possible and allows the programmer to focus on implementing business logic instead of writing and modifying interfaces and deployment descriptors over and over. In the best case this IDE also integrates the packaging process. To further simplify the development other tools should be evaluated. This text aims to present an overview of available tools regarding each of these points. Finally, a sample application is developed to demonstrate how much work an IDE can do for the programmer.

Choosing a J2EE-Server

Today there are many J2EE-Servers available, both commercial and free products. A collection of the most common can be found below:

Commercial Products

Free / Open Source Products

The Sun Java System Application Server is important, because since it comes from Sun, it is the reference implementation of J2EE. Furthermore, at present it is the only available server supporting JSF. Despite this it is not Open Source Software. It can be bought from Sun or used freely in the Platform Edition.

In the sector of Open Source Software, JBoss is the most important and most popular server. The only disadvantage is the poor availability of free documentation. If this is an important issue, JOnAS is a very good alternative, since it is very well documented and many tutorials and sample applications are available.

When evaluating commercial servers, a key factor will be the amount of money one is willing to invest.

A very good summary of available servers of both categories can be found at http://www.theserverside.com/reviews/matrix.tss.

IDEs for the development of EJBs

Since the development of EJBs normally includes a lot of repetitive tasks, it is very important to use an IDE which takes care of those issues on behalf of the programmer. This is an incomplete list of available products:

Commercial Products

Free / Open Source Products

When using a commmercial server, the decision is easy in most cases. Since nearly every company which sells an application server also offers an own IDE, one should use this one, since it then integrates perfectly with the corresponding server.

In other cases it depends on the available money to spend. If you have some, IntelliJ IDEA is the tool of choice, since you get an excellent tool for every kind of Java development.

If you want to get something for free, situation is a little worse. NetBeans currently (version 3.6) does not support development of EJBs. Support is announced to be available in version 4.1 which is far ahead, but you have to keep in mind that Sun pushes NetBeans against Eclipse, so there will most probably be some support then.

Eclipse plugins

Eclipse itself does not support EJB development either, but there are a some plugins available:

JBoss IDE offers most features of those plugins and though its name suggest adaption to only JBoss AS, it also supports a variety of other servers.

Oracle JDeveloper

Available at: http://www.oracle.com/technology/products/jdev/index.html

Unlike other application server-vendors, Oracle gives away its IDE for free. Any way, it has a lot of cool features:

  • 2-way UML designer (for DB-Schemas, page-flow, use-cases...)
  • superior integration of the database schemas into the development process
  • rich visual editing (nearly everything can be done programmatically or by drag 'n drop: JSP-design, DB-schema-design, bean-design...)
  • professional profiler
  • TopLink integration

So even if you do not use Oracle AS or Oracle DB at all you should take a look at JDeveloper.

Deployment and Packaging

Deployment Descriptor

Every package of beans needs a Deployment Descriptor which is stored in a file called ejb-jar.xml located in the directory META-INF. It consist of two parts:

  • bean configuration (<enterprise-beans>): In this section beans are configured, e.g. whether they are stateless or stateful sessicn beans, which classes form their interfaces, whether they use container or bean managed persistence and so on. This is an example:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd">
<ejb-jar >
  <enterprise-beans>
    <session >
        <description><![CDATA[Session bean that implements business processes]]></description>
        <display-name>BookShop Bean</display-name>
        <ejb-name>BookShop</ejb-name>
        <home>example.interfaces.BookShopHome</home>
        <remote>example.interfaces.BookShop</remote>
        <ejb-class>example.ejb.BookShopBean</ejb-class>
        <session-type>Stateless</session-type>
        <transaction-type>Container</transaction-type>
        <ejb-ref >
           <ejb-ref-name>ejb/Book</ejb-ref-name>
           <ejb-ref-type>Entity</ejb-ref-type>
           <home>example.interfaces.BookHome</home>
           <remote>example.interfaces.Book</remote>
           <ejb-link>Book</ejb-link>
        </ejb-ref>
     </session>
 </enterprise-beans>
  • application assembly information (<assembly-descriptor>): The application assembly information is used to put the beans together to form an application. Mainly services may be configured in this section, such as security, transactions and so on. Example:
 <assembly-descriptor>    
    <security-role>
         <role-name>jaas</role-name>
    </security-role>
    <method-permission>
         <role-name>jaas</role-name>
               <method>
                     <ejb-name>JAASOp</ejb-name>
                     <method-name>*</method-name>
               </method>
    </method-permission>
    <container-transaction>
         <method>
               <ejb-name>JAASOp</ejb-name>
               <method-name>*</method-name>
         </method>
    </container-transaction>
 </assembly-descriptor>
</ejb-jar>

Note: With XDoclet you can avoid writing deployment descriptors again and again.

Packaging

As with normal Java applications, the compiled sources have to packaged. In J2EE there are three main types of archives:

  • JARs: Java-Archive - contains libraries, clients, beans, …
  • WARs: Web-Archive - contains libraries, servlets, JSPs, …
  • EARs: Enterprise-Archive - contains J2EE components (beans + clients, e.g. jars and wars)

Useful tools

Some tools can simplify the work significantly. Here is a list of some of them:

  • XDoclet: Very important! This tool can be used to generate deployment descriptors as well as all the necessary interface classes for every bean automatically! Available at: http://xdoclet.sourceforge.net/ (A good IDE already comes with this tool.)
  • Sun J2EE Application Verification Kit: This application can be used to verify your application regarding conformance to J2EE specification. It can only be used together with the Sun Java System Application Server. Available at: http://java.sun.com/j2ee/verified/avk_enterprise.html
  • UML2EJB: This tool takes an UML-model from a case tool and generates EJBs with the appropriate XDoclet-Tags (model driven development). It is no longer developed, but possibly interesting anyway: http://sourceforge.net/projects/uml2ejb/ Note: If you use JDeveloper you do not need it, since JDeveloper does all this for you.


Sample application

The sample application consist of three beans, one of each type: one entity bean BookBean, one session bean BookShopBean and one message-driven bean BookWatcherBean. Some JSP-pages serve as the beans client.

The application was developed in the following environment:

  • Eclipse 3.0 with JBoss IDE with XDoclet
  • JBoss AS 4.0
  • MySQL 4.1 - although I used MySQL you should be able to run the application with any database without having to configure anything.