EasyBeans Logo

Getting started with the embedded EasyBeans for Jetty application server.

Installing the EasyBeans container in Jetty 6.1.x server. (Tested on 6.1.15)

Note: The Jetty 5.x/6.0.x versions of Jetty will not work with EasyBeans due to some missing features in Jetty for EasyBeans.

JDK 5.0

To run the EasyBeans container, a JDK 5.0 or higher is required. A JDK 1.4.x will not work.

EasyBeans Web application

The EasyBeans container is provided as a .war file named ow2-easybeans-xxxx.war which allows to add or remove it in a few steps.
The .war file needs to be copied into $JETTY_HOME/webapps directory.

Note

This war file is specific to Jetty. It will detect the Jetty application server and register some libraries in Jetty application server.
Also, the java: naming of EasyBeans will be redirected to Jetty naming mechanism automatically.
By removing the war file of EasyBeans + easybeans-deploy/ folder (and examples), all is removed (No libraries or configuration files are copied).

Examples

The examples provided in this EasyBeans release need to be copied in the $JETTY_HOME/examples_ejb3s/ directory. The resulting directory will be $JETTY_HOME/examples_ejb3s with subfolders like statelessbean.

Deploying the EasyBeans container in a Jetty server.

Launching Jetty

As usual, Jetty is launched with the jetty start or jetty run command. Note that a JDK 5.0 is required

Deploying EasyBeans container

ow_easybeans.war file located in $JETTY_HOME/webapps directory will be autoloaded at the Jetty startup.

Trying examples.

Stateless Bean

Trying the example

The preview is bundled with some examples. The first one is a stateless bean

Here are the steps for launching this example:

  1. Check that JETTY_HOME environment variable is defined before launching ant scripts

    ant needs to be launched in $JETTY_HOME/examples_ejb3s/statelessbean directory. This will compile the source code and copy the archive into the $JETTY_HOME/easybeans-deploy/stateless.jar/ directory.
  2. On the server side, some traces will be printed. This is because EasyBeans has detected a new module in the $JETTY_HOME/easybeans-deploy directory. The bean was loaded automatically.
    Deploying EJB3DeployableImpl[archive=easybeans-deploy/stateless.jar]
         
  3. Then, the client can be executed by running the command ant run.client
    If the client is executed without errors, the output will be:
     run.client:
         [java] Calling helloWorld method...
         [java] Add 1 + 2...
         [java] Sum = '3'.
          
  4. There is also a servlet's client that has been put in $JETTY_HOME/webapps directory.
    It can be accessed with the default url:

Understanding the example

This example shows the use of JDK 5.0 annotations, the EJB3 interceptors and the EJB3 callbacks.

The SessionBean class is annotated with @Stateless. This means that this class will be used as a stateless session bean.

The method trace is annotated with @AroundInvoke EJB3 annotation. This method will be called at each call on a business method. The business methods are defined in the interface implemented by the SessionBean class.

As a new feature of the EJB3, the interface doesn't need to extend the Remote interface. On the client side, there is no need to do a PortableRemoteObject.narrow() call.

Customizing the example

Some annotations are commented in the session bean example. To discover some new features like interceptors in other classes or the EJB3 callbacks, the comments can be removed and the example compiled again.

New methods can be added in the interface and implemented in the bean class, then the only step to do is to call ant in $JETTY_HOME/examples_ejb3s/statelessbean directory. The EJB3 container will detect the changes and reload the bean.

Stateful Bean

Trying the example

As for the stateless session bean, the example needs to be compiled and deployed by calling ant in the $JETTY_HOME/examples_ejb3s/statefulbean directory.

Once the bean is detected by the container and loaded, the client can be launched with ant run.client command.

The output will be:

run.client:
     [java] org.objectweb.jotm.Current <init>
     [java] INFO: JOTM 2.0.11
     [java] Start a first transaction
     [java] First request on the new bean
     [java] Second request on the bean
     [java] Commit the transaction
     [java] Start a second transaction
     [java] Buy 50 amount.
     [java] Rollback the transaction
     [java] after rollback, value = 30
     [java] Request outside any transaction
     [java] Check that value = 30
     [java] ClientStateful OK. Exiting.
      

Understanding the example

The @Stateful annotation is used to mark the bean as a stateful session bean.

State of the stateful session bean is notified by the transactions (default = REQUIRED), as the bean implements the SessionSynchronization interface.

Entity Bean

Trying the example

The example needs to be compiled and deployed by calling ant in the $JETTY_HOME/examples_ejb3s/entitybean directory.

Once the bean is detected by the container and loaded, the client can be launched with ant run.client command.

Note that Hibernate traces will be displayed as it will manage the Employee entity class.

Understanding the example

This example shows the use of an entity bean and using EJB3 persistence provider which is in this release Hibernate. TopLink Essentials, OpenJPA can also be used with this EasyBeans. ObjectWeb Speedo will also provide a JPA interface.

The Entity class is a POJO class annotated with @Entity. The entities class are managed by the persistence provider, and in this case, by Hibernate.

Message Driven Bean

Trying the example

As usual, ant command needs to be launched in the $JETTY_HOME/examples_ejb3s/messagedrivenbean directory.

Once the bean is detected by the container and loaded, the client can be launched with ant run.client command.

The output on the server side will be:

      Receiving a message named '((org.objectweb.joram.client.jms.TextMessage...'. with the content 'Message0
      Receiving a message named '((org.objectweb.joram.client.jms.TextMessage...'. with the content 'Message1
      Receiving a message named '((org.objectweb.joram.client.jms.TextMessage...'. with the content 'Message2
      Receiving a message named '((org.objectweb.joram.client.jms.TextMessage...'. with the content 'Message3
      Receiving a message named '((org.objectweb.joram.client.jms.TextMessage...'. with the content 'Message4
      

Understanding the example

The @MessageDriven annotation is used to mark the bean as a message driven bean.

The client send 5 messages on the SampleQueue queue object that are received by the bean and displayed on the output.

Timer Example

Trying the example

The ant command needs to be launched in the $JETTY_HOME/examples_ejb3s/timerservice directory.

When the bean is loaded by the container, the client can be launched with the ant run.client command.

The output on the server side will be:

     [java]  SLSB -> Timer method called by the Timer Service.
     [java]  SLSB -> Timer received = '[email protected]'.
     [java]  SLSB -> Info object inside the timer object is 'Simple Serializable object'.
     [java]  SLSB -> Sending a message to a MDB which will start a timer.
     [java]  SLSB -> Message sent
     [java]  SLSB -> Call a local bean in order to start a new timer.
     [java]   MDB -> Timer method called by the Timer Service.
     [java]   MDB -> Timer received = '[email protected]'.
     [java]   MDB -> Info object inside the timer object is 'Timer started by the onMessage() method'.
     [java]  TimedBean -> Got a timer with value '[email protected]'.
      

Understanding the example

This example shows the use of the @Timeout annotation on a method. The client invokes the TimerBean that will launch a timer. This timer will send a message to an MDB and then calls another bean which implements javax.ejb.TimedObject interface.

Security Example

Trying the example

The ant command needs to be launched in the $JETTY_HOME/examples_ejb3s/security directory.

When the bean is loaded by the container, the client can be launched with the ant run.client command.

The output on the server side will be:

       someRolesAllowed() called
        -> Caller is 'Principal[EasyBeans/Anonymous]'.
       for run-as bean, caller is Caller is 'Principal[EasyBeans/Anonymous]
       onlyAdminAllowed() called
        -> Caller is 'Principal[admin]'.
       someRolesAllowed() called
        -> Caller is 'Principal[admin]'.
      

Understanding the example

All the annotations related to the security are used in this example. The annotations are @DeclareRoles, @RolesAllowed, @DenyAll and @RunAs.

To get the admin role, the @RunAs annotation is used.

As shown by the output on the server, role admin is authorized to do some actions.

Pool Example

Trying the example

The ant command needs to be launched in the $JETTY_HOME/examples_ejb3s/pool directory.

When the bean is loaded by the container, the client can be launched with the ant run.client command.

The output on the client side will be:

      [java] Calling bean's methods...
     [java] Waiting some time before checking the number of instances...
     [java] Number of instances Annotation Bean = 5
     [java]   --> This value is OK, pool is limited to 5
     [java] Number of instances XML Bean = 2
     [java]   --> This value is OK, pool is limited to 2
     [java] Sending messages with multiple threads...
     [java] Waiting some time to ensure that all messages have been sent...
     [java] Look at the server side console to check pool values of MDB ...
      

Understanding the example

The pool example is using XML or annotations to configure the pool of Stateless or MDB. The annotation used is @Pool

The configuration of the pool when using XML deployment descriptor is done in the META-INF/easybeans.xml file of the EJB-Jar file.

Migration Example

Trying the example

The ant command needs to be launched in the $JETTY_HOME/examples_ejb3s/migrationejb21 directory.

When the bean is loaded by the container, the client can be launched with the ant run.client command.

The output on the server side will be:

           [java] Calling hello() method on EJB 3.0 view of the Bean...
           [java] Calling hello() method on Remote EJB 2.1 view of the Bean...
      

Understanding the example

This example is using two interfaces for accessing the stateless session bean.

The stateless bean class EJB2And3Bean is annotated with @Remote(EJB3RemoteBusinessInterface.class) for the EJB 3.0 view. For the EJB 2.1 view, the class is annotated with @RemoteHome(EJB2RemoteHome.class). For a LocalHome, it can be annotated with @LocalHome(EJB2LocalHome.class).

EAR Example

Trying the example

This example will deploy the EJB3 included in the EAR file in EasyBeans EJB3 container while the .war file will be deployed in Jetty Application server.

The ant command needs to be launched in the $JETTY_HOME/examples_ejb3s/ear directory.

When the EAR is detected by EasyBeans, the following traces will be displayed :

AbsWebContainerDeployer.deployEAR : Deploying EARDeployableImpl[archive=/tmp/EasyBeans-Deployer-benoitf/EAR/ear3.ear]
ENCManager.getInterceptorClass : Detecting Jetty: using Jetty ENC for the naming.
JPersistenceUnitInfoHelper.loadDefaultValues : Default persistence provider set to value org.hibernate.ejb.HibernatePersistence.
...
Version.<clinit> : Hibernate Annotations 3.3.0.GA
Environment.<clinit> : Hibernate 3.2.4
...
JettyDeployer.deployWARs : The war 'WARDeployableImpl[archive=/tmp/EasyBeans-Deployer-benoitf/EAR/ear3.ear/web.war]' has been deployed on the 'web' context.
      

Then, this example is available at the http://localhost:8080/ear-web/ URL.

Understanding the example

This EAR example includes an EJB3 and a WAR file. This allows to use local interface between the WEB layer and the EJB layer.

The EAR file has no entry named META-INF/application.xml, EasyBeans will detect the type of the given archives and use default values for the name of the web context.

Due to the use of local interface, the Entities don't need to implement the Serializable interface.

The interface is not annotated with @Local annotation as it is the default value.

Each entity class provides a @NamedQuery query that allows to get all the objects

There is a relationship between Author and Book entities. It is very simple: One Author can write several books, but a Book is written only by one Author.

@OneToMany and @ManyToOne annotations are used to define the relationship


Some aspects of EasyBeans design.

Proxy

On the client side, only the EJB3 libraries + bean's remote interface + client code is needed, no needs of the JOnAS***Stub.class, etc.

Bytecode enhancement

The generated code is done by using ASM which is faster than velocity + javac methods.

Automatic class (re)loading

It detects automatically the changes in the class made by the user and reload the bean. This can be tried on the examples provided in this release : Change the source code of a class, recompile and EasyBeans will reload the bean.

Reports

Bugs or comments can be reported on or to the EasyBeans mailing-list available at EasyBeans Mailing-List

Hibernate is a product available at http://www.hibernate.org. Toplink Essentials is a product available at https://glassfish.dev.java.net/downloads/persistence/JavaPersistence.html. OpenJPA is a product available at http://openjpa.apache.org. EclipseLink is a product available at http://www.eclipse.org/eclipselink/