EJB. Component Characteristics An enterprise Bean typically contains business logic that operates on...

41
EJB

Transcript of EJB. Component Characteristics An enterprise Bean typically contains business logic that operates on...

Page 1: EJB. Component Characteristics An enterprise Bean typically contains business logic that operates on the enterprise’s data. An enterprise Bean’s instances.

EJB

Page 2: EJB. Component Characteristics An enterprise Bean typically contains business logic that operates on the enterprise’s data. An enterprise Bean’s instances.

Component Characteristics

• An enterprise Bean typically contains business logic that operates on the enterprise’s data.

• An enterprise Bean’s instances are created and managed at runtime by a Container.

• An enterprise Bean can be customized at deployment time by editing its environment entries.

Page 3: EJB. Component Characteristics An enterprise Bean typically contains business logic that operates on the enterprise’s data. An enterprise Bean’s instances.

Components

• Various metadata, such as a transaction and security attributes, are separate from the enterprise Bean class. This allows the metadata to be managed by tools during application assembly or deployment (or both).

• Client access is mediated by the Container in which the enterprise Bean is deployed.

Page 4: EJB. Component Characteristics An enterprise Bean typically contains business logic that operates on the enterprise’s data. An enterprise Bean’s instances.

Components

• An enterprise Bean can be included in an assembled application without requiring source code changes or recompilation of the enterprise Bean.

• A client view of an enterprise Bean is defined by the Bean Provider. The client view can be manually defined by the Bean developer, or generated automatically by application development tools. The client view is unaffected by the container and server in which the Bean is deployed. This ensures that both the Beans and their clients can be deployed in multiple execution environments without changes or recompilation.

Page 5: EJB. Component Characteristics An enterprise Bean typically contains business logic that operates on the enterprise’s data. An enterprise Bean’s instances.

EJB use

• Tier 1: Client

• Tier 2: EJB server– EJB container (provides transaction

management, security, remote connectivity, concurrency, life cycle management)

• Enterprise beans

• Tier 3: Databases, legacy code

Page 6: EJB. Component Characteristics An enterprise Bean typically contains business logic that operates on the enterprise’s data. An enterprise Bean’s instances.

Session beans

• Model business processes such as checking, canceling, shipping orders.

• Run by code on a remote client: locates bean through JNDI (Java Naming and Directory Interface)

• EJB container creates copy of session bean on client

Page 7: EJB. Component Characteristics An enterprise Bean typically contains business logic that operates on the enterprise’s data. An enterprise Bean’s instances.

Session Beans

• Four pieces of code you need to write– remote interface: methods client can use– home interface: used to create remote copy– session bean: does most of the work– client code that calls the bean

Page 8: EJB. Component Characteristics An enterprise Bean typically contains business logic that operates on the enterprise’s data. An enterprise Bean’s instances.

Remote Interface

public interface Hello

extends EJBObject {

String sayHello()

throws RemoteException;

Client will call only one method

Follow RMI rules: must throw RemoteException, return types primitive or ...

Page 9: EJB. Component Characteristics An enterprise Bean typically contains business logic that operates on the enterprise’s data. An enterprise Bean’s instances.

Home Interface

public interface HelloHome

extends EJBHome {

Hello create(String str)

throws RemoteException, …;

}

Page 10: EJB. Component Characteristics An enterprise Bean typically contains business logic that operates on the enterprise’s data. An enterprise Bean’s instances.

Session Bean Implementation

public class HelloEJB

implements SessionBean {

String str;

public ejbCreate(String

str) {this.str = str; }

public String sayHello()

throws RemoteException

{return str;}…}

Page 11: EJB. Component Characteristics An enterprise Bean typically contains business logic that operates on the enterprise’s data. An enterprise Bean’s instances.

Session bean

• Also: ejbRemove(), ejbActivate(), ejbPassivate()

• ejbCreate corresponds to create method in home interface

Page 12: EJB. Component Characteristics An enterprise Bean typically contains business logic that operates on the enterprise’s data. An enterprise Bean’s instances.

Client: main method

Context ic = new InitialContext();

Object obj =

ic.lookup(“helloapp/Hello”);

HelloHome hhome = (HelloHome) PortableRemoteObject.narrow

(obj, HelloHome.class);

Page 13: EJB. Component Characteristics An enterprise Bean typically contains business logic that operates on the enterprise’s data. An enterprise Bean’s instances.

Client: main method

Hello hobj = hhome.create(“Hello World”);

String hstr = hobj.sayHello();

System.out.println(hstr);

Page 14: EJB. Component Characteristics An enterprise Bean typically contains business logic that operates on the enterprise’s data. An enterprise Bean’s instances.

Entity Beans

• Persistent: can be container managed

• using deployment descriptor

• container also provides transaction management– Remote interface– Home interface– Entity bean class

Page 15: EJB. Component Characteristics An enterprise Bean typically contains business logic that operates on the enterprise’s data. An enterprise Bean’s instances.

Example

• Tier 1: WebBrowser: Applet

• Tier 2: – WebServer: Servlet– EJB Server

• EJB Container– Product Entity Bean– Order Entity Bean– Customer Entity Bean

• Tier 3: Data Base Server

Page 16: EJB. Component Characteristics An enterprise Bean typically contains business logic that operates on the enterprise’s data. An enterprise Bean’s instances.

Entity Bean Class

• business methods

• create methods

• remove methods

• finder methods

• load and store methods

• activation, passivation methods

Page 17: EJB. Component Characteristics An enterprise Bean typically contains business logic that operates on the enterprise’s data. An enterprise Bean’s instances.

EJB

• Session beans– represents a client in the server. Client calls

methods (e.g., enterOrder). Is transient: terminates when client terminates.

• Entity beans– represents a business object (persistent).– Persistence can be beans or container managed

Page 18: EJB. Component Characteristics An enterprise Bean typically contains business logic that operates on the enterprise’s data. An enterprise Bean’s instances.
Page 19: EJB. Component Characteristics An enterprise Bean typically contains business logic that operates on the enterprise’s data. An enterprise Bean’s instances.

Enterprise Java Beans (EJB) and Aspectual components

• EJB: a hot Java component technology from SUN/IBM

• Aspectual components: a conceptual tool for the design of enterprise Java beans (and other components)

Page 20: EJB. Component Characteristics An enterprise Bean typically contains business logic that operates on the enterprise’s data. An enterprise Bean’s instances.

Enterprise JavaBeans (EJB)

• Addresses aspectual decomposition.

• An enterprise Bean provider usually does not program transactions, concurrency, security, distribution and other services into the enterprise Beans.

• An enterprise Bean provider relies on an EJB container provider for these services.

Page 21: EJB. Component Characteristics An enterprise Bean typically contains business logic that operates on the enterprise’s data. An enterprise Bean’s instances.

EJB

• Beans

• Containers: to manage and adapt the beans. Intercept messages sent to beans and can execute additional code. Similar to reimplementation of expected interface in aspectual component.

Page 22: EJB. Component Characteristics An enterprise Bean typically contains business logic that operates on the enterprise’s data. An enterprise Bean’s instances.

Aspectual components for EJB design/implementation

• Use ACs to model transactions, concurrency, security, distribution and other system level issues. Translate ACs to deployment descriptors (manually, or by tool).

• Use ACs to model beans in reusable form. Generate (manually or by tool) Java classes from ACs and connectors.

Page 23: EJB. Component Characteristics An enterprise Bean typically contains business logic that operates on the enterprise’s data. An enterprise Bean’s instances.

Example: Use AC for EJB persistence

As an example we consider how persistence is handled by EJB containers. The deployment descriptor of a bean contains an instance variable ContainerManagedFields defining the instance variables that need to be read or written. This will be used to generate the database access code automatically and protects the bean from database specific code.

Page 24: EJB. Component Characteristics An enterprise Bean typically contains business logic that operates on the enterprise’s data. An enterprise Bean’s instances.

Aspectual component: Persistence

component Persistence { PerMem p;

participant Source {

expect Target[] targets;

expect void writeOp();}

// for all targets:writeOp

participant Target

expect void writeOp();

replace void writeOp() {

// write to persistent memory p

expected();}}

Page 25: EJB. Component Characteristics An enterprise Bean typically contains business logic that operates on the enterprise’s data. An enterprise Bean’s instances.

Deployment

connector PersistenceConn1 {

ClassGraph g = ; // from Company to *

Company is Persistence.Source;

Nodes(g) is Persistence.Target;

with {writeOp = write*};

// must be the same writeOp for both

// Source and Target

}

Page 26: EJB. Component Characteristics An enterprise Bean typically contains business logic that operates on the enterprise’s data. An enterprise Bean’s instances.

Generate deployment descriptor

• Connector contains information about ContainerManagedFields

• Connector localizes information; it is not spread through several classes

Page 27: EJB. Component Characteristics An enterprise Bean typically contains business logic that operates on the enterprise’s data. An enterprise Bean’s instances.

Discussion of session bean and entity bean distinction

• interesting modeling is done in session beans; initially only session beans

• entity beans: dumb objects?Enterprise beans components are intended to be

relatively coarse-grained business objects (e.g. purchase order, employee record). Fine-grained objects (e.g. line item on a purchase order, employee’s address) should not be modeled as enterprise bean components.

Page 28: EJB. Component Characteristics An enterprise Bean typically contains business logic that operates on the enterprise’s data. An enterprise Bean’s instances.

Ejb and aop

• deployment descriptors :=: APPC

• session beans :=: “session” APPC– session beans are extensions of clients

• entity beans :=: “base” participants– key participants

• regular classes :=: define through APPC

Page 29: EJB. Component Characteristics An enterprise Bean typically contains business logic that operates on the enterprise’s data. An enterprise Bean’s instances.

Terminology

• Building block = BB

• Participants (classes at basic level) = BB of type 1

• APPC = BB of type 2 = consisting of set of generic BB of type 1.

• Connectors = map generic BB

Page 30: EJB. Component Characteristics An enterprise Bean typically contains business logic that operates on the enterprise’s data. An enterprise Bean’s instances.

AOP

• components = building blocks type 1

• whatever does not fit into one building block of type 1 is expressed as one building block type 2 that consists of a set of generic building blocks of type 1.

• generic building blocks of a building block of type 2 are mapped to building blocks of type 1.

Page 31: EJB. Component Characteristics An enterprise Bean typically contains business logic that operates on the enterprise’s data. An enterprise Bean’s instances.

Generic BB

• A building block with holes

Page 32: EJB. Component Characteristics An enterprise Bean typically contains business logic that operates on the enterprise’s data. An enterprise Bean’s instances.

AOP

• Properties of mapping– one generic building block is mapped to one or

more building blocks– building blocks may have parts. One part of

generic building block is mapped to one or more parts of corresponding generic building block.

Page 33: EJB. Component Characteristics An enterprise Bean typically contains business logic that operates on the enterprise’s data. An enterprise Bean’s instances.

Exceptions in EJB

• An application exception is an exception defined in the throws clause of a method of the enterprise Bean’s home and remote interface, other than the java.rmi.RemoteException.

• An enterprise bean business method may encounter various system exceptions or errors that prevent the method from successful completion.

Page 34: EJB. Component Characteristics An enterprise Bean typically contains business logic that operates on the enterprise’s data. An enterprise Bean’s instances.

Examples

• Application exception: account has insufficient balance

• System exception: failure to obtain a database connection, JNDI exceptions, unexpected RemoteException from invocation of other enterprise beans, unexpected RuntimeException, JVM errors, etc.

Page 35: EJB. Component Characteristics An enterprise Bean typically contains business logic that operates on the enterprise’s data. An enterprise Bean’s instances.

Use of application level exceptions

• Application exceptions are intended to be handled by the client, and not by the system administrator. They should be used only for reporting business logic exceptions, not for reporting system level problems.

Page 36: EJB. Component Characteristics An enterprise Bean typically contains business logic that operates on the enterprise’s data. An enterprise Bean’s instances.

Application exception

• Because an application exception does not automatically result in marking the transaction for rollback, the Bean Provider must do one of the following to ensure data integrity before throwing an application exception from an enterprise bean instance:– no loss of data integrity– mark for rollback before throwing application

exception, prevents commit

Page 37: EJB. Component Characteristics An enterprise Bean typically contains business logic that operates on the enterprise’s data. An enterprise Bean’s instances.

System exception guidelines

• If the bean method encounters a RuntimeException or error, it should simply propagate the error from the bean method to the Container (i.e. the bean method does not have to catchthe exception).

• Any other unexpected error conditions should be reported using the javax.ejb.EJBException.

Page 38: EJB. Component Characteristics An enterprise Bean typically contains business logic that operates on the enterprise’s data. An enterprise Bean’s instances.

What the container does

• catches a non-application exception,

• logs it (which can result in alerting the System Administrator), and

• throws the java.rmi.RemoteException (or subclass thereof) to the client.

Page 39: EJB. Component Characteristics An enterprise Bean typically contains business logic that operates on the enterprise’s data. An enterprise Bean’s instances.

What container does

• The Bean Provider can rely on the Container to perform to following tasks when catching a non-application exception:– The transaction in which the bean method

participated will be rolled back.– No other method will be invoked on an

instance that threw a non-application exception.

Page 40: EJB. Component Characteristics An enterprise Bean typically contains business logic that operates on the enterprise’s data. An enterprise Bean’s instances.

What container does

• This means that the Bean Provider does not have to perform any cleanup actions before throwing a non-application exception. It is the Container that is responsible for the cleanup.

Page 41: EJB. Component Characteristics An enterprise Bean typically contains business logic that operates on the enterprise’s data. An enterprise Bean’s instances.

AOP and Law of Demeter ?

• Only talk to friends