04_Implementing Business Logic

download 04_Implementing Business Logic

of 18

Transcript of 04_Implementing Business Logic

  • 8/14/2019 04_Implementing Business Logic

    1/18

    4Copyright 2007, Oracle. All rights reserved.

    Implementing Business Logic

  • 8/14/2019 04_Implementing Business Logic

    2/18

    4-2 Copyright 2007, Oracle. All rights reserved.

    Objectives

    After completing this lesson, you should be able to dothe following: Decide when to use a Session Bean

    Describe how a Session Bean works Discriminate between stateful and stateless beans Describe how the EntityManager manages Entities Create a Session Facade Bean and Interface

  • 8/14/2019 04_Implementing Business Logic

    3/18

    4-3 Copyright 2007, Oracle. All rights reserved.

    What Is a Session Bean?

    A Session Bean is a type of Enterprise JavaBean (EJB)that: Implements a business process

    Represents a client and server interaction Has a short lifespan Lives in memory rather than in persistent storage Is used to create a Session Facade

  • 8/14/2019 04_Implementing Business Logic

    4/18

    4-4 Copyright 2007, Oracle. All rights reserved.

    sr.srdemo.business

    Session Beans and JPA Entities

    sr.srdemo.persistence

    Products.java

    ServiceRequests.java

    Users.java

    EJB 3.0 Session Facade Bean JPA Entities

    Expose

    ServiceRequestFacadeBean.java

  • 8/14/2019 04_Implementing Business Logic

    5/18

    4-5 Copyright 2007, Oracle. All rights reserved.

    What Session Beans Do

    EJB 3.0 Session Beans: Contain persistence methods Implement JPA queries

    Encapsulate business logic Are invoked directly by the client Use data and resources on behalf of the client

  • 8/14/2019 04_Implementing Business Logic

    6/18

    4-6 Copyright 2007, Oracle. All rights reserved.

    Stateless and Stateful Session Beans

    There are two types of Session Beans: Stateless Session Bean (SLSB) Short-lived like HTTP request Is contained in a single method call Does not maintain client state

    Stateful Session Bean (SFSB) Long-lived like a login May invoke many methods Maintains state between different requests

    EJB container

    Client 1

    Client 2

    Pool of SLSBsEJB container

    Client 1

    Client 2

    SFSBs

  • 8/14/2019 04_Implementing Business Logic

    7/184-7 Copyright 2007, Oracle. All rights reserved.

    Life Cycle of a Stateless Session Bean

    Ready

    CreationDoes not

    existDestruction

    Method

  • 8/14/2019 04_Implementing Business Logic

    8/184-8 Copyright 2007, Oracle. All rights reserved.

    Life Cycle of a Stateful Session Bean

    Ready

    Creation

    Passive

    ActivationPassivation

    Does notexist

    Destruction

    Ready (in TX)

    Commit or rollback

    TX Method

    Method

    TX method

    Timeout

  • 8/14/2019 04_Implementing Business Logic

    9/184-9 Copyright 2007, Oracle. All rights reserved.

    Stateless Session Bean: Facade

    @Stateless(name=SRPublicFacade") public class SRPublicFacadeBean implements SRPublicFacade,

    SRPublicFacadeLocal{

    @PersistenceContext(unitName="EJB_Model")private EntityManager em;

    public Object mergeEntity(Object entity){ return em.merge(entity); }

    public Object persistEntity(Object entity) {em.persist(entity);return entity;

    }

  • 8/14/2019 04_Implementing Business Logic

    10/184-10 Copyright 2007, Oracle. All rights reserved.

    Implementing JPA Queries

    public List queryProductFindAll()

    { returnem.createNamedQuery(Product.findAll")

    .getResultList();}

    public Product findProductById(Integer findProdId);{return

    (Product)em .createNamedQuery(Product.findProductById")

    .setParameter(findProdId", findProdId).getSingleResult();}

    }

  • 8/14/2019 04_Implementing Business Logic

    11/184-11 Copyright 2007, Oracle. All rights reserved.

    Session Bean: Interface

    package sr.model;

    import java.util.List;import javax.ejb.Remote;

    @Local public interface SRPublicFacadeLocal {

    Object mergeEntity(Object entity);Object persistEntity(Object entity);List queryProductFindAll();

    Product findProductById(Integer, findProdId);

    }

  • 8/14/2019 04_Implementing Business Logic

    12/184-12 Copyright 2007, Oracle. All rights reserved.

    Stateful Session Bean: Example

    // CartBean.java package cart.ejb

    import javax.ejb.Stateful;@Stateful(name="Cart")

    public class CartBean implements Cart {private ArrayList items;

    @PostConstructpublic void initialize() { items = new ArrayList(); }public void addItem(String item) { items.add(item); }

    public void removeItem(String item){ items.remove(item); }public Collection getItems() { return items; }@Removepublic void dumpCart() {System.out.println("BYE!");};

    }

    2

    1

    3

    4

  • 8/14/2019 04_Implementing Business Logic

    13/184-13 Copyright 2007, Oracle. All rights reserved.

    EJB Client

    An EJB client is a stand-alone application, servlet,JSP, or another EJB that accesses the bean.It can be a:

    Local client Resides within the same Java Virtual Machine(JVM) as the bean

    Passes arguments by reference to the bean Interacts with the EJB through methods defined in

    the local interface Remote client

    Is location independent Passes arguments by value to the bean Interacts with the EJB through methods defined in

    the remote interface

  • 8/14/2019 04_Implementing Business Logic

    14/184-14 Copyright 2007, Oracle. All rights reserved.

    Interceptor Methods and Classes

    EJB 3.0 introduces the ability to create custominterceptor methods and classes that are called beforeinvoking the methods they intercept. Interceptors:

    Are available for only Session Beans (statelessand stateful) and message-driven beans Provide more granular control of a beans method

    invocation flow Can be used to implement custom transaction or

    security processes instead of having thoseservices provided by the EJB container

    Are a new feature whose implementation detailsare not fully defined and are subject to change

  • 8/14/2019 04_Implementing Business Logic

    15/184-15 Copyright 2007, Oracle. All rights reserved.

    Creating a Session Facade Beanin JDeveloper

    Select EJB Session Bean in the New Gallery. Select the Session Type: Stateless or Stateful. Select the Transaction Type: Container or Bean.

    Select Generate Session Facade Methods. Select the Entity implementation.

  • 8/14/2019 04_Implementing Business Logic

    16/184-16 Copyright 2007, Oracle. All rights reserved.

    Adding Methods to the Bean

    Select which methods you want to implement. JDeveloper proposes:

    Transactional methods

    Named queries Choose interfaces:

    Local Remote

  • 8/14/2019 04_Implementing Business Logic

    17/184-17 Copyright 2007, Oracle. All rights reserved.

    Summary

    In this lesson, you should have learned how to: Describe what a Session Bean does Describe what a Session Bean contains

    Differentiate between stateful and stateless beans Describe how the EntityManager manages Entities Create a Session Facade Bean and Interface

  • 8/14/2019 04_Implementing Business Logic

    18/18

    Practice Overview:Developing Session Facade Beans

    This practice covers the following topics: Creating a session facade Displaying data using a Session Facade Bean