EJB Programming with JBoss

Platform : Eclipse Juno, JBoss AS 7.1.1.

EJB at a Glance

Enterprise JavaBeans (EJB) technology is the server-side component architecture for Java Platform, Enterprise Edition (Java EE). EJB technology enables rapid and simplified development of distributed, transactional, secure and portable applications based on Java technology.

The discussion of EJB is very much closely related to the multi-tier programming or distributed programming. To a beginner, EJB can be simply explained using the 3-tier programming model – something that we learn at school and still relevant until today. If you are familiar with the model, then the picture below is quite self explanatory so it won’t be explained further in this article. If you are new, then you are suggested to seek the related literature in the Internet which can be easily found and free.

Session bean is a type of EJB object that most commonly used by the Java programmers when dealing with enterprise Java applications (we will learn other type of object like Message-driven Bean in another occasion). It performs business service operations and orchestrates transaction and access control behavior. EJB container in the middle tier is a server application where the session beans are hosted and running. It provides a run-time environment for enterprise beans within the application server. The container handles all aspects of an enterprise bean’s operation within the application server and acts as an intermediary between the user-written business logic within the bean and the rest of the application server environment. Some popular EJB containers are WebSphere Application Server, JBoss Application Server, WebLogic Server and Apache Geronimo.


From the client side point of view, the EJB model is suitable for both desktop applications (i.e. window/GUI programs or console/command line programs) and web applications.


As mentioned earlier, EJB is an implementation of a distributed computing where the processes are split between the client (regardless the client is a thin or thick model) and the server side. In order for the client and the server to be able to communicate, there is a communication tunnel between the two nodes so they can pass messages back and forth. That tunnel is established by a pair of objects so called proxy and stub. A proxy resides in the client side, while a stub in the server side. A stub has a unique identifier in a specified network which used as an address by the corresponding proxy to create a link.


A stub may be referred by more that one proxy. When there are several clients access the session bean concurrently at the same time, the EJB container will manage the transactions in a queue to ensure the transactions are processed reliably (atomicity, consistency, isolation, durability).

That is enough for the brief EJB introduction, now we start the main course.

Starting the JBoss Application Server

Make sure JBoss Application Server (JBoss AS) has been installed on your computer. If JBoss AS hasn’t been installed, please read the other article in this website which easily explains how to obtain and set up JBoss. Now let us start the server when you are ready.

In this tutorial, we assume the JBoss AS is installed in the C:\jboss-as-7.1.1.Final directory. This time we are going to start the application server.

  1. Get into the bin sub-directory, locate the standalone.bat file and run it.
  2. Open an Internet browser and try to access the URL: localhost:8080.


  1. Click the Administration Console link to administer this application server. If it is the first time the server is up, most probably you haven’t created any administrator ID. If so you will be required to create it first before being able to open the console.
  2. To create a user ID, enter the bin sub-directory again, locate the add-user.bat file and run it.

Follow the step below to create an administrator ID:

Type of user   :   Management User (type “a”)

Username       :   asadmin

Password        :   12345678


When the ID creation is completed, please go back to the Internet browser and click again the “Administration Console” link. This time you will be prompted with a login box to enter the user ID and password. Please proceed with the ID you’ve just created and you will see the administration console right away.

  1. Besides the JBoss administrator ID, we need another IDs that will be used by the application client to connect to the JBoss AS. This time the appropriate ID is an application user ID, instead of an administrator ID. Now let us repeat to run the add-user.bat file.

Follow the step below to create a sample of an application user:

Type of user   :   Application User (type “b”)

Realm               :   <blank>

Username       :   user01

Password        :   12345678

Roles                 : <blank, we will add it later>


Upon the application user creation, be noted that JBoss will add some entries in the following files:

    • ..\standalone\configuration\application-users.properties
    • ..\standalone\configuration\application-roles.properties
    • ..\domain\configuration\application-users.properties
    • ..\domain\configuration\application-roles.properties

Those files can be opened with any text editor program such as Ms Notepad. Now please open the first file.


There is one entry corresponds to the user01. You may guess the encrypted characters after the user name is the password.

Open the second file and see what inside it.


The application-roles.properties file contains a list of application users with their roles. At this moment there may not any user with defined roles. Here we must associate our application user example with at least one role so that the client application is able to connect to JBoss using the user ID.

Please insert a new line in the bottom of the file and type “user01=PowerUser”. It indicates that the user01 is granted the PowerUser role. It is a quite powerful user role, but that is okay only for our example here. Save and close the files.

Please repeat the same steps for the files in the ..\domain\configuration directory.

We are done preparing the JBoss AS with the new IDs, now it is time for the exiting part: creating the JBoss application!


Creating the Session Beans

This part explains the component programming which runs on the application server side.

  1. Open Eclipse and create a new Enterprise Application Project, which is also referred to as EAR project.


Name the project as MathBean Demo and ensure the selected target runtime is JBoss AS. Click Finish to create the project files.

  1. Create a new EJB Project.

Name the project as MathBean and ensure the selected target runtime is JBoss AS. Also ensure to add the project to the MathBean Demo EAR.

Click Finish to create the project files.


When it is done, you will realize that Eclipse creates two EJB projects instead of one!

There is a MathBean project, the one that we expect to exist – and a MathBeanClient project, the one which appears surprisingly.

In fact, there is really nothing wrong with this behavior. Notice that Eclipse automatically creates the second project if you recall that there must be a proxy-stub pair in any distributed application. The MathBean project relates to the stub part, while the MathBeanClient project corresponds to the proxy part.


MathBeanClient will consist of interfaces with all method declarations, but no business logic inside them – the script is very less. On the other hand, MathBean comprises classes that implement the interfaces; therefore the programmers will spend most of their time in working with these classes, writing the scripts to program the business logic inside it.

  1. Open the MathBeanClient project and create a new interface called ComputeBeanLocal.


Type the following code for the interface.

 Create the second interface called ComputeBeanRemote.

 Both interfaces are very much alike except the local and remote key words. The reason why we need to create two similar interfaces is to cope with two different calls: the local call and remote call. Please see the following picture for the explanation.


Here both web container and the EJB container are running in the same JVM, therefore the calls made by the JSP or servlet to the session beans are local calls.


In above picure both client application and the session bean are running in separate/different JVM, therefore the calls made by client to the session beans are remote calls.

For these two different calls, we must create respective interfaces whose methods may or may not be the same.

  1. Open the MathBeanClient project and create a new class called ComputeBean to implement the previous interfaces.

Type the following code for the class.


That’s it. We have done with the session beans.

Packaging the Proxy and Stub

The next step is to create the pair of proxy and stub. Later the stub will be deployed in the JBoss AS, while the proxy will be embedded in a client application.

  1. Open the MathBean Demo EAR created in the early steps, then right-click on the project name and select Export à EAR file.


Export the project as MathBeanDemo.ear. That is the stub component.

If the file is opened using a decompression program such as WinZip, then we will see two JAR files: MathBean.jar and MathBeanClient.jar. Be noted that MathBean.jar is the session bean where the business logic resides.


  1. Open the MathBeanClient project in the early steps, then right-click on the project name and select Export. From the Export dialog window, select Java à JAR file.


Name JAR file as MathBeanClient.jar. That is the proxy component.

If the file is opened using a decompression program such as WinZip, then we will see two class files: ComputeBeanLocal.class and ComputeBeanRemote.class. Be noted that these classes are merely interfaces – no business logic at all.


Deploying the Stub in JBoss AS

The session bean inside the generated EAR file should be put on the JBoss AS.

  1. Open the JBoss AS administration console and login using an administrator ID created earlier.


Open the Manage Deployments menu. At this time there may not be any items in the list. Click the Add Content button to upload to EAR file.


Locate the MathBeanDemo.ear file and click Next.


Click Save to upload.

  1. Now the MathBeanDemo should appear in the item list. Please ensure the item is enabled. If not, then press the Enable button.


That’s all. We are done deploying the stub in the JBoss AS.

Creating a Client Application

The last part is to create a client application that tries to call the session bean remotely.

  1. Open Eclipse and create a new ordinary Java Project.


Name the project as MathBean ClientApp.

  1. Click the Libraries tab to associate the JAR files.


Click the Add External JARs button to add two JAR files:

    • Jboss-client.jar – add this file from the JBoss installation directory under ..bin\client.
    • MathBeanClient.jar – add this generated proxy file from the earlier step.

Click Finish to generate the project files.

  1. Create a new class, name it as MainClass and include the main() method.


Click Finish to save the class.

  1. Open the class just created and type the following code.


One of the important code lays in the following line:

 This line tells the program to search a session bean or a stub which identified with addressed by the lookupObj variable. When it is found, the a link will be established using a proxy which represented by the bean variable. Be noted that the bean variable is an instance of the ComputeBeanRemote interface – not the actual class itself. But amazingly the method calls of the object return the values just like what we expect as if we call the class (notice that an interface does not have any business logic). So we’re quite confident that these proxy-stub things really work.

The lookupObj variable is an instance of a Properties object type. It represents the address of the application server that the proxy wishes to locate.

 Some of the properties above like INITIAL_CONTEXT_FACTORY and URL_PKG_PREFIXES should be assigned with values that already given by the vendor of the allocation server, so that’s not arguable – just receive it. The other application servers will have different values, so you need to read the related documentation carefully. The PROVIDER_URL property represents the address of the server. If the server is not a local computer, it can be set as an IP address or a server name

Both SECURITY_PRINCIPAL and SECURITY_CREDENTIALS properties are the application user ID and password that we discussed earlier.

  1. Save and run the code.


 If the program works well, you should see the output of the program as shown in the picture above.

We have learned how to create a very simple distributed application using JBoss. There are a lot of fantastic things that can be done by JBoss. As a hungry learner, you are highly recommended to seek another literature that written by professional authors and experienced practitioners where you can obtain various techniques and knowledge.




Leave a Reply

Your email address will not be published. Required fields are marked *