+ All Categories
Home > Documents > EJB Programming with JBoss - Tobuku.com Programming with JBoss.pdf · EJB PROGRAMMING WITH JBOSS...

EJB Programming with JBoss - Tobuku.com Programming with JBoss.pdf · EJB PROGRAMMING WITH JBOSS...

Date post: 07-Feb-2018
Category:
Upload: dinhnguyet
View: 231 times
Download: 2 times
Share this document with a friend
19
www.tobuku.com - 1 - EJB PROGRAMMING WITH JBOSS March 2013 Level: By : Feri Djuandi Beginner Intermediate Expert 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. 1 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. 1 Source: http://www.oracle.com/technetwork/java/javaee/ejb/index.html Network Desktop Client Network Database EJB Container Session Beans Presentation tier Application tier/ Business Logic Data tier
Transcript
Page 1: EJB Programming with JBoss - Tobuku.com Programming with JBoss.pdf · EJB PROGRAMMING WITH JBOSS March 2013 Level: ... EJB containers are WebSphere Application Server, JBoss Application

www.tobuku.com

- 1 -

EJB PROGRAMMING WITH JBOSS March 2013 Level: √ By : Feri Djuandi Beginner Intermediate Expert

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.1

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.

1 Source: http://www.oracle.com/technetwork/java/javaee/ejb/index.html

Network Desktop

Client Network Database

EJB Container

Session

Beans

Presentation

tier Application tier/

Business Logic

Data tier

Page 2: EJB Programming with JBoss - Tobuku.com Programming with JBoss.pdf · EJB PROGRAMMING WITH JBOSS March 2013 Level: ... EJB containers are WebSphere Application Server, JBoss Application

www.tobuku.com

- 2 -

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

Network

Client

EJB Container

Session

Beans Proxy

Stub

Client

Proxy

Client

Proxy

Network Web

Client

Network Database

EJB Container

Session

Beans

Web Container

JSP,

Servlet

Page 3: EJB Programming with JBoss - Tobuku.com Programming with JBoss.pdf · EJB PROGRAMMING WITH JBOSS March 2013 Level: ... EJB containers are WebSphere Application Server, JBoss Application

www.tobuku.com

- 3 -

queue to ensure the transactions are processed reliably (atomicity, consistency, isolation,

durability).2

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.

3. 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.

4. 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

2 Source: http://en.wikipedia.org/wiki/ACID

Page 4: EJB Programming with JBoss - Tobuku.com Programming with JBoss.pdf · EJB PROGRAMMING WITH JBOSS March 2013 Level: ... EJB containers are WebSphere Application Server, JBoss Application

www.tobuku.com

- 4 -

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.

5. 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>

Page 5: EJB Programming with JBoss - Tobuku.com Programming with JBoss.pdf · EJB PROGRAMMING WITH JBOSS March 2013 Level: ... EJB containers are WebSphere Application Server, JBoss Application

www.tobuku.com

- 5 -

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

following files:

a. ..\standalone\configuration\application-users.properties

b. ..\standalone\configuration\application-roles.properties

c. ..\domain\configuration\application-users.properties

d. ..\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!

Page 6: EJB Programming with JBoss - Tobuku.com Programming with JBoss.pdf · EJB PROGRAMMING WITH JBOSS March 2013 Level: ... EJB containers are WebSphere Application Server, JBoss Application

www.tobuku.com

- 6 -

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.

2. 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.

Page 7: EJB Programming with JBoss - Tobuku.com Programming with JBoss.pdf · EJB PROGRAMMING WITH JBOSS March 2013 Level: ... EJB containers are WebSphere Application Server, JBoss Application

www.tobuku.com

- 7 -

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

Page 8: EJB Programming with JBoss - Tobuku.com Programming with JBoss.pdf · EJB PROGRAMMING WITH JBOSS March 2013 Level: ... EJB containers are WebSphere Application Server, JBoss Application

www.tobuku.com

- 8 -

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.

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

Type the following code for the interface.

package demo.mathbean.server;

import javax.ejb.Local;

@Local

public interface ComputeBeanLocal {

double AddNumbers(double num1, double num2);

double SubtractNumbers(double num1, double num2);

double MultiplyNumbers(double num1, double num2);

double DivideNumbers(double num1, double num2);

}

Create the second interface called ComputeBeanRemote.

package demo.mathbean.server;

import javax.ejb.Remote;

@Remote

public interface ComputeBeanRemote {

double AddNumbers(double num1, double num2);

double SubtractNumbers(double num1, double num2);

double MultiplyNumbers(double num1, double num2);

double DivideNumbers(double num1, double num2);

}

Page 9: EJB Programming with JBoss - Tobuku.com Programming with JBoss.pdf · EJB PROGRAMMING WITH JBOSS March 2013 Level: ... EJB containers are WebSphere Application Server, JBoss Application

www.tobuku.com

- 9 -

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 this 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.

4. Open the MathBeanClient project and create a new class called ComputeBean to

implement the previous interfaces.

Type the following code for the class.

package demo.mathbean.server;

import javax.ejb.Remote;

import javax.ejb.Stateless;

@Stateless

@Remote(ComputeBeanRemote.class)

public class ComputeBean implements ComputeBeanLocal,

ComputeBeanRemote{

public ComputeBean() {

}

Java

Desktop

Client

EJB Container

Session

Beans

JVM

Application Server

JVM

Remote

calls

EJB Container

Session

Beans

Web Container

JSP,

Servlet

JVM

Application Server

Local calls

Page 10: EJB Programming with JBoss - Tobuku.com Programming with JBoss.pdf · EJB PROGRAMMING WITH JBOSS March 2013 Level: ... EJB containers are WebSphere Application Server, JBoss Application

www.tobuku.com

- 10 -

@Override

public double AddNumbers(double num1, double num2) {

return num1 + num2;

}

@Override

public double SubtractNumbers(double num1, double num2) {

return num1 - num2;

}

@Override

public double MultiplyNumbers(double num1, double num2) {

return num1 * num2;

}

@Override

public double DivideNumbers(double num1, double num2) {

return num1 / num2;

}

}

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

Page 11: EJB Programming with JBoss - Tobuku.com Programming with JBoss.pdf · EJB PROGRAMMING WITH JBOSS March 2013 Level: ... EJB containers are WebSphere Application Server, JBoss Application

www.tobuku.com

- 11 -

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.

Page 12: EJB Programming with JBoss - Tobuku.com Programming with JBoss.pdf · EJB PROGRAMMING WITH JBOSS March 2013 Level: ... EJB containers are WebSphere Application Server, JBoss Application

www.tobuku.com

- 12 -

2. 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.

Page 13: EJB Programming with JBoss - Tobuku.com Programming with JBoss.pdf · EJB PROGRAMMING WITH JBOSS March 2013 Level: ... EJB containers are WebSphere Application Server, JBoss Application

www.tobuku.com

- 13 -

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.

Page 14: EJB Programming with JBoss - Tobuku.com Programming with JBoss.pdf · EJB PROGRAMMING WITH JBOSS March 2013 Level: ... EJB containers are WebSphere Application Server, JBoss Application

www.tobuku.com

- 14 -

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.

2. 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.

Page 15: EJB Programming with JBoss - Tobuku.com Programming with JBoss.pdf · EJB PROGRAMMING WITH JBOSS March 2013 Level: ... EJB containers are WebSphere Application Server, JBoss Application

www.tobuku.com

- 15 -

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.

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

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

a. Jboss-client.jar – add this file from the JBoss installation directory

under ..bin\client.

b. MathBeanClient.jar – add this generated proxy file from the earlier step.

Click Finish to generate the project files.

Page 16: EJB Programming with JBoss - Tobuku.com Programming with JBoss.pdf · EJB PROGRAMMING WITH JBOSS March 2013 Level: ... EJB containers are WebSphere Application Server, JBoss Application

www.tobuku.com

- 16 -

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

Click Finish to save the class.

Page 17: EJB Programming with JBoss - Tobuku.com Programming with JBoss.pdf · EJB PROGRAMMING WITH JBOSS March 2013 Level: ... EJB containers are WebSphere Application Server, JBoss Application

www.tobuku.com

- 17 -

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

package demo.mathbean.client;

import java.util.Properties;

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.naming.NamingException;

import demo.mathbean.server.ComputeBeanRemote;

public class MainClass {

public static void main(String[] args) {

try {

Properties properties = new Properties();

properties.put(Context.INITIAL_CONTEXT_FACTORY,

"org.jboss.naming.remote.client.InitialContextFactory");

properties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");

properties.put(Context.PROVIDER_URL, "remote://localhost:4447");

properties.put(Context.SECURITY_PRINCIPAL, "user01");

properties.put(Context.SECURITY_CREDENTIALS, "12345678");

properties.put("jboss.naming.client.ejb.context", true);

Context ctx = new InitialContext(properties);

final String appName = "MathBeanDemo"; //The application name in the JBoss AS

final String moduleName = "MathBean"; //The name of the session bean (the JAR file)

final String distinctName = "";

final String beanName = "ComputeBean"; //The class name of the session bean

final String viewClassName = ComputeBeanRemote.class.getName();

//The interface name (either local or remote)

String lookupObj = "ejb:" + appName + "/" + moduleName + "/" + distinctName + "/" +

beanName + "!" + viewClassName;

ComputeBeanRemote bean = (ComputeBeanRemote)ctx.lookup(lookupObj);

System.out.println("Add: " + bean.AddNumbers(100, 20));

Page 18: EJB Programming with JBoss - Tobuku.com Programming with JBoss.pdf · EJB PROGRAMMING WITH JBOSS March 2013 Level: ... EJB containers are WebSphere Application Server, JBoss Application

www.tobuku.com

- 18 -

System.out.println("Divide: " + bean.DivideNumbers(100, 20));

System.out.println("Multiply: " + bean.MultiplyNumbers(100, 20));

System.out.println("Subtract: " + bean.SubtractNumbers(100, 20));

}

catch (NamingException e) {

e.printStackTrace();

}

}

}

One of the important code lays in the following line:

...

ComputeBeanRemote bean = (ComputeBeanRemote)ctx.lookup(lookupObj);

...

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.

...

properties.put(Context.INITIAL_CONTEXT_FACTORY,

"org.jboss.naming.remote.client.InitialContextFactory");

properties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");

properties.put(Context.PROVIDER_URL, "remote://localhost:4447");

properties.put(Context.SECURITY_PRINCIPAL, "user01");

properties.put(Context.SECURITY_CREDENTIALS, "12345678");

properties.put("jboss.naming.client.ejb.context", true);

...

Page 19: EJB Programming with JBoss - Tobuku.com Programming with JBoss.pdf · EJB PROGRAMMING WITH JBOSS March 2013 Level: ... EJB containers are WebSphere Application Server, JBoss Application

www.tobuku.com

- 19 -

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.

5. 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.

References:

� Kodali, Raghu R. and Jonathan Wetherbee. “Beginning EJB3 Application Development”.

Apress. 2006

� https://docs.jboss.org/author/display/AS71/EJB+invocations+from+a+remote+client+usi

ng+JNDI

� http://www.oracle.com/technetwork/java/javaee/ejb/index.html


Recommended