37
1 Architectural Overview Architectural Overview For application developers, assembling For application developers, assembling enterprise beans requires little or no enterprise beans requires little or no expertise in the complex system-level expertise in the complex system-level issues that often plague three-tier issues that often plague three-tier development efforts. development efforts. While EJB makes the process easier for While EJB makes the process easier for application developers, it also provides EJB application developers, it also provides EJB server developers with a great deal of server developers with a great deal of flexibility in how they support the EJB flexibility in how they support the EJB specification. specification.

Architectural Overview

  • Upload
    tejano

  • View
    33

  • Download
    0

Embed Size (px)

DESCRIPTION

Architectural Overview. For application developers, assembling enterprise beans requires little or no expertise in the complex system-level issues that often plague three-tier development efforts. - PowerPoint PPT Presentation

Citation preview

Page 1: Architectural Overview

11

Architectural OverviewArchitectural Overview

For application developers, assembling For application developers, assembling enterprise beans requires little or no enterprise beans requires little or no expertise in the complex system-level expertise in the complex system-level issues that often plague three-tier issues that often plague three-tier development efforts. development efforts.

While EJB makes the process easier for While EJB makes the process easier for application developers, it also provides application developers, it also provides EJB server developers with a great deal of EJB server developers with a great deal of flexibility in how they support the EJB flexibility in how they support the EJB specification. specification.

Page 2: Architectural Overview

22

Architectural OverviewArchitectural Overview

Java Persistence also makes it fairly easy Java Persistence also makes it fairly easy for developers to write objects that can be for developers to write objects that can be persisted to a relational database. persisted to a relational database.

Writing a persistent entity bean can be as Writing a persistent entity bean can be as easy as using a few strategically placed easy as using a few strategically placed annotations with little to no information annotations with little to no information about the database.about the database.

Page 3: Architectural Overview

33

The Entity BeanThe Entity Bean

Entity beans in the Java Persistence 1.0 Entity beans in the Java Persistence 1.0 specification are available only as plain old specification are available only as plain old Java objects (POJOs), and are mapped to Java objects (POJOs), and are mapped to tables in a relational database. tables in a relational database.

Unlike other EJB types, entities can be Unlike other EJB types, entities can be allocated, serialized, and sent across the allocated, serialized, and sent across the network like any other POJO.network like any other POJO.

Page 4: Architectural Overview

44

The Entity BeanThe Entity Bean

To implement an entity bean, you need to To implement an entity bean, you need to define a bean class and decide what field define a bean class and decide what field you will use as the identifier (primary key) you will use as the identifier (primary key) of that bean class:of that bean class:

Page 5: Architectural Overview

55

Primary keyPrimary key

The primary key is something that The primary key is something that provides a pointer into the database. It provides a pointer into the database. It gives the entity bean class's identity both gives the entity bean class's identity both in memory as an object and in the in memory as an object and in the database as a row in a table. The primary database as a row in a table. The primary key can be a class or a primitive type.key can be a class or a primitive type.

Page 6: Architectural Overview

66

Bean classBean class

The bean class is a POJO that does not The bean class is a POJO that does not have to implement any interface or even have to implement any interface or even be serializable. It must be tagged with the be serializable. It must be tagged with the @javax.persistence.Entity annotation and @javax.persistence.Entity annotation and have at least one field or getter method have at least one field or getter method that is designated as the primary key of that is designated as the primary key of the entity bean class in the database. the entity bean class in the database.

Page 7: Architectural Overview

77

Bean classBean class

This is usually done with the This is usually done with the @javax.persistence.Id annotation. Entities @javax.persistence.Id annotation. Entities have other annotations available to define a have other annotations available to define a full object-relational database mapping as full object-relational database mapping as well.well.

In Java Persistence, entity beans are not In Java Persistence, entity beans are not components like their EJB 2.1 counterparts components like their EJB 2.1 counterparts are. are.

Page 8: Architectural Overview

88

Bean classBean class

Application code works directly with the Application code works directly with the entity bean classes and does not interact entity bean classes and does not interact with the entity through a component with the entity through a component interface, as you would with an EJB interface, as you would with an EJB session bean. To interact with entity session bean. To interact with entity beans, Java Persistence provides a new beans, Java Persistence provides a new service called the EntityManager . service called the EntityManager .

Page 9: Architectural Overview

99

Bean classBean class

All access to an entity goes through this All access to an entity goes through this service. It provides a query API and life service. It provides a query API and life cycle methods for the entity. No magic. No cycle methods for the entity. No magic. No bytecode manipulation. No special bytecode manipulation. No special proxies. Just plain Java.proxies. Just plain Java.

Page 10: Architectural Overview

1010

The Entity Bean ClassThe Entity Bean Class

To define an entity bean, all we'll need to To define an entity bean, all we'll need to do is define the bean class and annotate do is define the bean class and annotate it:it:

import javax.persistence.* ;

@Entity

@Table(name="CABIN")

public class Cabin {

private int id;

private String name;

private int deckLevel;

Page 11: Architectural Overview

1111

The Entity Bean ClassThe Entity Bean Class@Id

@GeneratedValue

@Column(name="CABIN_ID")

public int getId( ) { return id; }

public void setId(int pk) { this.id = pk; }

@Column(name="CABIN_NAME")

public String getName( ) { return name; }

public void setName(String str) { this.name = str; }

@Column(name="CABIN_DECK_LEVEL")

public int getDeckLevel( ) { return deckLevel; }

public void setDeckLevel(int level) { this.deckLevel = level }

}

Page 12: Architectural Overview

1212

The Entity Bean ClassThe Entity Bean Class

Unlike EJB 2.1, in which entity beans were Unlike EJB 2.1, in which entity beans were implemented as abstract classes, Java implemented as abstract classes, Java Persistence entity beans are real objects. Persistence entity beans are real objects. Java Persistence defines a relatively Java Persistence defines a relatively complete object-to-relational database complete object-to-relational database mapping (ORM)mapping (ORM)

Page 13: Architectural Overview

1313

XML Deployment Descriptors and XML Deployment Descriptors and JAR FilesJAR Files

Once you have written your entity bean classes, Once you have written your entity bean classes, you need to be able to deploy them in your you need to be able to deploy them in your environment. Entity beans are grouped into a finite environment. Entity beans are grouped into a finite set of classes called a set of classes called a persistence unit persistence unit . .

There is an EntityManager service that manages a There is an EntityManager service that manages a persistence unit, and it must be identified so that it persistence unit, and it must be identified so that it can be referenced in application code. can be referenced in application code.

Also, each persistence unit must be associated to Also, each persistence unit must be associated to a particular database so that the persistence a particular database so that the persistence provider knows where, how, and with what kind of provider knows where, how, and with what kind of database it is interacting. database it is interacting.

Page 14: Architectural Overview

1414

This information is stored in an XML deployment This information is stored in an XML deployment descriptor named descriptor named persistence.xmlpersistence.xml and is a and is a required artifact.required artifact.

Once you have defined your XML deployment Once you have defined your XML deployment descriptors and entity bean classes, you must descriptors and entity bean classes, you must package them all in a Java Archive (JAR) file. package them all in a Java Archive (JAR) file. The JAR file is used as both a library at runtime The JAR file is used as both a library at runtime and a holder for deployment information.and a holder for deployment information.

XML Deployment Descriptors and XML Deployment Descriptors and JAR FilesJAR Files

Page 15: Architectural Overview

1515

The Enterprise Bean ComponentThe Enterprise Bean Component

Enterprise JavaBeans server-side Enterprise JavaBeans server-side components come in two fundamentally components come in two fundamentally different types: different types: session beans session beans and and message-message-driven beans driven beans . .

Session beans are server-side components Session beans are server-side components that can be accessed using a variety of that can be accessed using a variety of distributed object protocols. distributed object protocols.

Message-driven beans process messages Message-driven beans process messages asynchronously from systems like the JMS, asynchronously from systems like the JMS, legacy systems, and web services. legacy systems, and web services.

Page 16: Architectural Overview

1616

The Enterprise Bean ComponentThe Enterprise Bean Component

All EJB servers must at least support a JMS-All EJB servers must at least support a JMS-based message-driven bean, but they may also based message-driven bean, but they may also support other types of message-driven beans.support other types of message-driven beans.

The activity that a session or message-driven The activity that a session or message-driven bean represents is fundamentally transient: you bean represents is fundamentally transient: you start making a reservation, you do a bunch of start making a reservation, you do a bunch of work, and then it's finished. work, and then it's finished.

The session and message-driven beans do not The session and message-driven beans do not represent things in the database.represent things in the database.

Page 17: Architectural Overview

1717

Classes and InterfacesClasses and Interfaces

A good way to understand the design of A good way to understand the design of enterprise beans is to look at how you'd go enterprise beans is to look at how you'd go about implementing one. about implementing one.

To implement session and message-To implement session and message-driven enterprise beans, you need to driven enterprise beans, you need to define their component interfaces, and a define their component interfaces, and a bean class:bean class:

Page 18: Architectural Overview

1818

Remote interfaceRemote interface

The remote interface defines a session The remote interface defines a session bean's business methods, which can be bean's business methods, which can be accessed from applications outside the EJB accessed from applications outside the EJB container: the business methods a bean container: the business methods a bean presents to the outside world to do its work. presents to the outside world to do its work.

The remote interface is a plain Java interface. The remote interface is a plain Java interface. It is tagged with the @javax.ejb.Remote It is tagged with the @javax.ejb.Remote annotation to identify that it is a remote annotation to identify that it is a remote interface.interface.

Page 19: Architectural Overview

1919

Local interfaceLocal interface

The local interface defines a session The local interface defines a session bean's business methods that can be used bean's business methods that can be used by other beans in the same EJB container: by other beans in the same EJB container: the business methods a bean presents to the business methods a bean presents to other beans running in the same JVM. other beans running in the same JVM.

Page 20: Architectural Overview

2020

Local interfaceLocal interface

It allows beans to interact without the It allows beans to interact without the overhead of a distributed object protocol, overhead of a distributed object protocol, which improves their performance. which improves their performance.

The local interface is a plain Java interface. The local interface is a plain Java interface. It is tagged with the @javax.ejb.Local It is tagged with the @javax.ejb.Local annotation to identify that it is a local annotation to identify that it is a local interface.interface.

Page 21: Architectural Overview

2121

Endpoint interfaceEndpoint interface

The endpoint interface defines business The endpoint interface defines business methods that can be accessed from methods that can be accessed from applications outside the EJB container via applications outside the EJB container via SOAP. SOAP.

The endpoint interface is based on Java API for The endpoint interface is based on Java API for XML-RPC (JAX-RPC) and is designed to XML-RPC (JAX-RPC) and is designed to adhere to the SOAP and WSDL standards. adhere to the SOAP and WSDL standards.

The endpoint interface is a plain Java interface The endpoint interface is a plain Java interface that is annotated with the that is annotated with the @javax.jws.WebService [email protected] annotation.

Page 22: Architectural Overview

2222

Message interfaceMessage interface

Message-driven beans implement the Message-driven beans implement the message interface, which defines the message interface, which defines the methods by which messaging systems, methods by which messaging systems, such as the JMS, can deliver messages to such as the JMS, can deliver messages to the bean.the bean.

Page 23: Architectural Overview

2323

Bean classBean class The session bean class contains business The session bean class contains business

logic and must have at least one remote, logic and must have at least one remote, local, or endpoint interface. local, or endpoint interface.

It usually implements these interfaces, but it It usually implements these interfaces, but it is not required to. is not required to.

A bean class may also have more than one A bean class may also have more than one interface of a given type. interface of a given type.

The EJB container usually determines The EJB container usually determines whether a session bean is remote and/or whether a session bean is remote and/or local by the interfaces it implements. local by the interfaces it implements.

Page 24: Architectural Overview

2424

Bean classBean class

The session bean class must also be The session bean class must also be tagged with the @javax.ejb.Stateful or tagged with the @javax.ejb.Stateful or @javax.ejb.Stateless annotation so that @javax.ejb.Stateless annotation so that the EJB container knows what session the EJB container knows what session bean type it is.bean type it is.

Page 25: Architectural Overview

2525

The remote interfaceThe remote interface

Having introduced the machinery, let's Having introduced the machinery, let's look at how to build a very simple stateless look at how to build a very simple stateless session bean with a remote component session bean with a remote component interface. interface.

Now we will examine the Calculator EJB, a Now we will examine the Calculator EJB, a session bean that exposes basic session bean that exposes basic calculator functions as a service. Let's calculator functions as a service. Let's start with its remote interface.start with its remote interface.

Page 26: Architectural Overview

2626

The remote interfaceThe remote interface

We'll define the remote interface for the We'll define the remote interface for the Calculator bean using the Calculator bean using the CalculatorRemote interface, which defines CalculatorRemote interface, which defines arithmetic operations. arithmetic operations.

Remote interfaces are denoted by the Remote interfaces are denoted by the @javax.ejb.Remote annotation:@javax.ejb.Remote annotation:

Page 27: Architectural Overview

2727

The remote interfaceThe remote interfaceimport javax.ejb.Remote;

@Remote

public interface CalculatorRemote {

public int add(int x, int y);

public int subtract(int x, int y);

}

You'll notice that even though this is the remote You'll notice that even though this is the remote interface of the EJB, there is no reference to Java interface of the EJB, there is no reference to Java RMI interfaces or APIs. RMI interfaces or APIs.

This is a big change in the EJB 3.0 specification This is a big change in the EJB 3.0 specification compared to older versions.compared to older versions.

Page 28: Architectural Overview

2828

The bean classThe bean class

Now let's look at an actual stateless Now let's look at an actual stateless session bean class. session bean class.

Here's the code for the CalculatorBean Here's the code for the CalculatorBean class; it's a sparse implementation, but it class; it's a sparse implementation, but it shows how the pieces fit together:shows how the pieces fit together:

Page 29: Architectural Overview

2929

The bean classThe bean class

import javax.ejb.*;

@Stateless

public class CalculatorBean implements CalculatorRemote {

public int add(int x, int y) {

return x + y;

}

public int subtract(int x, int y) {

return x - y;

}

}

Page 30: Architectural Overview

3030

The bean classThe bean class

The CalculatorBean class is required to The CalculatorBean class is required to implement at least one remote or local implement at least one remote or local interface and to be annotated with interface and to be annotated with @javax.ejb.Stateless [email protected] .

Those familiar with earlier EJB Those familiar with earlier EJB specifications will notice that session specifications will notice that session beans do not have to implement an EJB-beans do not have to implement an EJB-specific interface or any of the callback specific interface or any of the callback notifications.notifications.

Page 31: Architectural Overview

3131

Message-Driven BeansMessage-Driven Beans

Message-driven beans also have a bean Message-driven beans also have a bean class that implements a message class that implements a message interface; they don't implement remote, interface; they don't implement remote, local, or endpoint interfaces. local, or endpoint interfaces.

This bean class is annotated using the This bean class is annotated using the @javax.ejb.MessageDriven annotation. @javax.ejb.MessageDriven annotation. Message-driven beans are integration Message-driven beans are integration points for other applications interested in points for other applications interested in working with EJB applications. working with EJB applications.

Page 32: Architectural Overview

3232

Message-Driven BeansMessage-Driven Beans

Java applications or legacy systems that Java applications or legacy systems that need to access EJB applications can send need to access EJB applications can send messages to message-driven beans via messages to message-driven beans via JMS. JMS.

These beans process those messages These beans process those messages and perform the required tasks using other and perform the required tasks using other entity and session beans. entity and session beans.

Page 33: Architectural Overview

3333

Message-Driven BeansMessage-Driven Beans

EJB 3.0 is not limited to JMS-based EJB 3.0 is not limited to JMS-based message-driven beans: message-driven message-driven beans: message-driven beans can support any messaging system beans can support any messaging system that implements the correct JCA 1.5 that implements the correct JCA 1.5 contracts. contracts.

However, support for JMS-based However, support for JMS-based message-driven beans (JMS-MDBs) in message-driven beans (JMS-MDBs) in EJB 3.0 is mandatory.EJB 3.0 is mandatory.

Page 34: Architectural Overview

3434

Stateful and statelessl session Stateful and statelessl session beansbeans

Session beans can be either Session beans can be either statefulstateful or or statelessstateless. .

Stateful session beans maintain Stateful session beans maintain conversational conversational state state when used by a client. when used by a client.

Conversational state is not written to a database; Conversational state is not written to a database; it's information that is kept in memory while a it's information that is kept in memory while a client carries on a conversation with an client carries on a conversation with an enterprise bean, and it is lost when the enterprise bean, and it is lost when the conversation ends or the EJB container crashes. conversation ends or the EJB container crashes.

Page 35: Architectural Overview

3535

Stateful and statelessl session Stateful and statelessl session beansbeans

Stateful session beans are not shared Stateful session beans are not shared among clients; they are dedicated to the among clients; they are dedicated to the same client for the life of the enterprise same client for the life of the enterprise bean. bean.

Stateless session beans do not maintain Stateless session beans do not maintain any conversational state. Each method is any conversational state. Each method is completely independent and uses only completely independent and uses only data passed in its parameters.data passed in its parameters.

Page 36: Architectural Overview

3636

The Bean-Container ContractThe Bean-Container Contract

The environment that surrounds the beans on The environment that surrounds the beans on the EJB server is often called the the EJB server is often called the containercontainer. .

The container is more a concept than a physical The container is more a concept than a physical construct. It acts as an intermediary between the construct. It acts as an intermediary between the bean and the EJB server. bean and the EJB server.

It manages the EJB objects and helps these It manages the EJB objects and helps these constructs manage bean resources and provide constructs manage bean resources and provide services such as transactions, security, services such as transactions, security, concurrency, and naming at runtime. concurrency, and naming at runtime.

Page 37: Architectural Overview

3737

The Bean-Container ContractThe Bean-Container Contract

The distinction between the container and the The distinction between the container and the server is not clearly defined, but the EJB server is not clearly defined, but the EJB specification defines the component model in specification defines the component model in terms of the container's responsibilities, so terms of the container's responsibilities, so we will follow that convention here.we will follow that convention here.