Thursday 12 September 2013

EJB Patterns: Service Facade example

This article shows the common Service Facade pattern in the EJB context. Several aspects takes into account this pattern.

 

1) The Service Facade insolates the service definition from its implementation.

2) It is the way of decoupling the Web tier from the Business tier.

3) From a transactional point of view, the business service may implement his transaction scope or be part of the current transaction.

4) The contract of the Service Facade interface is oriented to be invoked by Web tier or client software, but methods of the contract should not be invoked within the business implementation.

The following is an example of this pattern.

Firstly, an interface with the contract definition is defined:

@Local

public interface FarewellBusiness {

      public void sendMessage(String message);

      public void sendMessageDetail(String message,String detail);

     

}

 Secondly, a class that implements this interface is created. This class is the Stateless bean that will be managed by the JEE-container.

@Stateless (name="farewellBusiness")

@Clustered

@TransactionAttribute (TransactionAttributeType.REQUIRES_NEW)

public class FarewellBusinessBean implements FarewellBusiness {

 

      @Inject FarewellBusinessLogic business;

      public void sendMessage(String message) {

           

           

            business.sendMessage(message);

     

      }

      public void sendMessageDetail(String message,String detail)

      {

            business.sendDetailForMessage(message, detail);

      }

 

}

As the class reflects :

@Stateless: It is a stateless java bean.

@Clustered: It is executed within a JBoss cluster (for instance)

@TransactionAttribute (REQUIRED_NEW). Invocations of methods of this class require a transaction scope. If there is already a transaction scope running, the instance is executed under the transaction scope, if not; a new transactional process is created.

The invocation of the EJB is by way of the Service Interface, as the example below shows:

@ManagedBean (name="manager")

@SessionScoped

public class ManagerBean implements Serializable{

            @ManagedProperty(value="#{farewell1}")

            FarewellBean bean;

           

            @EJB

            FarewellBusiness farewellBusiness;

           

            public String send(){

                                    // send the message invoking business ejb

                 

                  farewellBusiness.sendMessage(bean.getFarewell1());

                  return "success";

            }

            public void setBean(FarewellBean bean) {

                  this.bean = bean;

            }

            public String sendDetail(){

                  farewellBusiness.sendMessageDetail(bean.getFarewell1(), bean.getDetail());

                  return "success";

            }

           

}

 

No comments:

Post a Comment