Developing an EJB 3 Stateless Session Bean

This tutorial covers the development of an EJB 3 Stateless Session bean. Because JPA Entities and EJB 3 Entities are so similar, developing an EJB 3 Entity Bean is not covered in this tutorial. However, in this tutorial you will learn how to:

  • Create an EJB 3 project
  • Create a stateless session bean
  • Deploy and test the bean

This feature is available in MyEclipse.

1. Create an EJB 3 Project

Begin by creating a new EJB 3 project that will include the EJB.

Note: The project created in this tutorial is available in the Resources section.

  1. Select File>New>EJB Project.
  2. Type SampleEJBProject in the Project name field.
  3. Select JavaEE 6 – EJB 3.1 as the Java EE version, select None for JPA Support, and click Finish. The remaining fields in the wizard are filled in from the MyEclipse EJB project template set up from Window>Preferences>Frameworks>EJB Project This menu option was updated in MyEclipse 2017. For prior versions, click here. .

    Note: This tutorial uses JavaEE 6 to enable it to run on TomEE 1.7.2; however, JavaEE 7 is an available option when creating new projects.

    Creating a new EJB project

You can optionally configure any data source you might use for this project to generate EJB3 Entity Beans, but as mentioned earlier, this is not part of this tutorial. See the MyEclipse JPA Tutorial to learn more.

The new project includes the default ejbModule source folder and a sampleejbproject package:

Project structure

2. Create a Stateless Session Bean

Creating a stateless session bean involves defining the bean’s interfaces and creating the session bean. When the Session Bean is generated, it implements two interfaces, one for Local calls (in the same VM) and one for Remote calls (Outside VM, over network, etc.). It is possible to have different functions exposed based on the caller (e.g. don’t expose methods to Remote invocation that return huge data sets).

For this tutorial, and in some cases, you expose exactly the same information to the Local and Remote callers of the bean. Because of this assumption, the code remains easy to follow by implementing a base interface with all the methods defined in it, that both our Local and Remote versions of the bean extend and our Session Bean implements. The result looks like the following:

Session bean flow

  1. Right-click the sampleejbproject package, and select New>Interface.

    Creating a new interface

  2. Type IMyBean in the Class name field.
  3. Click Add, type java.io. in the Choose interfaces field, select Serializable, and then click OK. This extends Serializable so the application server can better handle the Session Bean if needed.

    Extending the interface with serializable

  4. Click Finish.

    New Java interface

  5. The IMyBean interface opens automatically in the Java editor. Add a single method signature public void doSomething(); and press Ctrl+S to save.

    Adding a method to the interface

  6. Right-click the sampleejbproject package, and select New>Session Bean (EJB 3.x).
  7. Type MyBean in the Name field, select the Remote and Local checkboxes to generate local and remote interface versions, and click Finish.

    Creating the session bean with local and remote interfaces

    The Local and Remote interfaces are added to the project.

    Project structure with the session bean and interfaces

    The MyBeanLocal and MyBeanRemote interfaces need to be modified to extend IMyBean as well as add the implementation for doSomething()to MyBean. If you hadn’t defined IMyBean, you would have to copy-paste the method definitions from it to both MyBeanLocal and MyBeanRemote to expose those methods. Defining the methods in a single interface makes things a bit easier. As a reminder, you now have this structure:

  8. Double-click MyBean.java, and add the following code before the final bracket to implement the doSomething() method added to the bean interface. Press Ctrl+S to save.

    Also, each of the interfaces need to extend IMyBean. Open MyBeanLocal.java and MyBeanRemote.java and add extends IMyBean to public interface. An example of MyBeanLocal follows:

3. Deploy the Bean

The deploying and running steps are done by using MyEclipse to deploy the bean to a Java EE 6-compliant application server.  This tutorial uses TomEE.

Note: The blacklist must be disabled in the TomEE system.properties file to allow EJBs to be called remotely. Open the following file: <install-dir>/conf/system.properties and replace the “*” with a “-” for the blacklist property (tomee.serialization.class.blacklist = -).

  1. Right-click the TomEE server in the Servers view, and select Add/Remove Deployments.
  2. Select SampleEJBProject in the available column, click Add, and click Finish.

    Adding the project to the server

  3. Right-click the server, and select Start if the server is not running.

The application server starts up and displays messages to the Console view about successfully deploying the Session Bean.

Console output

4. Test the Bean

To test, you need to create a new Java test class in the package.

  1. Right-click the package, and select New>Class.

    Creating a test class

  2. Type MyBeanClient in the Name field, and select the Public static void main checkbox to tell MyEclipse to generate a main method for it. Click Finish.

    Java class details

    Before adding code to the client and running it, you need to add openejb-client-4.7.4.jar to the build path. This JAR is from the TomEE library folder and and implements the client side of the EJB remote invocation specification.
  3. Right-click the project, and select Build Path>Configure Build Path.
  4. Click the Libraries tab, and click Add External JARs.


    Adding an external JAR to the build path
  5. Navigate to the TomEE installation folder, and then the lib folder, and double-click openejb-client-4.7.4.jar.
  6. Click OK.

    External JAR added to the build path

    Now you are ready to add code to the test client and run it. The actual code, thanks to the JAR just added, is surprisingly simple.

  7. Double-click MyBeanClient.java, and replace the code with the following. Press Ctrl+S to save.

    There are some key things to notice in the code above to make sense of it:

    • The code casts the returned bean not to MyBean but to the interface MyRemoteBean because it is requesting the remote bean from the JNDI context. As mentioned above, the methods exposed by the different Local/Remote interfaces could vary, so you need to stick to the interface you are requesting.

    • TomEE uses a default JNDI name-binding for EJBs that don’t specify one. Look at the following screenshot. Notice the default name is printed out in the log. This default name varies by application server, and most people would use mappedName value of the @Stateless annotation to specify a new binding across all app servers. For example: @Stateless(name="MyBean", mappedName="ejb/MyBean").

    The server log in the Console view

    • After you have the bean, you can treat it like a local instance, and simply invoke it.

Because of how the code for the bean is written (System.out.println), the result is output to the application server Console view in MyEclipse. To see the results of the application, restart the server and then right-click MyBeanClient.java, and select Run As>Java Application from the menu.

Result of running the test client