Enterprise JavaBeans (EJB)alfuqaha/Fall10/CS5560/lectures/EJB3-Intro.pdfIf all of the business logic...

33
Enterprise JavaBeans (EJB)

Transcript of Enterprise JavaBeans (EJB)alfuqaha/Fall10/CS5560/lectures/EJB3-Intro.pdfIf all of the business logic...

Page 1: Enterprise JavaBeans (EJB)alfuqaha/Fall10/CS5560/lectures/EJB3-Intro.pdfIf all of the business logic operations and data manippgulations are done using EJBs, the JSPs and Servlets

Enterprise JavaBeans (EJB)

Page 2: Enterprise JavaBeans (EJB)alfuqaha/Fall10/CS5560/lectures/EJB3-Intro.pdfIf all of the business logic operations and data manippgulations are done using EJBs, the JSPs and Servlets

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

Page 3: Enterprise JavaBeans (EJB)alfuqaha/Fall10/CS5560/lectures/EJB3-Intro.pdfIf all of the business logic operations and data manippgulations are done using EJBs, the JSPs and Servlets

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

Page 4: Enterprise JavaBeans (EJB)alfuqaha/Fall10/CS5560/lectures/EJB3-Intro.pdfIf all of the business logic operations and data manippgulations are done using EJBs, the JSPs and Servlets

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

Page 5: Enterprise JavaBeans (EJB)alfuqaha/Fall10/CS5560/lectures/EJB3-Intro.pdfIf all of the business logic operations and data manippgulations are done using EJBs, the JSPs and Servlets
Page 6: Enterprise JavaBeans (EJB)alfuqaha/Fall10/CS5560/lectures/EJB3-Intro.pdfIf all of the business logic operations and data manippgulations are done using EJBs, the JSPs and Servlets

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

Page 7: Enterprise JavaBeans (EJB)alfuqaha/Fall10/CS5560/lectures/EJB3-Intro.pdfIf all of the business logic operations and data manippgulations are done using EJBs, the JSPs and Servlets

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)

Page 8: Enterprise JavaBeans (EJB)alfuqaha/Fall10/CS5560/lectures/EJB3-Intro.pdfIf all of the business logic operations and data manippgulations are done using EJBs, the JSPs and Servlets

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

Page 9: Enterprise JavaBeans (EJB)alfuqaha/Fall10/CS5560/lectures/EJB3-Intro.pdfIf all of the business logic operations and data manippgulations are done using EJBs, the JSPs and Servlets

EJBs & Multi-Tiered Architectures

AppletEJBs 3rd Party

EJBs

Web Page Servlets & JSPs Database

Page 10: Enterprise JavaBeans (EJB)alfuqaha/Fall10/CS5560/lectures/EJB3-Intro.pdfIf all of the business logic operations and data manippgulations are done using EJBs, the JSPs and Servlets

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

Page 11: Enterprise JavaBeans (EJB)alfuqaha/Fall10/CS5560/lectures/EJB3-Intro.pdfIf all of the business logic operations and data manippgulations are done using EJBs, the JSPs and Servlets

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)

Page 12: Enterprise JavaBeans (EJB)alfuqaha/Fall10/CS5560/lectures/EJB3-Intro.pdfIf all of the business logic operations and data manippgulations are done using EJBs, the JSPs and Servlets

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

Page 13: Enterprise JavaBeans (EJB)alfuqaha/Fall10/CS5560/lectures/EJB3-Intro.pdfIf all of the business logic operations and data manippgulations are done using EJBs, the JSPs and Servlets

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

Page 14: Enterprise JavaBeans (EJB)alfuqaha/Fall10/CS5560/lectures/EJB3-Intro.pdfIf all of the business logic operations and data manippgulations are done using EJBs, the JSPs and Servlets

EJB TypesEJB Types

JPA

Page 15: Enterprise JavaBeans (EJB)alfuqaha/Fall10/CS5560/lectures/EJB3-Intro.pdfIf all of the business logic operations and data manippgulations are done using EJBs, the JSPs and Servlets

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

Page 16: Enterprise JavaBeans (EJB)alfuqaha/Fall10/CS5560/lectures/EJB3-Intro.pdfIf all of the business logic operations and data manippgulations are done using EJBs, the JSPs and Servlets

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

Page 17: Enterprise JavaBeans (EJB)alfuqaha/Fall10/CS5560/lectures/EJB3-Intro.pdfIf all of the business logic operations and data manippgulations are done using EJBs, the JSPs and Servlets

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

Page 18: Enterprise JavaBeans (EJB)alfuqaha/Fall10/CS5560/lectures/EJB3-Intro.pdfIf all of the business logic operations and data manippgulations are done using EJBs, the JSPs and Servlets

Session and JPA EntitiesSession and JPA Entities

JPA

Page 19: Enterprise JavaBeans (EJB)alfuqaha/Fall10/CS5560/lectures/EJB3-Intro.pdfIf all of the business logic operations and data manippgulations are done using EJBs, the JSPs and Servlets

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

Page 20: Enterprise JavaBeans (EJB)alfuqaha/Fall10/CS5560/lectures/EJB3-Intro.pdfIf all of the business logic operations and data manippgulations are done using EJBs, the JSPs and Servlets

Example// EJB 3.0 Stateless Session Bean: Bean Class

@Statelesspublic class PayrollBean implements Payroll {

public void setTaxDeductions(int empId,int deductions) {

...}}

Page 21: Enterprise JavaBeans (EJB)alfuqaha/Fall10/CS5560/lectures/EJB3-Intro.pdfIf all of the business logic operations and data manippgulations are done using EJBs, the JSPs and Servlets

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

Page 22: Enterprise JavaBeans (EJB)alfuqaha/Fall10/CS5560/lectures/EJB3-Intro.pdfIf all of the business logic operations and data manippgulations are done using EJBs, the JSPs and Servlets

Example// EJB 3.0 Stateless Session Bean: Remote Interface

@Remotepublic interface Payroll {

public void setTaxDeductions(int empId, int deductions);

}

Page 23: Enterprise JavaBeans (EJB)alfuqaha/Fall10/CS5560/lectures/EJB3-Intro.pdfIf all of the business logic operations and data manippgulations are done using EJBs, the JSPs and Servlets

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) {

...}}

Page 24: Enterprise JavaBeans (EJB)alfuqaha/Fall10/CS5560/lectures/EJB3-Intro.pdfIf all of the business logic operations and data manippgulations are done using EJBs, the JSPs and Servlets

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

Page 25: Enterprise JavaBeans (EJB)alfuqaha/Fall10/CS5560/lectures/EJB3-Intro.pdfIf all of the business logic operations and data manippgulations are done using EJBs, the JSPs and Servlets

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

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

public void onMessage(Message msg) {...

}}

Page 26: Enterprise JavaBeans (EJB)alfuqaha/Fall10/CS5560/lectures/EJB3-Intro.pdfIf all of the business logic operations and data manippgulations are done using EJBs, the JSPs and Servlets

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);

Page 27: Enterprise JavaBeans (EJB)alfuqaha/Fall10/CS5560/lectures/EJB3-Intro.pdfIf all of the business logic operations and data manippgulations are done using EJBs, the JSPs and Servlets

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);}}}

Page 28: Enterprise JavaBeans (EJB)alfuqaha/Fall10/CS5560/lectures/EJB3-Intro.pdfIf all of the business logic operations and data manippgulations are done using EJBs, the JSPs and Servlets

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)

Page 29: Enterprise JavaBeans (EJB)alfuqaha/Fall10/CS5560/lectures/EJB3-Intro.pdfIf all of the business logic operations and data manippgulations are done using EJBs, the JSPs and Servlets

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

Page 30: Enterprise JavaBeans (EJB)alfuqaha/Fall10/CS5560/lectures/EJB3-Intro.pdfIf all of the business logic operations and data manippgulations are done using EJBs, the JSPs and Servlets

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) {

...}}

Page 31: Enterprise JavaBeans (EJB)alfuqaha/Fall10/CS5560/lectures/EJB3-Intro.pdfIf all of the business logic operations and data manippgulations are done using EJBs, the JSPs and Servlets

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) {

...}}

Page 32: Enterprise JavaBeans (EJB)alfuqaha/Fall10/CS5560/lectures/EJB3-Intro.pdfIf all of the business logic operations and data manippgulations are done using EJBs, the JSPs and Servlets

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();

}}...}

Page 33: Enterprise JavaBeans (EJB)alfuqaha/Fall10/CS5560/lectures/EJB3-Intro.pdfIf all of the business logic operations and data manippgulations are done using EJBs, the JSPs and Servlets

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