Praveen EJB Architecture

Embed Size (px)

Citation preview

  • 8/6/2019 Praveen EJB Architecture

    1/36

    EJB Architecture and Design

    R. Praveen.

  • 8/6/2019 Praveen EJB Architecture

    2/36

    What is EJB?

    An EJB is just a collection of Javaclasses and XML file, bundled into a

    single unit. The Java classes mustfollow certain rules and providecertain callback methods.

    EJB is just a specification. It is not aproduct.EJBs are reusable components.

  • 8/6/2019 Praveen EJB Architecture

    3/36

    What is EJB?

    EJB is a widely-adopted server-sidecomponent architecture for J2EE.

    EJB components are designed toencapsulate business logic, and toprotect the application developer

    from having to worry about systemlevel issues.

  • 8/6/2019 Praveen EJB Architecture

    4/36

    Contents

    Services provided by EJB containerCircumstances of EJB component usage

    How an EJB component looks like?View of an EJB component by clientprogrammer and EJB developerMechanisms by which EJB container providesits servicesRules an EJB developer must follow and how to use EJBs in a web architecture?

  • 8/6/2019 Praveen EJB Architecture

    5/36

  • 8/6/2019 Praveen EJB Architecture

    6/36

    Key features of EJBtechnology

    EJB architecture is inherently transactional, distributed,portable, multi-tier, scalable and secureEJB components are fully portable across any EJB

    server and any OS, work with any client.Components are declaratively customized

    There are four major parts to every bean: the homeinterface, the remote interface, the implementation

    class and the XML deployment descriptor

  • 8/6/2019 Praveen EJB Architecture

    7/36

    EJB vs JavaBeans

    The JavaBeans architecture is meant toprovide a format for general-purpose

    components whereas the EJBarchitecture provides a format forencapsulation and management of business logic.

    JavaBeans has tier of execution at Clientand EJB has at Server (specificallybusiness logic tier)

  • 8/6/2019 Praveen EJB Architecture

    8/36

    EJB vs JavaBeans

    In JavaBeans the runtime executionenvironment provides services like

    Java libraries, Java application etc. The EJB runtime environmentprovides services of Persistence,

    declarative transactions andsecurity, connection pooling andlifecycle services.

  • 8/6/2019 Praveen EJB Architecture

    9/36

    Varieties of Beans

    Session Beans Stateful session bean

    Stateless session beanEntity Beans

    With container-managed persistence

    With bean-managed persistenceMessage-Driven Beans

  • 8/6/2019 Praveen EJB Architecture

    10/36

    Why use EJBs in your design?

    EJB specification provides enterprise-levelservices, that is, it provides software servicesthat are fundamental to an organizationspurpose.EJBs API was designed to keep the applicationprogrammer from having to provide systems-level services, so that they are free toconcentrate on business logic.

  • 8/6/2019 Praveen EJB Architecture

    11/36

    Why use EJBs in your design?

    A requirement of any of the servicesprovided by an EJB container like

    transactions, scalability, persistence,security, future growth possibilities isan appropriate reason to use EJB in

    the design of the application.

  • 8/6/2019 Praveen EJB Architecture

    12/36

    EJB Architecture

    J2EE Application Server

    EJB Container

    Application Logic DataClient

    RDBMS

    Corba

    Mail

    RMI

    J DB C

    J a v a M a i l

    J M S

    J T ASession Bean

    Entity Bean

    Client

    Application

  • 8/6/2019 Praveen EJB Architecture

    13/36

    Roles in EJB Development

    EJB provider - a person who developsEJB Components

    EJB Deployer - a person responsiblefor deploying EJBs in EJB serverApplication Server/ EJB Container

    Vendor - one who providesapplication server on which theapplication is deployed

  • 8/6/2019 Praveen EJB Architecture

    14/36

    Roles in EJB Development

    Application assembler - one whocombine the EJB components with

    other software to make a completeapplicationSystem administrator - one who

    manages the application after it hasbeen deployed into a targetenvironment.

  • 8/6/2019 Praveen EJB Architecture

    15/36

    Roles in EJB Development

    EJBProvider

    ApplicationAssembler

    App Server/EJB Container

    Provider

    Deployer

    SystemAdministrator

  • 8/6/2019 Praveen EJB Architecture

    16/36

    EJB Container and its Services

    A container is an execution environmentfor a component. The component lives in

    the container and the container providesthe services for the component.Similarly, a container lives in anapplication server, which provides anexecution environment for it and othercontainers.

  • 8/6/2019 Praveen EJB Architecture

    17/36

    Services provided by an EJB container

    Persistence Ex: simple connection pooling,

    automatic persistence, etc. EJBscreated with applicationdevelopment tools will encapsulate

    data access in components.

  • 8/6/2019 Praveen EJB Architecture

    18/36

    Services provided by an EJB container

    Declarative transactionsData caching

    Declarative SecurityError HandlingComponent Framework for Business Logic

    Scalability and Fall-OverPortabilityManageability

  • 8/6/2019 Praveen EJB Architecture

    19/36

    How the Container Provides Services

    There are three basic ideas:First, there are clearly defined responsibilities between thevarious parts of an application using EJB component namely theclient, the EJB container and the EJB component. The definition of

    these responsibilities is formally known as a contract .Second, the services that the container provides are defined insuch a way that they are orthogonal to the component. In otherwords, security, persistence, transactions are separate from the

    Java files that implement the business logic of the component.

  • 8/6/2019 Praveen EJB Architecture

    20/36

    How the Container Provides Services

    Third, the container interposes oneach and every call to an EJB

    component so that it can provide itsservices. In other words, thecontainer puts itself between the

    client and the component on everysingle business method call.

  • 8/6/2019 Praveen EJB Architecture

    21/36

    Contracts

    EJB Container/Application

    Server

    Enterprise JavaBean

    Client

  • 8/6/2019 Praveen EJB Architecture

    22/36

    Rules for the bean programmer

    The developer of the EJB component must implementthe business methods in the implementation class

    The bean provider must implement the ejbCreate(),

    ejbPostCreate(),ejbRemove() methods and theejbFind() methods if the bean is an entitywith bean managed persistence

    The bean provider must define the enterprise beanshome and remote interfaces

    For session beans, the bean provider must implementthe container callbacks defined in the

    javax.ejb.SessionBean interface

  • 8/6/2019 Praveen EJB Architecture

    23/36

  • 8/6/2019 Praveen EJB Architecture

    24/36

    EJB Container from a remoteclient

    First, the client makes a call on the RMIstub

    This RMI stub interposes on the methodcall in order to marshal parameters andsend the information across the networkA skeleton on the server side unmarshalsthe parameters and delivers them to theEJB Container

  • 8/6/2019 Praveen EJB Architecture

    25/36

    Interposition diagram

    Interposition class

    ClientContainer generated

    classEJB

    RMIStub

    RMIStub

    Network

  • 8/6/2019 Praveen EJB Architecture

    26/36

    Interposition : from EJBContainer to EJBs

    The container will examine the security credentials of thecaller of the methodIt will start or join with any required transactions

    It will make any necessary calls to persistence functionsIt will trigger various callbacks to allow the EJB Componentto acquire resourcesOnly after all this is done will the actual business methodbe called

    Once it is called, the container will do some more workwith transactions, persistence, callbacks and returns dataor exception to the remote client

  • 8/6/2019 Praveen EJB Architecture

    27/36

    Working with EJBs

    The Enterprise JavaBeansspecification is written for three

    audiences: The Client developer The EJB developer

    The EJB container developer

  • 8/6/2019 Praveen EJB Architecture

    28/36

    EJB Clients

    EJB Clients are applications that access EJBcomponents in EJB containers. There are twopossible types. The first category is application

    clients which are stand-alone applicationsaccessing the EJB components using the RMI-IIOPprotocol. The second category of applicationclients are components in the web container.

    They are java servlets and JSPs which also accessthe EJB components via the RMI-IIOP protocol.

  • 8/6/2019 Praveen EJB Architecture

    29/36

    The Client DevelopersView

    The client has a smaller set of concerns then abean developer with regard to using EJBs.Basically, he need to know :

    how to find or create a bean, how to use its methods and how to release its resources

    The client need not worry about theimplementation of the EJB, callbacks that the EJBcontainer will make on the EJB or nature of theservices provided to the EJB.

  • 8/6/2019 Praveen EJB Architecture

    30/36

    EJBs interface

    Home Interface : It is primarily for the life cycleoperations of the bean: creating, finding, andremoving EJBs. The home interface is not

    associated with a particular bean, just with a typeof bean.Remote Interface : It is for business methods.Logically, it represents a particular bean on the

    server. The remote interface also provides someinfrastructure methods associated with a beaninstance, rather than a bean type.

  • 8/6/2019 Praveen EJB Architecture

    31/36

    Sample client applicationpseudo code

    A client programmer will acquire an EJBshome interface through JNDI, and they usethis home interface to :

    Create or findinstance of bean

    Execute methods Reference(Handle)

    Remove bean

  • 8/6/2019 Praveen EJB Architecture

    32/36

    The Bean Programmersview

    Main responsibility is write business logic and structurethe code in a particular structure. The structure has 4files, the home interface, remote interface, business

    logic class file and the XML file. The XML file called thedeployment descriptor, contains the structuralinformation about the bean, declares the beansexternal dependencies and specifies certaininformation about how services such as transaction and

    security work.

  • 8/6/2019 Praveen EJB Architecture

    33/36

    Structure of JAR file

    META -INF\ ejb-jar.xml

    orderMgmt\ OrderManagement.class OrderManagementHome.class

    OrderManagementBean.class

  • 8/6/2019 Praveen EJB Architecture

    34/36

    What you cant do in an EJBcomponent?

    You cannot use Reflection API to accessinformation inaccessible to you.

    You cannot create a class loader orreplace a security manager. You cannot set the socket factory used byServerSocket or Socket

    You cannot use the object substitutionfeatures of the serialization protocol

  • 8/6/2019 Praveen EJB Architecture

    35/36

    What you cant do in an EJBcomponent?

    use Threads or the Threading APIuse the AWTAct as a Network Server

    use Read/Write static fieldsuse java.io packageLoad a native library

    use this as an Argument or Return valueuse Loopback Calls

  • 8/6/2019 Praveen EJB Architecture

    36/36

    EJB Components on theWeb

    Three classes of objects in MVC architecture:Model : This is the data and business-logiccomponent. It can serve multiple views.View : This is the presentation component orthe user-interface component. There can bedifferent presentations of a single model.Controller : This is the component that respondsto user input. Translates user-interface eventsinto changes to the model and defines the waythe user-interface reacts to those events.