Developing an EJB 3 Stateless Session Bean

This tutorial covers the development of an EJB 3 Stateless Session bean in MyEclipse. 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

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 a  Java EE version of your choice; for this tutorial, select JavaEE 8 – EJB 3.2 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. .

    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.
  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.
  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 save the file.
    Adding a method to the interface
  6. Right-click the sampleejbproject package, and select New>EJB 3+ Session Bean.
  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.

    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.

    public void doSomething() {
        	System.out.println("Hello World!");

    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:

    public interface MyBeanLocal extends IMyBean {

3. Deploy the Bean

The deploying and running steps are done by using MyEclipse to deploy the bean to a Java EE compliant application server.  This tutorial specifies several steps that are unique to TomEE 8. If you wish to deploy to another application server, please look up equivalent settings.

Note: To allow EJBs to be called remotely, please make the following changes in lt;install-dir>/conf/system.properties

  • Set tomee.remote.support to true
  • Set tomee.serialization.class.blacklist to
  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.
  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, and click Finish.
    Java class details
  3. Before adding code to the client and running it, you need to add the OpenEJB client to the build path, as it implements the client side of the EJB remote invocation specification.

    Maven Project:
    Add the following dependency fragment to your pom.xml.


    Non-Maven Project: You can find the open-ejb-* JAR in the TomEE installation’s lib folder. Go to your project’s Java Build Path property page, click Add External JARs… and select this JAR to add it to the classpath.

    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.

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

    package sampleejbproject;
    import java.util.Properties;
    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.naming.NamingException;
    public class MyBeanClient {
    	public static void main(String[] args) {		
    		try {
    			Properties p = new Properties();
    			p.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.RemoteInitialContextFactory");
    			p.put(Context.PROVIDER_URL, "http://localhost:8080/tomee/ejb");
    			InitialContext ctx = new InitialContext(p);
    			IMyBean bean = (IMyBean) ctx.lookup("MyBeanRemote");
    		} catch (NamingException e) {

    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, right-click MyBeanClient.java, and select Run As>Java Application from the menu.

Result of running the test client