Arch2Arch Tab BEA.com
Syndicate this blog (XML)

Using Workshop 10 for EJB 3.0 Development

Bookmark Blog Post

del.icio.us del.icio.us
Digg Digg
DZone DZone
Furl Furl
Reddit Reddit

Greg Mally's Blog | November 1, 2007   2:47 PM | Comments (9)


Introduction

Workshop for WebLogic Platform 10.0, 10.1, and 10.2 rely on Web Tools Platform (WTP) 1.5 for Web and Java EE development. This reliance introduces some challenges when trying to do Java Enterprise Edition 5 (JEE5/EE5) development since EE5 is not officially supported by WTP until the 2.0 release. However, there are ways to "trick" WTP 1.5 into behaving nicely when doing certain types of EE5 development. One area where WTP 1.5 and EE5 can coexist nicely is in the area of EJB 3.0.

This write-up is intended to demonstrate how to configure an EJB 2.1 project, which was created using the WTP 1.5 project creation wizard, for EJB 3.0 development.

Assumptions

Workshop for WebLogic version 10.0 or 10.1 is installed and you have a basic understanding of Workshop 10.x, the EJB 2.1 specification, and the EJB 3.0 specification.

Create a WTP EJB 2.1 Project

1.       File > New > Project...

To access the WTP EJB 2.1 New Project wizard, be sure to select the Show All Wizards option in the New Project window:

2.       Select the EJB Project to start the New EJB Project wizard.

3.       Create the EJB Project with the appropriate values and facet configuration in the New EJB Project wizard.

All that is needed on page 1 of the wizard is the Project name:

Remove the WebLogic EJB Extensions facet on page 2 of the wizard because this facet is specific to WebLogic EJB 2.1 development:

At this point you can simply Finish creating the EJB Project.

Configure the EJB 2.1 Project for EJB 3.0 Support

1.       Update the ejb-jar.xml file that was created by the wizard with the EJB 3.0 deployment descriptor Schema reference.

The ejb-jar.xml file that was created by the wizard is configured for EJB 2.1. Although the EJB 3.0 specification states that the deployment descriptor is not necessary, WTP 1.5 requires that an ejb-jar.xml be present in the project. To keep WTP 1.5 from complaining about a missing ejb-jar.xml, we can simply add an empty EJB 3.0 ejb-jar.xml. The descriptor should be located in the ejbModule/META-INF directory of the project:

The contents of the ejb-jar.xml should look something like the following:

<ejb-jar xmlns="http://java.sun.com/xml/ns/javaee"

         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

         http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd"

         version="3.0">

</ejb-jar>

2.       Your project is now ready for EJB 3.0 Session & MDB development.

At this point, WTP is fine because it sees the deployment descriptor it is expecting. You can now use all of the EJB 3.0 annotations for creating your new EJBs.

Create a Simple EJB 3.0 Stateless Session Bean

1.       Create Interface Class.

Create a new Interface class called EchoService in the ejb3.beans package with a method definition called echo that accepts and returns a String. Your new interface should look something like the following:

package ejb3.beans;

 

public interface EchoService

{

    String echo(String echoValue);

}

2.       Create Class that Implements Interface Class.

Create a new class called EchoServiceBean in the ejb3.beans package that extends EchoService and Serializable. Your new class should look something like the following:

package ejb3.beans;

 

import java.io.Serializable;

 

public class EchoServiceBean implements EchoService, Serializable

{

 

    public String echo(String echoValue)

    {

        // TODO Auto-generated method stub

        return null;

    }

 

}

3.       Add EJB Annotation to EJB 3.0 Bean class.

Add the @Stateless annotation with the name attribute of echoService to the EchoServiceBean class. Your EJB 3.0 Bean should look something like the following:

package ejb3.beans;

 

import java.io.Serializable;

 

import javax.ejb.Stateless;

 

@Stateless(name="echoService")

public class EchoServiceBean implements EchoService, Serializable

{

 

    public String echo(String echoValue)

    {

        // TODO Auto-generated method stub

        return null;

    }

 

}

4.       Add code to the body of the echo method that will echo the input parameter value.

Here's an example of what can be returned by the echo method:

    public String echo(String echoValue)

    {

        return "Echo from - " + this.getClass().getName() + ": echoValue = " + echoValue;

    }

Create Dynamic Web Project with EE5 Support

1.       File > New > Project...

We are going to create a Dynamic Web Project called EJB3Servlet and a new EAR Application Project called EJB3ServletEAR. (Note: the EAR can be created on the first page of the New Dynamic Web Project wizard). Other than the Project name and EAR Project Name, you can use the defaults.

Once you have done this, we will configure the Dynamic Web Project for EE5 support. We will bundle the Web and EJB projects in the EJB3ServletEAR to test out our simple EJB 3.0 Bean.

2.       Update the web.xml with the Servlet 2.5 deployment descriptor Schema reference.

Much like was done for the EJB Project, we are going to tweak the web.xml for Servlet 2.5. This will tell WLS that resource injection can occur, which is necessary for accessing the new EJB via EJB 3.0 annotations in Servlets. The tweak involves updating the descriptor with the Servlet 2.5 schema reference:

<web-app id="WebApp_ID"

         version="2.5"

         xmlns="http://java.sun.com/xml/ns/javaee"

         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

         http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

3.       Add a Module Dependency for the EAR Project to the EJB 3.0 Project

Make sure that the EAR project that was created in step 1 has the EJB 3.0 project as a Module Dependency. The project properties for the EAR should look something like the following:

Test the Simple EJB 3.0 Stateless Session Bean

1.       Add Servlet to the Dynamic Web Project

Using the Create Servlet wizard (e.g., File > New > Other..., then Web/Servlet), specify a Class name of InvokeEJB. The wizard should look something like the following:

2.       Annotate Servlet for EJB 3.0 Reference

Add the @EJB annotation to the new Servlet above a variable declaration to the EchoService Bean. The code should look like the following:

    @EJB()

    private EchoService echoService;

At deployment time, WebLogic Server will inject the EJB 3.0 reference into the local variable which will be used in the doGet method.

Best Practices/Performance Consideration: If you define your variable declaration as private or protected, then you should also define a setter and a getter for the variable because WLS will do setter injection vs. field injection. Setter injection performs better than field injection when using encapsulation good practices. Here's an example:

    @EJB()

    private EchoService echoService;

    public void setEchoService(EchoService value)

    { this.echoService = value; }

    public EchoService getEchoService()

    { return this.echoService; }

3.       Invoke the EJB 3.0 Bean in the doGet Servlet method using the following code:

ServletOutputStream outputStream = response.getOutputStream();

 

if(echoService != null)

{

    outputStream.println("Start EJB Invoke");

    outputStream.println("<br><br>");

    outputStream.println(echoService.echo("Servlet Request"));

    outputStream.println("<br><br>");

    outputStream.println("Invoke Complete");

}

else

{

    outputStream.println("ERROR. EJB Reference is null");

}

4.       Run on Server to Invoke Servlet via Run As > Run on Server

5.       Review Output

Assuming everything is configured correctly, the WebLogic Server will have done the EJB 3.0 injection into the Servlet and the results will look like the following when the Servlet is run:


Comments

Comments are listed in date ascending order (oldest first) | Post Comment

  • I reviewed this for Greg, and just wanted to mention that what he has put together here is for more than just EJB 3.0 development. Essentially, by updating the deployment descriptor schemas for components developed by Workshop to servlet 2.5 or EJB 3.0, the annotations and all the features of WLS 10 associated with annotations are available to you. This includes dependency injection (not just in EJB 3.0), WL-specific annotations, and other JEE5 features. Good stuff Greg! I know I will use it!

    Posted by: mlindros on November 1, 2007 at 3:10 PM

  • Greg, thanks for writing this up. Several of my customers are interested in this functionality. I tried this out myself and two minor potential snags came up that I wanted to make note of in case others had them too. 1st is that if you copy/paste the ejb-jar.xml and web.xml snippets as you have them above, make sure that you keep the attribute value for xsi:schemaLocation all on one line. If I remember correctly, Workshop prompted me to download the schema. Workshop will still give you a warning that: The value of the attribute "version" should be one of: 2.4, but you can ignore that.

    <web-app id="WebApp_ID"
             version="2.5"
             xmlns="http://java.sun.com/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    

    Secondly, when you add the code in the servlet for the dependency injection for the EJB reference, you cannot control-space import by placing your cursor after the parenthesis in the @EJB() annotation, but it does work if you try it after the B in EJB. Basically, just don't forget to add the import as follows, however you do it:

    import javax.ejb.EJB;
    
    Would it be that difficult to add a custom facet for this for Dynamic Web and EJB projects?

    Posted by: jbayer on November 5, 2007 at 7:23 AM

  • First of all, I want to thank you for posting your experiences regarding this blog. It's this type of feedback that really helps everyone who is interested in using this approach :-D

    Now for my response: The intent of this tutorial is to provide a stopgap until Workshop adopts Eclipse 3.3 and WTP 2.0. WTP 2.0 will have the appropriate facets to support what we have done in this tutorial plus it will allow the deployment descriptor for the EJB 3.0 project to be optional.

    So providing a custom facet for this is possible, but my take on that is this: The steps involved in setting up an EJB 2.1 project for EJB 3.0 is not that difficult (plus it's a 1-time thing per project). The custom facets would be providing duplicate functionality that you get with WTP 2.0. IMO, going through the manual setup makes it clear that we are "tricking" WTP 1.5 to work with EJB 3.0 whereas having a facet would mask this trickery and potentially be a point of confusion for folks.

    Posted by: gmally on November 5, 2007 at 8:08 AM

  • You guys might also be interested in checking out WebLogic Server Tools, which already supports Eclipse 3.3 / WTP 2.0 (including various Java EE 5 scenarios) and has many of the same base WLS support features as Workshop.

    https://dev2devclub.bea.com/updates/eclipse-3.3/wls-plugins/

    Posted by: kosta on November 8, 2007 at 9:36 AM

  • Greg, thanks for writing this up. Your I tryed to deploy an Entity but javax.persistence.Entity is not reconnised where are the jars I need to ?

    Posted by: nbulteau on November 13, 2007 at 1:28 AM

  • Nuts. This doesn't work - I get the error "ERROR. EJB Reference is null ". I'm using WebLogic Server 10 MP1 and Workshop 10.1. The EAR and the EJB appear to be deployed on the server (I created a domain for WLW 10.1 to test this.) I get output from the doGet() method showing that it is being called. However, I never get output from the setEchoService() method. It looks like it's not being called. Any ideas ?

    Posted by: warelock on December 10, 2007 at 12:17 PM

  • Never mind. If Iuse jbayer's version of the web.xml schema it works fine. Thanks for the article !

    Posted by: warelock on December 10, 2007 at 12:32 PM

  • Hi, i have done all, when i try to run servlet i am getting this error: Module named 'EJB3ServletEAR' failed to deploy. java.net.MalformedURLException: no protocol: files/Workshop/tools/eclipse_pkgs/1.0/pkgs/eclipse/plugins/com.bea.workshop.thirdparty.suntools_1.0.1/tools.jar any idea? Thanks.

    Posted by: kkurt@mu.edu.tr on December 11, 2007 at 8:14 AM

  • Hi, I followed your post, and we have a working EJB, but I've noticed an issue. If you switch the ejb-jar.xml from EJB2.1 to 3.0 schema one can no longer export an ant script for the EJB project. Has anyone else ran into this and found a work-around?

    Posted by: parrots on March 3, 2008 at 12:41 PM



Only logged in users may post comments. Login Here.

Powered by
Movable Type 3.31