1 / 41

EJB

EJB. 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.

Download Presentation

EJB

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. EJB

  2. 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.

  3. 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.

  4. 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.

  5. 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

  6. 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

  7. 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

  8. 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 ...

  9. Home Interface public interface HelloHome extends EJBHome { Hello create(String str) throws RemoteException, …; }

  10. Session Bean Implementation public class HelloEJB implements SessionBean { String str; public ejbCreate(String str) {this.str = str; } public String sayHello() throws RemoteException {return str;}…}

  11. Session bean • Also: ejbRemove(), ejbActivate(), ejbPassivate() • ejbCreate corresponds to create method in home interface

  12. Client: main method Context ic = new InitialContext(); Object obj = ic.lookup(“helloapp/Hello”); HelloHome hhome = (HelloHome) PortableRemoteObject.narrow (obj, HelloHome.class);

  13. Client: main method Hello hobj = hhome.create(“Hello World”); String hstr = hobj.sayHello(); System.out.println(hstr);

  14. Entity Beans • Persistent: can be container managed • using deployment descriptor • container also provides transaction management • Remote interface • Home interface • Entity bean class

  15. 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

  16. Entity Bean Class • business methods • create methods • remove methods • finder methods • load and store methods • activation, passivation methods

  17. 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

  18. 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)

  19. 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.

  20. 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.

  21. 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.

  22. 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.

  23. 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();}}

  24. 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 }

  25. Generate deployment descriptor • Connector contains information about ContainerManagedFields • Connector localizes information; it is not spread through several classes

  26. 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.

  27. 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

  28. 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

  29. 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.

  30. Generic BB • A building block with holes

  31. 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.

  32. 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.

  33. 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.

  34. 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.

  35. 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

  36. 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.

  37. 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.

  38. 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.

  39. 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.

  40. AOP and Law of Demeter ? • Only talk to friends

More Related