Thursday, October 29, 2009

OpenWebBeans Injection Service in Embeddable Apache OpenEJB

Hi;

In this post, I would like to give a simple tutorial on how to use the OpenWebBeans Dependency Injection service in an embeddable EJB container (Apache OpenEJB within Apache Tomcat 6.X).

This tutorial shows how to deploy and run sample web application that is located at "
https://svn.apache.org/repos/asf/incubator/openwebbeans/trunk/samples/ejb-telephone/
".

Sample example simulates a simple telephone contact list. It has two main functionality

1* Add new telephone contact.
Adds a new telephone contact record into the database.

2* Show telephone contacts
Shows all contacts in a table.

Configuration Steps

1* If your system does not have Maven and Java Development Kit Version 6 (JDK 6).
Install maven and java 6 sdk into your system.
Configure your classpath so "mvn" and "java" commands can be run from any location.

2* Checkout OpenWebBeans development code from SVN trunk.

1.1)Checkout source code into the folder openwebbeans.
svn co https://svn.apache.org/repos/asf/incubator/openwebbeans/trunk/ openwebbeans

1.2)Compile and install it using maven command
cd openwebbeans;
mvn clean install;

If there is no errors, all jars are nstalled into respective "target" folders of the projects.

3* Download Tomcat 6.X version, (you can download Tomcat from http://tomcat.apache.org/download-60.cgi)

4* Download and configure OpenEJB. Look at the URL http://openejb.apache.org/tomcat.html for OpenEJB installation steps.

5* Copy "atinject-api-1.0.0-incubating-SNAPSHOT.jar" from "openwebbeans/atinject-api/target" to Tomcat "/lib" folder.

6* Copy "openwebbeans-api-1.0.0-incubating-SNAPSHOT.jar" from "openwebbeans/webbeans-api/target" to Tomcat "/lib" folder.

After completing the above configuration steps successfully, our OpenEJB+Tomcat embeddable container is ready to use.


OpenWebBeans Configuration

To use the OpenWebBeans in an OpenEJB+Tomcat, it requires some configuration. OpenWebBeans has lots of configuration parameters. You can override all of them with an properties file. (You can look at https://svn.apache.org/repos/asf/incubator/openwebbeans/trunk/readme/README_M3.txt for further details).

OpenWebBeans default properties can be overriden by using a "openwebbeans.properties" file locating at the classpath of an application.

1* Create a "META-INF/openwebbeans/openwebbeans.properties" in your web application classpath.(Its location will be "java/main/resources" in a mavenized web application.)

2* Add the following [key,value] pairs into the "openwebbeans.properties" file. This enables EJB functionality and resource injection service in the web project, for example injection of @PersistenceContext, @PersistenceUnit, @Resource etc.

#use embedded openejb metadata discovery
org.apache.webbeans.spi.deployer.UseEjbMetaDataDiscoveryService=true

#use resource service
org.apache.webbeans.resource.spi.ResourceService=org.apache.webbeans.spi.ee.openejb.resource.OpenEjbResourceServiceImpl


Maven Based Web Application Configuration

Web Application Configuration

Create a web application using maven archetype. Look at the code in the "ejb-telephone" sample.

It uses "tomcat-maven-plugin" to deploy the web application into a running tomcat instance.

The web application directory structure look like this;
telephone
   src
   main
      java
      resources
       META-INF
        openwebbeans
         openwebbeans.properties --> OWB specific file
         beans.xml
         persistence.xml --> JPA specific file
     WebContent
       WEB-INF
         beans.xml
         web.xml
         faces-config.xml --> JSF Specific file
     contact.xhtml
     index.html


Compile And Deploy

Source code of the telephone application can be found at the "openwebbeans/samples/ejb-telephone".

Tomcat plugin uses http://localhost:8080/manager application to deploy war file
into your embeddable EJB Tomcat container. There must be an tomcat-users.xml
file in the "conf" directory of the Tomcat server that contains manager role and username.

Example tomcat-users.xml file

<tomcat-users>
<role rolename="manager"/>
<user username="admin" password="" roles="manager"/>
</tomcat-users>

>Start Tomcat server if not started
>cd Tomcat_Directory/bin;
>catalina.sh (or catalina.bat) run;
>mvn clean tomcat:deploy;

If everything goes well, hit the location in your favorite browser

Ejb Sample URL : http://localhost:8080/ejb-telephone.


Enjoy!

6 comments:

powerj said...

Thank you for this example. I've followed it and encountered an issue : the two field injections defined on OperationBean don't work (operation and messageUtil remain null after bean construction). I've solved it by adding the @Default annotation in addition to the @Inject one. For instance :
"private @Inject @Default Telephone operation" instead of just "private @Inject Telephone". Not sure it's a normal behavior regarding the spec.

Gurkan Erdogdu said...

Hey,

I tried again and it works! Actually it does not need to use @Default, it is automatically added to Qualifier list of injection point if there is no any other Qualifier.

jfjames said...

I want to know wether there a way to run OpenWebBeans in junit tests ? I mean outside Tomcat, just by starting OpenEJB locally and trying to call EJB directly. Currently, I'm getting a NullPointerException on the bean variable in the OpenWebBeansInterceptor.afterConstruct method. I am looking for a way to startup OpenWebBeans from a setUp or @BeforeClass junit method.

Gurkan Erdogdu said...

Hi;

Actually OpenWebBeans is booted using webbeans-impl/src/main/java/org/apache/webbeans/spi/Lifecycle.java
SPI implementations. Currently there are 3 differents implementation

1* Use in web application (default) : EnterpriseLifecycle.java

2* Use in standalone Java SE projects :
StandaloneLifeCycle.java

3* Use in junit tests
OpenWebBeansTestLifecycle

You could create an OpenEJB lifecycle implementation and patch our codebase:) You are always welcome to join us.

In this implementation, you could boot up OpenEJB in applicationStarted and destroy it in applicationEnded methods.

You could look at above implementations for getting some help.

Please drop at dev@ for any questions.

Thanks;

--Gurkan

vitamine b12 said...

This blog post shows how to use the Apache OpenWebBeans within embeddable ejb container, Apache OpenEJB running in the Apache Tomcat. In this post, I would like to give a simple tutorial on how to use the OpenWebBeans Dependency Injection service in an embeddable EJB container.

dorla said...

Hi! Your blog is simply super. you have create a differentiate. more templates easy to download Thanks for the sharing this website. it is very useful professional knowledge.