Remote Accessibility EJB 3.1

In a new project I’ve had to used the Enterprise Java Bean (EJB) techonology to distribute object in different layers and access it in remote way.

In this article I would like to show you the first contact with this type of technology and how it works.

The aim of my using of Ejb was to split in two differents layers the business object. The idea was client-server paradigm.

We’ll need an application server to do it. You can find a lot of application server on internet, both commercial and not. I used Glassfish in my project, anyway you get this result even with other application server (Jboss is another).

Let’s start to work.

The example describes a fake order process center of one company. I used eclipse template project. I’d like to show you the steps to use it

Create an EJB Project

On the next page you’ll decide whether create client module for consuming the server object. I left checked the box and created it.

The final result is to obtain three projects:

OrderProcess
OrderProcessClient
OrderProcessEAR.

The business object is located server side and client access it through remote interface using EJB technology.

@Remote annotation

How is possible access an object from other application? You can find a lot of documentation about application server and how it works. Briefly, an interface annotated by @Remote is accessible through application server from other application. This interface will be exposed as JNDI resource.

I’m using Ejb version 3.1 and this is my remote interface.

package it.orderprojectserver.controller;
import it.orderprojectserver.bean.Order;
import javax.ejb.Remote;

@Remote
public interface IOrder {

 public boolean acceptOrder(Order order);
 
 public Order processOrder(Order order);
 
 public Order shippingOrder(Order order);
 
}

And this is the implementation of this interface.

package it.orderprojectserver.controller;

import java.util.Date;

import javax.ejb.Stateless;
import javax.transaction.SystemException;

import it.orderprojectserver.bean.Order;

@Stateless(name="OrderImpl", mappedName="ejb/OrderProjectServer")
public class OrderImpl implements IOrder {

 @Override
 public boolean acceptOrder(Order order) {
  if (order.quantity==0)
   return false;
  else
   return true;
 }

 @Override
 public Order processOrder(Order order) {
  order.setAmount(order.quantity * 0.8f);
  return order;
 }

 @Override
 public Order shippingOrder(Order order) {
  order.setResponse("Shipping in time " + new Date().toString());
  return order;
 }

}

Take a look at row:

@Stateless(name=”OrderImpl”, mappedName=”ejb/OrderProjectServer”)

I defined a Stateless bean with name OrderImpl and it’ll be exposed with name ejb/OrderProjectServer. Again, you can find more and more documentation about these definitions on many web site.

The last code is the Order bean. It must be serializable because that will be used both client and server side.


package it.orderprojectserver.bean;

public class Order implements java.io.Serializable {
 
private static final long serialVersionUID = 7830120993371620210L;
 
 public String orderId;
 public int quantity;
 public float amount;
 private String response;
 
 public Order(String orderId,
     int quantity,
     float amount)
 {
  this.orderId = orderId;  
  this.quantity = quantity;
  this.amount = amount;
 }
 
 public String getOrderId() {
  return orderId;
 }
 public void setOrderId(String orderId) {
  this.orderId = orderId;
 }
 public int getQuantity() {
  return quantity;
 }
 public void setQuantity(int quantity) {
  this.quantity = quantity;
 }
 public float getAmount() {
  return amount;
 }
 public void setAmount(float amount) {
  this.amount = amount;
 }
 
 @Override
 public String toString()
 {
  return "orderId: " + orderId +
    " quantity: " + quantity +
    " amount: " + amount +
    " response:" + response ;
 }

 public String getResponse() {
  return response;
 }

 public void setResponse(String response) {
  this.response = response;
 }
}

Now, it’s time to deploy it on the application server! If everything is ok you’ll see into the log file rows like these:

INFO: EJB5181:Portable JNDI names for EJB OrderImpl: [java:global/OrderProcessEAR/OrderProcess/OrderImpl, java:global/OrderProcessEAR/OrderProcess/OrderImpl!it.orderprojectserver.controller.IOrder]
INFO: EJB5182:Glassfish-specific (Non-portable) JNDI names for EJB OrderImpl: [ejb/OrderProjectServer, ejb/OrderProjectServer#it.orderprojectserver.controller.IOrder]

We’ve deployed the object on the server.

In the next part I’ll show you how consuming the object from client and test it.

Advertisements

2 thoughts on “Remote Accessibility EJB 3.1

  1. Hi .
    How to fix that error.
    “INFO: EJB5181:Portable JNDI names for EJB OrderImpl: [java:global/OrderProcessEAR/OrderProcess/OrderImpl, java:global/OrderProcessEAR/OrderProcess/OrderImpl!it.orderprojectserver.controller.IOrder]
    INFO: EJB5182:Glassfish-specific (Non-portable) JNDI names for EJB OrderImpl: [ejb/OrderProjectServer, ejb/OrderProjectServer#it.orderprojectserver.controller.IOrder]”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s