EasyBeans Logo

Getting started with the embedded EasyBeans for JOnAS application server.

Installing the EasyBeans container in a JOnAS 4.x server. (Tested on a 4.7.8, 4.8.7, 4.9.2 and 4.10.3 JOnAS releases)

JDK 5.0

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

Resource Adapter

The EasyBeans container is provided as a .rar file named ow_easybeans_for_jonas.rar which allows to add or remove it in a few steps.
The .rar file needs to be copied into $JONAS_BASE/rars or $JONAS_BASE/rars/autoload directory. By copying the file in $JONAS_BASE/rars/autoload, this allows to start the EasyBeans container when JOnAS is starting.
Note : $JONAS_BASE could be replaced by $JONAS_ROOT if $JONAS_BASE is not defined.

DataSource/JDBC

A datasource with the default name jdbc_1 needs to be present, else the entity bean example will not work. Note that a fresh install of JOnAS is providing this datasource by default.

Examples

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

Deploying the EasyBeans container in a JOnAS server.

Launching JOnAS

As usual, JOnAS is launched with the jonas start command. Note that a JDK 5.0 is required

Deploying EasyBeans container

If ow_easybeans_for_jonas.rar is in $JONAS_BASE/rars/autoload directory, no configuration is needed, the EasyBeans container has been started when JOnAS was started.

If ow_easybeans_for_jonas.rar is in $JONAS_BASE/rars directory, this module needs to be deployed by using either the JOnAS Admin console (http://localhost:9000/jonasAdmin) or the command line. In this case, the command is the following : jonas admin -a ow_easybeans_for_jonas.rar.

Trying examples.

Stateless Bean

Trying the example

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

Here are the steps for launching this example :

  1. ant needs to be launched in $JONAS_ROOT/examples/ejb3s/statelessbean directory. This will compile the source code and copy the archive into the $JONAS_BASE/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 $JONAS_BASE/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'.
          

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 $JONAS_ROOT/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 $JONAS_ROOT/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 $JONAS_ROOT/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 $JONAS_ROOT/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 $JONAS_ROOT/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 = 'org.ow2.easybeans.component.quartz.EasyBeansTimer@6e7d3050'.
     [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 = 'org.ow2.easybeans.component.quartz.EasyBeansTimer@59d794d'.
     [java]   MDB -> Info object inside the timer object is 'Timer started by the onMessage() method'.
     [java]  TimedBean -> Got a timer with value 'org.ow2.easybeans.component.quartz.EasyBeansTimer@2dd5b883'.
      

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 $JONAS_ROOT/examples/ejb3s/security directory.

The bean will be loaded and then 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 $JONAS_ROOT/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 $JONAS_ROOT/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 JOnAS Application server.

The ant command needs to be launched in the $JONAS_ROOT/examples/ejb3s/ear directory.

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

JOnASDeployer.deployEAR : Deploying EARDeployableImpl[archive=/tmp/EasyBeans-Deployer-benoitf/EAR/ear3.ear]
ENCManager.getInterceptorClass : Detecting JOnAS: using JOnAS 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
...
JContainer3.start : Container started in : 5619 ms
AbsJWebContainerServiceImpl.registerWar : War /tmp/EasyBeans-Deployer-benoitf/EAR/ear3.ear/ear-web.war available at the context /ear-web.
JOnASDeployer.deployEAR : 'EARDeployableImpl[archive=/tmp/EasyBeans-Deployer-benoitf/EAR/ear3.ear]' EAR Deployable is now deployed
      

Then, this example is available at the http://localhost:9000/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 JIRA Issue web site 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/

Valid XHTML 1.1