introduction to ejb 3

Upload: rguptamtech

Post on 05-Apr-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 Introduction to EJB 3

    1/79

    Rajeev GuptaM. Tech. CS

    EJB 3.0

  • 7/31/2019 Introduction to EJB 3

    2/79

  • 7/31/2019 Introduction to EJB 3

    3/79

    Session 1

  • 7/31/2019 Introduction to EJB 3

    4/79

    Overview

    Typical 4 tier architecture

    Why EJB

    EJB Containers

    Container Services Types of EJB and Introduction

    EJB use Cases

  • 7/31/2019 Introduction to EJB 3

    5/79

    typical four tier architecture

  • 7/31/2019 Introduction to EJB 3

    6/79

  • 7/31/2019 Introduction to EJB 3

    7/79

    What is ejb?

    EJB is component based framework for buildingand deploying distributed applications

  • 7/31/2019 Introduction to EJB 3

    8/79

    why we need it?

    EJB as a framework provides services to the EJBcomponents

  • 7/31/2019 Introduction to EJB 3

    9/79

    EJB as a container?

    It provides a set of services basically start with transaction

    security

    persistence

    Removing

    timer state managements

    Messaging etc

    Without EJB overhead on the programmer to write allabove services

    EJB container is deployed on application server suchas Jboss, Weblogic, glassfish etc.

    Programmer is only writing logic rest is provided byejb container.

  • 7/31/2019 Introduction to EJB 3

    10/79

  • 7/31/2019 Introduction to EJB 3

    11/79

    Type of bean

    Session bean Business Logic

    Entity bean

    Entity + Bean, called for persistence....

    CMP(Container managed): Our container is going

    to take care ...

    BMP(Programmer managed): A programme has to

    write jdbc code in entity bean to read /delete record

    Message driven bean

  • 7/31/2019 Introduction to EJB 3

    12/79

    What is Session Bean Sesssion Bean

    An EJB 3.0 or EJB 2.1 EJB component created by aclient for the duration of a single client-serversession used to perform operations for the client.

    Stateless

    oA session bean that does not maintain conversationalstate. Used for reusable business services that are notconnected to any specific client.

    Stateful

    oA session bean that does maintain conversational

    state. Used for conversational sessions with a singleclient (for the duration of its lifetime) that maintainstate, such as instance variable values or transactionalstate.

  • 7/31/2019 Introduction to EJB 3

    13/79

    What is Entity Bean

    Entity An EJB 3.0 compliant light-weight entity object that

    represents persistent data stored in a relational database

    using container-managed persistence. Because it is not a

    remotely accessible component, an entity can represent a

    fine-grained persistent object.

    Entity Bean An EJB 2.1 EJB component that represents persistent data

    stored in a relational database

  • 7/31/2019 Introduction to EJB 3

    14/79

    What is MDB

    MDB A Message-Driven Bean (MDB) is an EJB 3.0 or EJB 2.1

    EJB component that functions as an asynchronous

    consumer of Java Message Service (JMS) messages.

  • 7/31/2019 Introduction to EJB 3

    15/79

    Once upon a time . EJB 2.0

    Services such as CheckInventoryEJB etc is to beconfigured by two way

    Programmatic

    declarative

    Testing and deployment is not easy to create any type of bean in EJB 2.0 We have

    some interface

    such as Remote, Home Interface and Local

    Interface and Whatever bean we are writing has to be

    implemented by ejb we are writing-Depandancywith the interface

    Bean are configured using xml files

    Clint for EJB 2.0 is Servlet, JSP, A let etc

  • 7/31/2019 Introduction to EJB 3

    16/79

    Diving into EJB 2.0

  • 7/31/2019 Introduction to EJB 3

    17/79

  • 7/31/2019 Introduction to EJB 3

    18/79

  • 7/31/2019 Introduction to EJB 3

    19/79

  • 7/31/2019 Introduction to EJB 3

    20/79

  • 7/31/2019 Introduction to EJB 3

    21/79

  • 7/31/2019 Introduction to EJB 3

    22/79

  • 7/31/2019 Introduction to EJB 3

    23/79

  • 7/31/2019 Introduction to EJB 3

    24/79

  • 7/31/2019 Introduction to EJB 3

    25/79

    EJB 3.0

    EJB provide configuration using annotation Development and testing is easy

    We have POJO based bean: don't require anyinterface ;we can expose any normal bean as ejb

    bean Bean are configured using annotation

    EJB 3.0 don't have concept of entity bean, theentity bean is replaced by POJO based JPA

    In EJB 3.0 sun micro system comes with its ownframework for ORM i.e. JPA(Annotation based)

    EJB 3.0 bean can be exposed as a web servicecan be called from .NET client

  • 7/31/2019 Introduction to EJB 3

    26/79

  • 7/31/2019 Introduction to EJB 3

    27/79

    EJB Use Cases?

    Example consider flight booking system(Ecommerce application)

    Browse

    Select Flight

    Address Details

    Passport details

    Credit card details

    Confirmation

  • 7/31/2019 Introduction to EJB 3

    28/79

    Flight Booking system--------------------------------------------

    possible use cases.

    Browse-Session Bean

    Select Flight-Session Bean

    Address Details-Session Bean Passport details-Session Bean

    Credit card details-Session Bean

    Confirmation-Entity bean

  • 7/31/2019 Introduction to EJB 3

    29/79

    Use case for Message Driven Bean?

    During X-max world wide the amazon.commillions of order from one place to another ...i am

    in India and my friend in US...

    eBay provide on line ecommerce and ship to my

    friend...

    they have MILLIONS OF TRAINSACTION to

    complete

  • 7/31/2019 Introduction to EJB 3

    30/79

    Order processing system

  • 7/31/2019 Introduction to EJB 3

    31/79

    Mobile Activation

  • 7/31/2019 Introduction to EJB 3

    32/79

    Session 2

  • 7/31/2019 Introduction to EJB 3

    33/79

    Session Beans

    Stateless session bean Singletons

    Stateful session bean

    Lifecycle and State Transitions

  • 7/31/2019 Introduction to EJB 3

    34/79

    Stateless session bean

  • 7/31/2019 Introduction to EJB 3

    35/79

    Stateless Session Beans A stateless session bean does not maintain a

    conversational state with the client ie Once themethod is finished, the client-specific state should notbe retained i.e. the EJB container destroys astateless session bean

    These types of session beans do not use the instancevariables,So they do not persist data across methodinvocation and therefore there is no need to

    passivates the bean's instance.

    Because stateless session beans can supportmultiple clients, they provide the better scalability forapplications that require large numbers of clients.

  • 7/31/2019 Introduction to EJB 3

    36/79

    The Lifecycle of a StatelessSession Bean

    Because a stateless session bean is neverpassivated, its lifecycle has only two stages:

    1. nonexistent and

    2. ready for the invocation of business methods.

  • 7/31/2019 Introduction to EJB 3

    37/79

    The Lifecycle of a StatelessSession Bean

    The EJB container typically creates andmaintains a pool of stateless session beans,

    beginning the stateless session beans lifecycle.

    The container performs any dependency

    injection and then invokes the method annotated@PostConstruct, if it exists. The bean is now

    ready to have its business methods invoked by a

    client.

    At the end of the lifecycle, the EJB container calls

    the method annotated @PreDestroy, if it exists.

    The beans instance is then ready for garbage

    collection.

  • 7/31/2019 Introduction to EJB 3

    38/79

    Lifecycle Callbacks for StatelessSession Beans

  • 7/31/2019 Introduction to EJB 3

    39/79

    Addition of a new instance to the

    Stateless Session Bean Pool:

  • 7/31/2019 Introduction to EJB 3

    40/79

  • 7/31/2019 Introduction to EJB 3

    41/79

    Servicing a business method

    S S

  • 7/31/2019 Introduction to EJB 3

    42/79

    Removing the Stateless Session

    Bean instance

  • 7/31/2019 Introduction to EJB 3

    43/79

  • 7/31/2019 Introduction to EJB 3

    44/79

    Hello world exampleStateless session bean

  • 7/31/2019 Introduction to EJB 3

    45/79

    Promise

    package com.demo;import javax.ejb.Remote;

    @Remotepublic interface FirstDemoEJBRemote {

    public String testDemo();

    }

  • 7/31/2019 Introduction to EJB 3

    46/79

    Implementation.package com.demo;

    import javax.ejb.Stateless;

    /**

    * Session Bean implementation class FirstDemoEJB

    */

    @Stateless

    public class FirstDemoEJB implements FirstDemoEJBRemote {

    public FirstDemoEJB() {

    // TODO Auto-generated constructor stub

    }

    @Override

    public String testDemo() {

    // TODO Auto-generated method stubreturn "testing session bean..........";

    }

    }

    And the client

  • 7/31/2019 Introduction to EJB 3

    47/79

    And the client

    public class DemoTestEJBServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException {

    // TODO Auto-generated method stub

    PrintWriter out=response.getWriter();

    try

    {

    InitialContext ctx=new InitialContext();

    Object obj=ctx.lookup("FirstDemoEJB/remote");

    out.print(obj);

    FirstDemoEJBRemote remote=(FirstDemoEJBRemote)obj;

    String result=remote.testDemo();

    out.print(result);

    }

    catch(Exception ex){

    }

    }

    }

  • 7/31/2019 Introduction to EJB 3

    48/79

    ejb-jar.xml

    DemoEJB

  • 7/31/2019 Introduction to EJB 3

    49/79

    High level view

  • 7/31/2019 Introduction to EJB 3

    50/79

    Low level view

  • 7/31/2019 Introduction to EJB 3

    51/79

    Singleton Session Bean

    The Lifec cle of a Singleton

  • 7/31/2019 Introduction to EJB 3

    52/79

    The Lifecycle of a SingletonSession Bean

    Like a stateless session bean, a singletonsession bean is never passivated and has only

    two stages, nonexistent and ready for the

    invocation of business methods

    The Lifecycle of a Singleton

  • 7/31/2019 Introduction to EJB 3

    53/79

    The Lifecycle of a SingletonSession Bean

    The EJB container initiates the singleton sessionbean lifecycle by creating the singleton instance.This occurs upon application deployment if thesingleton is annotated with the @Startupannotation The container performs anydependency injection and then invokes themethod annotated @PostConstruct, if it exists.The singleton session bean is now ready to haveits business methods invoked by the client.

    At the end of the lifecycle, the EJB container callsthe method annotated @PreDestroy, if it exists.The singleton session bean is now ready for

    garbage collection.

  • 7/31/2019 Introduction to EJB 3

    54/79

    Stateful Session Bean

    Stateful Session Bean

  • 7/31/2019 Introduction to EJB 3

    55/79

    Stateful Session Bean

    These types of beans use the instance variables that allows the datapersistent across method invocation because the instance variables

    allow persistence of data across method invocation.

    The client sets the data to these variables which he wants to persist.

    A stateful session bean retains its state across multiple methodinvocations made by the same client. If the stateful session bean's state

    is changed during a method invocation, then that state will be availableto the same client on the following invocation.

    The state of a client bean is retained for the duration of the client-beansession.

    Once the client removes the bean or terminates, the session ends andthe state disappears.

    Because the client interacts with its bean, this state is often called theconversational state

  • 7/31/2019 Introduction to EJB 3

    56/79

    The Lifecycle of a Stateful Session

  • 7/31/2019 Introduction to EJB 3

    57/79

    The Lifecycle of a Stateful SessionBean

    The client initiates the lifecycle by obtaining areference to a stateful session bean. The

    container performs any dependency injection and

    then invokes the method annotated with

    @PostConstruct, if any. The bean is now ready tohave its business methods invoked by the client.

    The Lifecycle of a Stateful Session

  • 7/31/2019 Introduction to EJB 3

    58/79

    The Lifecycle of a Stateful SessionBean

    While in the ready stage, the EJB container may decide todeactivate, orpassivate, the bean by moving it from memory tosecondary storage. (Typically, the EJB container uses a least-recently-used algorithm to select a bean for passivation.)

    The EJB container invokes the method annotated@PrePassivate, if any, immediately before passivating it. If aclient invokes a business method on the bean while it is in thepassive stage, the EJB container activates the bean, calls themethod annotated @PostActivate, if any, and then moves it tothe ready stage.

    At the end of the lifecycle, the client invokes a method annotated@Remove, and the EJB container calls the method annotated@PreDestroy, if any. The beans instance is then ready for

    garbage collection. Your code controls the invocation of only one lifecycle method:

    the method annotated @Remove. All other methods in Figureare invoked by the EJB container.

    Lifecycle Callbacks for Stateful

    http://download.oracle.com/javaee/6/tutorial/doc/giplj.htmlhttp://download.oracle.com/javaee/6/tutorial/doc/giplj.html
  • 7/31/2019 Introduction to EJB 3

    59/79

    Lifecycle Callbacks for StatefulSession Beans

    Stateful session beans support callbacks for the lifecycle events:

    construction, destruction, activation, and passivation.

  • 7/31/2019 Introduction to EJB 3

    60/79

    Retrieving reference to Stateful Session Bean

    business interface from JNDI

  • 7/31/2019 Introduction to EJB 3

    61/79

    Servicing a business method

  • 7/31/2019 Introduction to EJB 3

    62/79

    Passivating a Stateful Session Bean

  • 7/31/2019 Introduction to EJB 3

    63/79

    Activating a Stateful Session Bean

  • 7/31/2019 Introduction to EJB 3

    64/79

    Servicing a @Remove method

    lifecycle event callbacks

  • 7/31/2019 Introduction to EJB 3

    65/79

    lifecycle event callbacks PostConstruct

    PostConstruct methods are invoked on the newly constructed instance, afterany dependency injection has been performed by the container and beforethe first business method is invoked on the bean.

    PostConstruct methods are invoked in an unspecified transaction and securitycontext.

    PreDestroy PreDestroy methods execute after any method annotated with the Remove

    annotation has completed.

    PreDestroy methods are invoked in an unspecified transaction and security

    context. PostActivate

    This notification signals the instance it has just been reactivated.

    Its purpose is to allow stateful session beans to maintain those resources thatneed to be reopened during an instance's activation.

    PrePassivate

    This notification signals the intent of the container to passivate the instance. Its purpose is to allow stateful session beans to maintain those open

    resources that need to be closed prior to an instance's passivation.

    NOTE: The callbacks PreConstruct, PostDestroy, PreActivate, andPostPassivate were not introduced because there did not seem to be usecases that justified their introduction.

  • 7/31/2019 Introduction to EJB 3

    66/79

    Hello World

    Stateful Session Bean

  • 7/31/2019 Introduction to EJB 3

    67/79

    account session bean

    Account business interface

    package ejbExample.stateful;

    importjavax.ejb.Remote;@Remotepublic interfaceAccount {

    public float deposit(float amount);public float withdraw(float amount);@Remove

    public void remove();}

  • 7/31/2019 Introduction to EJB 3

    68/79

  • 7/31/2019 Introduction to EJB 3

    69/79

    Life-Cycle Callback Methods

    Methods in the bean class may be declared as alife-cycle callback method by annotating the

    method with the following annotations: javax.annotation.PostConstruct

    javax.annotation.PreDestroy javax.ejb.PostActivate

    javax.ejb.PrePassivate

  • 7/31/2019 Introduction to EJB 3

    70/79

    Method Description@PostConstruct

    Invoked by the container on newly constructed bean instancesbefore the first business method is invoked on the enterprisebean and after all dependency injection has completed.

    @PreDestroy

    Invoked, when the bean is about to be destoryed by EJB

    container before removing the enterprise bean instance and afterany method annotated @Remove has completed.

    @PostActivate

    Invoked by the container after the container moves the beanfrom secondary storage to active status.

    @PrePassivate

    Invoked by the container before the container passivates theenterprise bean, i.e. the container temporarily removes the beanfrom the environment and saves it to secondary storage.

  • 7/31/2019 Introduction to EJB 3

    71/79

    Session 3

  • 7/31/2019 Introduction to EJB 3

    72/79

    Message Driven Bean

    The Lifecycle of a Message-Driven

  • 7/31/2019 Introduction to EJB 3

    73/79

    The Lifecycle of a Message DrivenBean

    The Lifecycle of a Message-Driven

  • 7/31/2019 Introduction to EJB 3

    74/79

    The Lifecycle of a Message DrivenBean

    The EJB container usually creates a pool of message-driven bean instances. For each instance, the EJBcontainer performs these tasks.

    If the message-driven bean uses dependencyinjection, the container injects these references before

    instantiating the instance. The container calls the method annotated

    @PostConstruct, if any.

    Like a stateless session bean, a message-driven

    bean is never passivated and has only two states:nonexistent and ready to receive messages.

    At the end of the lifecycle, the container calls themethod annotated @PreDestroy, if any. The beansinstance is then ready for garbage collection.

    Lifecycle Callbacks for Message-

  • 7/31/2019 Introduction to EJB 3

    75/79

    Lifecycle Callbacks for MessageDriven Beans

    Addition of a new instance to the

  • 7/31/2019 Introduction to EJB 3

    76/79

    Addition of a new instance to the

    Message-Driven Bean pool

  • 7/31/2019 Introduction to EJB 3

    77/79

    Servicing an onMessage(...) method

    Removing the Message-Driven Bean

  • 7/31/2019 Introduction to EJB 3

    78/79

    Removing the Message Driven Bean

    instance

  • 7/31/2019 Introduction to EJB 3

    79/79

    lifecycle event callbacks The following lifecycle event callbacks are supported for

    message-driven beans:

    PostConstruct PostConstruct callbacks occur before the first message

    listener method invocation on the bean. This is at a point afterwhich any dependency injection has been performed by the

    container. PostConstruct callback methods execute in an unspecified

    transaction and security context.

    PreDestroy PreDestroy callbacks occur at the time the bean is removed

    from the pool or destroyed.

    PreDestroy callback methods execute in an unspecifiedtransaction and security context.

    NOTE: PostActivate and PrePassivate callbacks, ifspecified, are ignored for message-driven beans.