+ All Categories
Home > Documents > Enterprise JavaBeans (EJB) - wmich.edualfuqaha/Fall12/cs5560/lectures/EJB3-Intro.pdf · EJB as...

Enterprise JavaBeans (EJB) - wmich.edualfuqaha/Fall12/cs5560/lectures/EJB3-Intro.pdf · EJB as...

Date post: 21-Jan-2019
Category:
Upload: buiphuc
View: 221 times
Download: 0 times
Share this document with a friend
33
Enterprise JavaBeans (EJB)
Transcript

Enterprise JavaBeans (EJB)

What are EJBs?What are EJBs?

They are components that can be connected to form a system

They represent behavior or business logicJPA Entities are used to persist data

They are typically used in the server tierEJBs can interact with other EJBs

Advantages of EJBsAdvantages of EJBsEJBs are reusable components

Can be reused in different parts of the systemp yCan be packaged into libraries and sold

EJBs Can be combined visually using development IDEsE.g. Visual Age, Visual Cafég g ,

EJBs provide convenient abstractions so it do not requireyou to write:

Multi-threaded, multiple access codeNetwork communication code (i.e. it uses RMI) for client/server communicationNetwork communication code for EJB to EJB communicationTransaction management codeTransaction management code

EJBs from different businesses can interact easilyThis is because of their well defined interfacesThis is because of their well-defined interfaces

EJB in the Big Picture of Java EEEJB in the Big Picture of Java EE

EJB CommunicationEJB Communication

EJBs use IIOP as the wire protocolTherefore, EJBs are compatible with RMI (i.e.,Therefore, EJBs are compatible with RMI (i.e., RMI over IIOP) and CORBA librariesThus, you could write an EJB client in any , y ylanguage that supports CORBA

EJB as Client/Server MiddlewareEJB as Client/Server Middleware

Think of EJBs as just another Client/Server jmiddleware like RMI, CORBA, or Web Services

The EJB instance itself is the serverEJBs have clients (objects that call its methods)

One complication is that EJB clients can be:Java Applets and Applications (using RMI or CORBA)Non-Java applications (using CORBA)JSP d S l t ( i RMI CORBA)JSPs and Servlets (using RMI or CORBA)Other EJBs (using RMI or CORBA)

EJBs & 3-Tiered ArchitecturesIn enterprise systems, EJB clients are

ll S l t JSP Oth EJBClient Tier Server Tier Database Tier

usually: Servlets, JSPs, or Other EJBs

Applet EJBs

Database

Web Page Servlets & JSPs

EJBs & Multi-Tiered Architectures

AppletEJBs 3rd Party

EJBs

Web Page Servlets & JSPs Database

How EJBs Change Thingsg g

EJBs are most suitable for developing business logic and data manip lationlogic and data manipulation

If all of the business logic operations and data manipulations are done using EJBs, the JSPs and p gServlets will be focused mainly on displaying the results of those operations

Session EJBs: Used to represent system behavior (i eSession EJBs: Used to represent system behavior (i.e. business logic)

e.g. Storing products to purchase in the shopping cartJPA Entities: Used to represent & manipulate systemJPA Entities: Used to represent & manipulate system data

e.g. Finding products that match a search term

Application ServersApplication Servers

Containers where EJBs (and JSPs and servlets) are executedProvide EJB and JPA functionality, including:

P i t th h d t b ( i JDBC)Persistence through databases (using JDBC)Transactions (using Java Transaction Service)

Can provide advanced features including:Can provide advanced features, including:Load balancingDatabase connection poolingp g

Here are the major application servers:SJS AP, WebLogic (BEA), Internet Application S iAS (O l ) W bS h (IBM)Server or iAS (Oracle), WebSphere (IBM)

Alternatives to EJBsAlternatives to EJBs

Web Services are one of the technologies gcompeting with EJBs

Web services use the SOAP protocol to exchange information with some server

SOAP uses an XML format to exchange request and response information via HTTPresponse information via HTTPDue to SOAP's well-defined protocol, Web Services can be used to exchange information between businesses (B2B)

W b i id t th d th tWeb services provide one or more remote method that can be accessed easily from other applications

Alternatives to EJBsAlternatives to EJBsCORBA objects provide some functionality similar t EJBto EJBs:

Persistence (of CORBA object data)Transactions (between CORBA objects)Transactions (between CORBA objects)Security (between CORBA objects)

CORBA and EJBs are closely related, in fact, they use the same wire protocol:use the same wire protocol:

IIOPIn some sense, EJBs can be considered to be an ,enhanced version of CORBA

Except that EJBs can only be created in Java

EJB TypesEJB Types

JPA

Types of Enterprise BeansTypes of Enterprise Beans

Session beans:Also called business process objectsThey represent the business logic of the systemTheir lifetime is usually an entire sessionTheir lifetime is usually an entire session

When a session is done, the session bean expiresi.e. Session bean instances exist as long as a specific user is using the systemuser is using the system

JPA Entities:Al ll d b i d bjAlso called business data objectsThey represent persistent data

Often the data persistence is managed through a g gdatabase, using JDBC

Subtypes of Session BeansSubtypes of Session Beans

Stateful:Used for operations that require multiple requests to be completedMaintain data between requests

Stateless:Used for operations that can be performed in

i l ta single requestDo not maintain persistent data between subsequent requests from a given clientsubsequent requests from a given client

J EE A li ti M d lJava EE Application Model

J2EE is a multitiered distributed application modelclient machinesth J2EE hithe J2EE server machinethe database or legacy machines at the back end

Session and JPA EntitiesSession and JPA Entities

JPA

Bean ClassesBean ClassesIn EJB 3.0, session beans, message-d i b di Jdriven beans are ordinary Java classes

Bean type specified by annotation or XML

Annotations@Stateless, @Stateful, @MessageDrivenSpecified on bean class

Example// EJB 3.0 Stateless Session Bean: Bean Class

@Statelesspublic class PayrollBean implements Payroll {

public void setTaxDeductions(int empId,int deductions) {

...}}

Business InterfacesBusiness InterfacesPlain Java language interface

EJBObj t EJBH i t f i t dEJBObject, EJBHome interface requirements removed

Either local or remote accessLocal by defaultLocal by defaultRemote by annotation or deployment descriptorRemote methods not required to throwRemote methods not required to throw RemoteException

Bean class can implement its interfaceAnnotations: @Remote, @Local

Can specify on bean class or interface

Example// EJB 3.0 Stateless Session Bean: Remote Interface

@Remotepublic interface Payroll {

public void setTaxDeductions(int empId, int deductions);

}

Example// EJB 3.0 Stateless Session Bean: //// Alternative: Remote Interface specified on bean class

@Stateless @Remotepublic class PayrollBean

implements Payroll {

public void setTaxDeductions(int empId,int deductions) {

...}}

Message Driven BeansMessage Driven Beans

Message listener interface is business interface

Bean class implements it or designates with @MessageListener

No requirement to implement other interfacesAnnotations

@MessageDriven@MessageDriven

ExampleExample// EJB 3.0 Message-driven bean: Bean Class

@MessageDriven public class PayrollMDB implements javax.jms.MessageListener {

public void onMessage(Message msg) {...

}}

Simplification of Client ViewSimplification of Client View// Use Dependency Injection

// EJB 3.0: Client View

@EJB Payroll payroll;

// Use the bean

payroll.setTaxDeductions(1234, 3);

Dependency Injection ExampleDependency Injection Example// EJB 3.0 Stateless Session Bean: Bean Class// D t i i j ti d J P i t API// Data access using injection and Java Persistence API

@Stateless public class PayrollBean implements Payroll {

@PersistenceContext EntityManager payrollMgr;@PersistenceContext EntityManager payrollMgr;

public void setTaxDeductions(int empId,int deductions) {payrollMgr.find(Employee.class,

empId).setTaxDeductions(deductions);}}}

TransactionsTransaction Demarcation Types

Container-managed transactionsSpecify declaratively

Bean-managed transactionsUserTransaction API

Container-managed transaction d tidemarcation is defaultAnnotation: @TransactionManagementAnnotation: @TransactionManagement

Values: CONTAINER (default) or BEANAnnotation is applied to bean class (orAnnotation is applied to bean class (or superclass)

Container Managed TransactionsContainer Managed TransactionsTransaction Attributes

Annotations are applied to bean class and/or methods of bean class

Annotations applied to bean class apply to all methods of bean class unless overridden at method-levelAnnotations applied to method apply to method only

Annotation: @TransactionAttribute@Values: REQUIRED (default), REQUIRES_NEW, MANDATORY, _NEVER, NOT_SUPPORTED, SUPPORTS

ExampleExample// EJB 3.0: Container-managed transactions

@Stateless public class PayrollBean implements Payroll {

@TransactionAttribute(MANDATORY)public void setTaxDeductions(int empId,int deductions) {

...}

public int getTaxDeductions(int empId) {

...}}

Example// EJB 3.0: Container-managed transactions

@TransactionAttribute(MANDATORY) @Stateless public class PayrollBean implements Payroll {

public void setTaxDeductions(int empId,int deductions) {

...}

@TransactionAttribute(REQUIRED) public int getTaxDeductions(int empId) {

...}}

ExampleExample// EJB 3.0: Bean-managed transactions

@TransactionManagement(BEAN) @Stateless public class PayrollBean implements Payroll {

@R U T ti t@Resource UserTransaction utx;@PersistenceContext EntityManager payrollMgr;

public void setTaxDeductions(int empId, int deductions) {utx begin();utx.begin();payrollMgr.find(Employee.class,

empId).setDeductions(deductions);utx.commit();

}}...}

ReferencesReferencesDeveloping Enterprise Applications Using the J2EE Platform, http://java.sun.com/developer/onlineTraining/J2EE/Intro2/j2ee.htmlp j p g j

Sang Shin, EJB Overview, http://www.javapassion.com/j2ee


Recommended