MyEclipse EJB 3.x Tutorial

Table of Contents

  1. Introduction
  2. Suggested Audience
  3. System Requirements
  4. Getting Started
  5. Creating a Stateless Session Bean
  6. Testing the Bean
  7. Conclusion
  8. Resources

1. Introduction

Welcome to the MyEclipse EJB 3 Tutorial. In this tutorial we are going to cover the development of an EJB 3 Stateless Session bean. It is important to note that because JPA Entities and EJB 3 Entities are so similar, developing an EJB 3 Entity Bean will not be covered in this tutorial and we would encourage you to read through the MyEclipse JPA Tutorial to see how that process works.

It is also important to be aware of this tutorial's scope, because during the creation of an EJB Project, you are asked to specify datasource information in case you decide to generate EJB3 Entities. We skip that portion in this tutorial because we are simply developing a Stateless Session Bean.

The project created in this tutorial is available in the Resources section for folks that would like to jump ahead.

2. Suggested Audience

This tutorial is intended for developers who are somewhat familiar with MyEclipse or Eclipse so you recognize navigation within the IDE, and understand some of the more common views like the debugger. Familiarity with EJBs in general and EJB3 are helpful, but not necessary. The concepts covered are fairly straight forward to Java developers.

To learn more information about the topics presented in this tutorial please have a look at the links in our Resources section. To get a better feel for MyEclipse and learning more about it, please check out our product Documentation for more material.

3. System Requirements

This tutorial was created with MyEclipse 5.5. If you are using a another version of MyEclipse, most of these screens and instructions should still be very similar.

If you are using a newer version of MyEclipse and notice portions of this tutorial looking different than the screens you are seeing, please let us know and we will make sure to resolve any inconsistencies.

4. Getting Started

The first thing we need to do is create a new EJB Project so we can create our EJB in it. First click on File > New > Project:

Then select the EJB Project and hit Next:

On this screen be sure to fill out the project's name and select the Java EE 5.0 specification level (which includes the EJB 3.0 spec):

On this next screen we can optionally configure any datasource we plan on using for this project to generate EJB3 Entity Beans, but as mentioned above, we are not going to show that in this tutorial (Hint: Please read the MyEclipse JPA Tutorial to get an idea of how working with entities is done). So we disable both these sections and Finish creating the project:

Our new project will look very simple, like this:

5. Creating a Stateless Session Bean

Now that our project is ready we should create a package to put our bean in:

And after the package is created, you will have a source folder that looks like the following:

Now, before we create our Session Bean, let's discuss how we can define our bean's functionality.

When the Session Bean is generated, it will implement two interfaces, 1 for Local calls (in the same VM) and 1 for Remote calls (Outside VM, over network, etc.). It is possible to have different functionality exposed based on the caller (e.g. don't expose methods to Remote invocation that return huge data sets). But for this tutorial, and in some cases, you will expose exactly the same information to your Local and Remote callers of your bean. Because of this assumption, we can keep our code easy to follow by implementing a base interface with all our methods defined in it, that both our Local and Remote versions of the bean extend and our Session Bean implements. The result ends up looking like this:

So let's create that base interface now:

When we create the new interface, we want it to extends Serializable so the application server can better handle the Session Bean if it need to:

And when the interface is created, we add a single method signature to it:

Now that our interface is ready, let's create our new EJB3 Session Bean:

On this next screen we want to name our bean, but also want to be sure to tell MyEclipse to generate Local and Remote interfaces for it:

Now that our bean is generated with the Local and Remote interfaces as well, we end up with this:

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

The one last thing for us to do is add a simple implementation for the doSomething() method. For the purpose of this tutorial, a typical "Hello World" will suffice:

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

Simple Hello World implementation for doSomething() method

6. Testing our Bean

Now that our bean is written, we need to deploy, run and test it. The deploying and running step is done by using MyEclipse to deploy the bean to a Java EE 5.0 compliant application server, for this tutorial we are using Glassfish 2. After our bean is deployed, we will write a simple Java class that will load our EJB3 using JNDI, and call it's doSomething() method.

Deploying the Bean

First, assuming you have Glassfish 2 setup, you can quickly deploy the project by using the MyEclipse Deployment Tool:

Then make sure the project is selected, and click Add to add a new deployment for the project to Glassfish 2:

On the new deployment dialog, make sure to select the app server you want to deploy the bean to (must be Java EE 5.0 compliant for the purposes of this tutorial) and hit Finish:

Confirm that the deployment was successful and hit OK:

Now that our EJB is deployed, we can start the application server up before writing our simple test case. First click the Application Server launch drop down to select the app server to start:

Then the application server will start up, and likely print messages to the console about successfully deploying the Session Bean:

At this point, we are ready to write our test class that will call the EJB.

Testing the Bean

To start, we need to create a new Java class in our package to test our bean with:

Then name the test class and tell MyEclipse to generate a main method for it:

Before we are ready to add code to our client and run it, we need to add appserver-rt.jar to our Build Path. This JAR is from the Glassfish 2 library directory and contains a customized file that will allow us to connect directly to the Glassfish 2 JNDI context automatically and retrieve our bean with almost no effort. So let's add that JAR now:

Then switch to the Libraries tab, and click Add External JAR to find the JAR and add it:

Drill down into the Glassfish 2 install directory, then into the /lib directory and select the appserver-rt.jar file and add it:

After it's added, hit OK:

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

public static void main(String[] args) {
try {
InitialContext ctx = new InitialContext();
MyBeanRemote bean = ( MyBeanRemote) ctx.lookup(" com.myeclipseide.ejb3.MyBeanRemote");
} catch (NamingException e) {

Session Bean Test Client Code

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

  1. We cast the returned bean not to MyBean but to the interface MyRemoteBean because we are requesting the remote bean from the JNDI context. As mentioned above, the methods exposed by the different Local/Remote interfaces could vary, so we need to stick to the interface we are requesting.
  2. Glassfish uses a default JNDI name-binding for EJBs that don't specify one. If you scroll back up to the server-log screenshot, you'll notice that the default name is printed out in the log. This default name is different from application to application server, and most folks 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")
  3. Once we have the bean, we can treat it like a local instance, and simply invoke it.

Because of how we wrote the code for our bean ( System.out.println) the result of #3 is output to the application server console in MyEclipse:

Our test client worked!

7. Conclusion

In this tutorial we took a look at how Stateless Session Bean development worked in MyEclipse using the new EJB3 annotations and technology in Java EE 5.0. Another important part of EJB3 are the EJB Entity Beans. Fortunately using EJB3 Entity Beans is almost identical to using JPA Entities; both of which MyEclipse can reverse-engineer for you into your project. If you are interested in learning about JPA/EJB3 Entities, please check out the MyEclipse JPA Tutorial.

If you have any suggestions for us to help make it more informative, please let us know.

Below we would like to provide you with some more information pertaining to the topic covered in this tutorial. We offer the FAQ section for quick references to common questions and the Resources section with links to other helpful resources online that you may want to become familiar. We realize we can't cover every question you may have in one tutorial, but between this tutorial contents and our additional learning resources we hope you are far on your way to feeling comfortable with the technology.

9. Resources

In this section we want to provide you with additional links to resources that supplement the topics covered in this tutorial. While this is not an exhaustive list, we do make an effort to point to the more popular links that should provide you with diverse, high-quality information.

  • Sample EJB3 Project for This Tutorial
  • List of Many Glassfish / EJB3 Tutorials and Tips
  • Glassfish EJB3 FAQ


Anonymous said...

Please look at your layout. Your text is covered by the contents.