项目作者: ddellagiacoma

项目描述 :
Accessing an enterprise bean
高级语言: Java
项目地址: git://github.com/ddellagiacoma/webarch-2016-assignment-3.git
创建时间: 2016-12-16T14:39:00Z
项目社区:https://github.com/ddellagiacoma/webarch-2016-assignment-3

开源协议:

下载


Accessing an enterprise bean

1. INTRODUCTION

The topic of this assignment is the Enterprise JavaBean (EJB), which is a server-side software component
that encapsulates the business logic of an application into a single object (the bean).

The first part of the assignment consists of installing WildFly, writing an enterprise bean which exposes a
method for giving a string containing the date and the time of the day and finally deploying the enterprise
bean on WildFly.

Moreover, the second part of the assignment consists of writing a client (a standard Java Application) which
connects to the bean, asks twice the method on the bean and writes the result on screen.

2. IMPLEMENTATION

The Java Enterprise Application named EntApp is composed by the stateless EJB DateTime. The bean
includes the method print() which return a string containing the current time and date.

  1. @Stateless
  2. public class DateTime implements ABean {
  3. @Override
  4. public String print() {
  5. String timeStamp = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy").format(new Date());
  6. return timeStamp;
  7. }
  8. }

On the other hand, the client BeanClient establishes a connection to WildFly and connects to the bean
DateTime. After that, it calls twice the remote method print() and prints the result.

  1. try {
  2. Properties jndiProps = new Properties();
  3. jndiProps.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
  4. jndiProps.put(Context.PROVIDER_URL, "http-remoting://localhost:8080");
  5. jndiProps.put("jboss.naming.client.ejb.context", true);
  6. InitialContext initialContext = new InitialContext(jndiProps);
  7. ABean abean = (ABean) initialContext.lookup("EntApp/EntApp-ejb/DateTime!beans.ABean");
  8. System.out.println("\n" + abean.print() + "\n" + abean.print());
  9. } catch (NamingException e) {
  10. System.err.println(e);
  11. }

Furthermore, the remote interface ABean has been implemented in the following way:

  1. @Remote
  2. public interface ABean {
  3. public String print();
  4. }

3. DEPLOYMENT

The Java Enterprise Application EntApp has to be built in order to generate the EntApp.ear file. The ear file
need to be copied in the following folder:

JBOSS_HOME/standalone/deployments

After that, WildFly (in this case version 9.0.1.Final) can be started launching the standalone.bat (or
standalone.sh) file. The following screen should appear:

image

Therefore, the BeanClient can be started running the jar file in the dist folder of the client in this way:

  1. java -jar BeanClient.jar

And the result should be similar to this:

image

It important to remember that the BeanClient requires the jboss-client.jar library to work correctly.