EJB Introduction for All New Users

download EJB Introduction for All New Users

of 62

Transcript of EJB Introduction for All New Users

  • 8/10/2019 EJB Introduction for All New Users

    1/62

    EJB Architecture and Design

    CS486

    Global Knowledge NetworksInstructor : Dr. V. Juggy

    Presentation by:Aravind Vinnakota

  • 8/10/2019 EJB Introduction for All New Users

    2/62

    What is EJB?

    An EJB is just a collection of Java classesand XML file, bundled into a single unit.

    The Java classes must follow certain rulesand provide certain callback methods.

    EJB is just a specification. It is not a

    product.EJBs are reusable components.

  • 8/10/2019 EJB Introduction for All New Users

    3/62

    What is EJB?

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

    EJB components are designed toencapsulate business logic, and to protectthe application developer from having to

    worry about system level issues.

  • 8/10/2019 EJB Introduction for All New Users

    4/62

    Contents

    Services provided by EJB container

    Circumstances of EJB component usage

    How an EJB component looks like?View of an EJB component by client

    programmer and EJB developer

    Mechanisms by which EJB container provides itsservices

    Rules an EJB developer must followand howtouse EJBs in a web architecture?

  • 8/10/2019 EJB Introduction for All New Users

    5/62

    Key features of EJB technology

    EJB components are server-side componentswritten entirely in the Java programminglanguage

    EJB components contain business logic only - no

    System-level programming

    System-level services (i.e. "plumbing") such astransactions, security, Life-cycle, threading,persistence, etc. are automatically managed for

    the EJB component by the EJB server

  • 8/10/2019 EJB Introduction for All New Users

    6/62

    Key features of EJB technology

    EJB architecture is inherently transactional,distributed, portable, multi-tier, scalable andsecure

    EJB components are fully portable across anyEJB server and any OS, work with any client.

    Components are declaratively customized

    There are four major parts to every bean: thehome interface, the remote interface, theimplementation class and the XML deployment

    descriptor

  • 8/10/2019 EJB Introduction for All New Users

    7/62

    EJB vs JavaBeans

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

    components whereas the EJB architectureprovides a format for encapsulation andmanagement of business logic.

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

  • 8/10/2019 EJB Introduction for All New Users

    8/62

    EJB vs JavaBeans

    In JavaBeans the runtime executionenvironment provides services like Java

    libraries, Java application etc. The EJBruntime environment provides services ofPersistence, declarative transactions and

    security, connection pooling and lifecycleservices.

  • 8/10/2019 EJB Introduction for All New Users

    9/62

    Varieties of Beans

    Session Beans

    Stateful session bean

    Stateless session bean

    Entity Beans

    With container-managed persistence

    With bean-managed persistence

    Message-Driven Beans

  • 8/10/2019 EJB Introduction for All New Users

    10/62

    Why use EJBs in your design?

    EJB specification provides enterprise-levelservices, that is, it provides software

    services that are fundamental to anorganizations purpose.

    EJBs API was designed to keep the

    application programmer from having toprovide systems-level services, so thatthey are free to concentrate on businesslogic.

  • 8/10/2019 EJB Introduction for All New Users

    11/62

    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 is anappropriate reason to use EJB in the

    design of the application.

  • 8/10/2019 EJB Introduction for All New Users

    12/62

    EJB Architecture

    J2EE Application Server

    EJB Container

    Application Logic DataClient

    RDBMS

    Corba

    Mail

    RMI

    JDBC

    Java

    Mail

    JMS

    JTA

    Session Bean

    Entity Bean

    Client

    Application

  • 8/10/2019 EJB Introduction for All New Users

    13/62

    Roles in EJB Development

    EJB provider - a person who developsEJB Components

    EJB Deployer - a person responsible fordeploying EJBs in EJB server

    Application Server/ EJB Container Vendor

    - one who provides application server onwhich the application is deployed

  • 8/10/2019 EJB Introduction for All New Users

    14/62

    Roles in EJB Development

    Application assembler - one who combinethe EJB components with other software

    to make a complete applicationSystem administrator - one who manages

    the application after it has been deployed

    into a target environment.

  • 8/10/2019 EJB Introduction for All New Users

    15/62

    Roles in EJB Development

    EJB

    Provider

    Application

    Assembler

    App Server/

    EJB Container

    Provider

    Deployer

    System

    Administrator

  • 8/10/2019 EJB Introduction for All New Users

    16/62

    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 an

    application server, which provides anexecution environment for it and othercontainers.

  • 8/10/2019 EJB Introduction for All New Users

    17/62

    Services provided by an EJB container

    Persistence

    Ex: simple connection pooling, automatic

    persistence, etc. EJBs created withapplication development tools willencapsulate data access in components.

  • 8/10/2019 EJB Introduction for All New Users

    18/62

    Services provided by an EJB container

    Declarative transactions

    Data caching

    Declarative Security

    Error Handling

    Component Framework for Business Logic

    Scalability and Fall-Over

    Portability

    Manageability

  • 8/10/2019 EJB Introduction for All New Users

    19/62

    How the Container Provides Services

    There are three basic ideas:

    First, there are clearly defined responsibilities betweenthe various parts of an application using EJB component

    namely the client, the EJB container and the EJBcomponent. The definition of these responsibilities isformally known as a contract.

    Second, the servicesthat the container provides are

    defined in such a way that they are orthogonalto thecomponent. In other words, security, persistence,transactions are separate from the Java files thatimplement the business logic of the component.

  • 8/10/2019 EJB Introduction for All New Users

    20/62

    How the Container Provides Services

    Third, the container interposes on eachand every call to an EJB component so

    that it can provide its services. In otherwords, the container puts itself betweenthe client and the component on every

    single business method call.

  • 8/10/2019 EJB Introduction for All New Users

    21/62

    Contracts

    EJB Container/Application

    Server

    Enterprise JavaBean

    Client

  • 8/10/2019 EJB Introduction for All New Users

    22/62

    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 beans

    homeand remoteinterfaces For session beans, the bean provider must implement

    the container callbacks defined in thejavax.ejb.SessionBeaninterface

  • 8/10/2019 EJB Introduction for All New Users

    23/62

    Rules for the bean programmer

    For entity beans, the provider mustimplement the container callbacks defined

    in the javax.ejb.EntityBean interfaceThe bean provider must not use

    programming practices that would

    interfere with the containers runtimemanagement of the enterprise beaninstances

  • 8/10/2019 EJB Introduction for All New Users

    24/62

    Interposition : method call to an EJBContainer from a remote client

    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 network

    A skeleton on the server side unmarshalsthe parameters and delivers them to theEJB Container

  • 8/10/2019 EJB Introduction for All New Users

    25/62

    Interposition diagram

    Interposition class

    ClientContainer

    generated

    classEJB

    RMI

    StubRMI

    StubNetwork

  • 8/10/2019 EJB Introduction for All New Users

    26/62

    Interposition : from EJBContainer to EJBs

    The container will examine the security credentials ofthe caller of the method

    It will start or join with any required transactions

    It will make any necessary calls to persistence functions

    It will trigger various callbacks to allow the EJBComponent to acquire resources

    Only after all this is done will the actual business

    method be called

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

  • 8/10/2019 EJB Introduction for All New Users

    27/62

    Working with EJBs

    The Enterprise JavaBeans specification iswritten for three audiences:

    The Client developerThe EJB developer

    The EJB container developer

  • 8/10/2019 EJB Introduction for All New Users

    28/62

    EJB Clients

    EJB Clients are applications that access EJBcomponents in EJB containers. There are twopossible types. The first category is applicationclients which are stand-alone applicationsaccessing the EJB components using the RMI-IIOP protocol. The second category of

    application clients are components in the webcontainer. They are java servlets and JSPswhich also access the EJB components via the

    RMI-IIOP protocol.

  • 8/10/2019 EJB Introduction for All New Users

    29/62

    The Client Developers View

    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 theEJB container will make on the EJB or nature of

    the services provided to the EJB.

  • 8/10/2019 EJB Introduction for All New Users

    30/62

    EJBs interface

    Home Interface: It is primarily for the life cycleoperations of the bean: creating, finding, andremoving EJBs. The home interface is notassociated with a particular bean, just with atype of bean.

    Remote Interface: It is for business methods.

    Logically, it represents a particular bean on theserver. The remote interface also provides someinfrastructure methods associated with a beaninstance, rather than a bean type.

  • 8/10/2019 EJB Introduction for All New Users

    31/62

    Sample client applicationpseudo code

    A client programmer will acquire an EJBs homeinterface through JNDI, and they use this homeinterface to :

    Create or find

    instance of bean

    Execute methods Reference

    (Handle)Remove bean

  • 8/10/2019 EJB Introduction for All New Users

    32/62

    Client.java

    Package orderMgmt;

    import java.util.properties;

    import java.naming.Context; // for name-to-object findings

    import java.naming.InitialContext;// context for naming operationspublic class Client {

    try {

    Properties prop = new Properties();

    // server dependent properties for InitialContext

    prop.put(Context.INITIAL_CONTEXT_FACTORY,org.jnp.interfaces.NamingContextFactory);

    prop.put(Context.PROVIDER_URL, localhost:1099);

    Context ctx = new InitialContext(prop);

    Object objref = ctx.lookup(OrderManagement);

  • 8/10/2019 EJB Introduction for All New Users

    33/62

    Client contd..

    // casting home interface reference to the OrderManagementHome

    OrderManagementHome home = (OrderManagementHome)javax.rmi.PortableRemoteObject.narrow(objref,OrderManagementHome.class);

    // home interface to create an instance of the OrderManagement

    OrderManagement orderManagement = home.create();

    // calling placeOrder()

    orderManagement.placeOrder("Dan OConnor",

    "Wrox books on programming", 1000);orderManagement.remove();

    System.out.println("Order successfully placed.");

    } catch (Exception e) { e.printStackTrace(); }

    } }

  • 8/10/2019 EJB Introduction for All New Users

    34/62

    The Bean Programmers view

    Main responsibility is write business logic andstructure the code in a particular structure. Thestructure has 4 files, the home interface, remoteinterface, business logic class file and the XML

    file.The XML file called the deploymentdescriptor, contains the structural information

    about the bean, declares the beans externaldependencies and specifies certain informationabout how services such as transaction and

    security work.

  • 8/10/2019 EJB Introduction for All New Users

    35/62

    Interface EJBObject

    package javax.ejb;

    public interface javax.ejb.EJBObject extends java.rmi.Remote {

    EJBHome getEJBHome() throws java.rmi.RemoteException;

    Handle getHandle() throwsjava.rmi.RemoteException;Object getPrimaryKey() throws java.rmi.RemoteException;

    boolean isIdentical(EJBObject obj) throwsjava.rmi.RemoteException;

    void remove() throws java.rmi.RemoteException;

    }

  • 8/10/2019 EJB Introduction for All New Users

    36/62

    OrderManagement code..

    package orderMgmt;

    import javax.ejb.*;

    public interface OrderManagement extends javax.ejb.EJBObject {

    public void placeOrder(String custName, String prodName, int

    quantity)throws java.rmi.RemoteException;

    public void cancelOrder(String custName, String prodName) throws java.rmi.RemoteException;

    public boolean isShipped(StringcustName, String prodName)throws java.rmi.RemoteException;

    }

  • 8/10/2019 EJB Introduction for All New Users

    37/62

    OrderManagementBean code..

    package orderMgmt;

    import javax.ejb.*;

    public class OrderManagementEJB implementsjavax.ejb.SessionBean

    {

    public void placeOrder(String custName, String prodName, int

    quantity) { // ... Business logic ...}

    public void cancelOrder(String custName, String prodName)

    { // ... Business logic ...}

    public boolean isShipped(String custName, String prodName)

    { // ... Business logic return true; }

  • 8/10/2019 EJB Introduction for All New Users

    38/62

    OrderManagementBean code..

    public void ejbCreate()

    { // Can be empty }

    public void ejbRemove()

    { // Can be empty }

    public void ejbActivate()

    { // Can be empty}

    public void ejbPassivate()

    { // Can be empty}

    public void setSessionContext( SessionContext ctx )

    { // Can be empty}

    }

  • 8/10/2019 EJB Introduction for All New Users

    39/62

  • 8/10/2019 EJB Introduction for All New Users

    40/62

    OrderManagementHome code..

    package orderMgmt;

    import javax.ejb.*;

    public interface OrderManagementHome extendsjavax.ejb.EJBHome

    {

    public OrderManagement create()

    throws java.rmi.RemoteException, javax.ejb.CreateException;

    }

  • 8/10/2019 EJB Introduction for All New Users

    41/62

    The xml file : ejb-jar.xml

    OrderManagement

    orderMgmt.OrderManagementHome

    orderMgmt.OrderManagement

    orderMgmt.OrderManagementBean

    Stateless

    Container

  • 8/10/2019 EJB Introduction for All New Users

    42/62

    The xml file : ejb-jar.xml

    OrderManagement*

    Required

  • 8/10/2019 EJB Introduction for All New Users

    43/62

    Structure of JAR file

    META -INF\

    ejb-jar.xml

    orderMgmt\OrderManagement.class

    OrderManagementHome.class

    OrderManagementBean.class

  • 8/10/2019 EJB Introduction for All New Users

    44/62

    What you cant do in an EJB component?

    You cannot use Reflection API to accessinformation inaccessible to you.

    You cannot create a class loader or replace asecurity manager.

    You cannot set the socket factory used byServerSocket or Socket

    You cannot use the object substitution featuresof the serialization protocol

  • 8/10/2019 EJB Introduction for All New Users

    45/62

  • 8/10/2019 EJB Introduction for All New Users

    46/62

    EJB Components on the Web

    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.

  • 8/10/2019 EJB Introduction for All New Users

    47/62

    Implementation of MVC in a web site

    Controller

    Model

    Browser

    Client

    view1.jsp

    view2.jsp

    view3.jsp

    Main.jsp

    Views1

    2

    3

    4

    5

  • 8/10/2019 EJB Introduction for All New Users

    48/62

    Design of the EJB Tier

    UML use cases: UML is the UnifiedModeling Language, the standard

    language for expressing the model of thesoftware system that we intend to build.

    Use cases are subset of UML that

    expresses the functionality of the softwareto be delivered. Use cases describe whatto do, but not how to do it.

  • 8/10/2019 EJB Introduction for All New Users

    49/62

    Analysis Objects

    Interface Objects : The interface object isresponsible for controlling access to the

    EJB tier from any client. An interfaceobject should always be represented by asession bean in the implementation.

    Ex : controller servlet for the webapplications model-view-controllerarchitecture.

  • 8/10/2019 EJB Introduction for All New Users

    50/62

    Control Objects

    Control objects provide services to theapplication. They model functionality that

    is not naturally associated with aparticular entity or interface. Controlobjects should be represented by sessionbeans in the implementation.

  • 8/10/2019 EJB Introduction for All New Users

    51/62

    Entity Objects

    Entity objects model those businessobjects that should maintain their state

    after the use case completes. This meansthey represent data in the database.Entity beans are often represented byentity beans in the implementation model.

  • 8/10/2019 EJB Introduction for All New Users

    52/62

    An Example of EJB Design

    Consider the case of a company that developsproducts, takes orders for those products, andthen manufactures and ships them.

    Actors in the company : An engineer, a webcustomer, a phone operator who takes ordersfrom a catalog, floor manager who manages the

    manufacturing process, a crew member thatactually builds the product ordered and a

    manager who tracks overdue orders.

  • 8/10/2019 EJB Introduction for All New Users

    53/62

    Use Cases

    Create a Product

    Place an Order

    Cancel an OrderSelect an Order for Manufacture

    Build a Product

    Ship an Order

    List Overdue Orders

  • 8/10/2019 EJB Introduction for All New Users

    54/62

    Use case diagram from analysis

    Engineer

    Customer

    Operator

    Manager

    Crew

    Mment

    Create Product

    Place Order

    Cancel Order

    Select Order

    Build Product

    Ship an Order

    Overdue Orders

  • 8/10/2019 EJB Introduction for All New Users

    55/62

    Stereotype icons in UML

    Interface Object :

    Entity Object :

    Control Object :

  • 8/10/2019 EJB Introduction for All New Users

    56/62

    Translation of analysis model into implementation

    Actor User Interface Type Interface Object Implation

    Engineer Visual Basic Session Bean (RMI/IIOP)

    Customer Web Application JavaBean proxy / S Bean

    Operator Swing GUI Session Bean

    Manager Web Application JavaBean proxy / S Bean

    Crew Palm Pilot XHTML Servelet to Session BeanManagement Web Application JavaBean proxy / S Bean

    View of use case actors and their

  • 8/10/2019 EJB Introduction for All New Users

    57/62

    View of use case actors and theirrespective interface objects

    Engineer

    Customer

    Operator

    Manager

    Crew

    Manage-

    -ment

    VB App

    Web App

    Swing app

    Web App

    Web App

    Palm App

    View of interaction of interface

  • 8/10/2019 EJB Introduction for All New Users

    58/62

    View of interaction of interfaceand control objects

    Ship

    order

    Build

    Product

    Select

    Cancel

    Place

    Create

    List

    Overdue

    VB

    App

    Web

    App

    Web

    App

    Web

    App

    Swing

    App

    Palm

    App

    Vi f i t ti b t t l

  • 8/10/2019 EJB Introduction for All New Users

    59/62

    View of interaction between controlobjects and entity objects

    RoutingProduct

    Order

    Shipment

    Account

    Supplier

    Shipping

    Company

    Customer

    Create

    Product

    Place

    Order

    Cancel

    Order

    Select for

    Manufacture

    Build

    Product

    Ship

    Order

    List

    Overdue

  • 8/10/2019 EJB Introduction for All New Users

    60/62

    Summary

    EJBs are intended for transactional systems

    EJBs are portable, reusable server-sidecomponents that execute in a container

    Assist developer productivity, extend applicationcapability, and improve system stability

    Are accessible from many different types of

    clientsThere are three types of beans : stateful

    session, stateless session, and entity

  • 8/10/2019 EJB Introduction for All New Users

    61/62

    Summary

    There are four major parts to every bean: thehome interface, the remote interface, theimplementation class, and the XML deployment

    descriptor

    The enterprise bean developer must followcertain rules to get the benefits of EJB

    technologyThe roles of EJBs can be understood by

    analyzing a model of your enterprise in terms ofinterface, control and entity objects

  • 8/10/2019 EJB Introduction for All New Users

    62/62