Presentation is loading. Please wait.

Presentation is loading. Please wait.

Enterprise Java Beans: an introduction

Similar presentations


Presentation on theme: "Enterprise Java Beans: an introduction"— Presentation transcript:

1 Enterprise Java Beans: an introduction

2 Java E-Commerce © Martin Cooke, 2003
Today’s lecture Why is enterprise computing so complex? Component models and containers Session beans Entity beans 11/04/2019 Java E-Commerce © Martin Cooke, 2003

3 Why is enterprise computing so complex?
session management authorisation persistence transactions Core code 11/04/2019 Java E-Commerce © Martin Cooke, 2003

4 Java E-Commerce © Martin Cooke, 2003
A solution Benefits Programming becomes easier Certain aspects are hard eg transactions … … or tedious eg persistence No need to duplicate code or reinvent wheel Can take advantage of 3rd party solutions Business logic can be ported between container providers Scaleability issues taken out of webapp container business logic enterprise bean persistence transactions authorisation sessions object pooling load-balancing network services Drawback Loss of control can impact upon efficiency “middleware” services 11/04/2019 Java E-Commerce © Martin Cooke, 2003

5 Distributed object technologies
CORBA (Object Management Group) Revolutionised distributed computing, but overcomplex programming model, and less portable than expected Java RMI (Sun) DCOM/MTS (Microsoft) EJB (Sun) 11/04/2019 Java E-Commerce © Martin Cooke, 2003

6 Containers and components

7 Java E-Commerce © Martin Cooke, 2003
Definitions Component reusable software building block often customisable Container shell in which the component executes provides services all communication with component goes via container 11/04/2019 Java E-Commerce © Martin Cooke, 2003

8 Java E-Commerce © Martin Cooke, 2003
Definitions The relationship between a container and its components is analogous to that between a webserver and servlets a browser and applets 11/04/2019 Java E-Commerce © Martin Cooke, 2003

9 Java E-Commerce © Martin Cooke, 2003
Component model Defines structure of interfaces and mechanisms by which it interacts, both with container and with client applications Components are called Enterprise Java Beans 11/04/2019 Java E-Commerce © Martin Cooke, 2003

10 Java E-Commerce © Martin Cooke, 2003
Containers Manages many beans simultaneously Intercepts all method invocations on beans Pools resources Can ‘swap’ beans in and out: transparent to client applications Provides everything needed by bean eg JDBC access 11/04/2019 Java E-Commerce © Martin Cooke, 2003

11 Comparison with java beans
Enterprise java bean Typically used for widgets, controls Manipulated using visual tools Configured at compile time Run on one machine, in one address space Typically used for larger-grained business processes and objects Can be configured at deploy time 11/04/2019 Java E-Commerce © Martin Cooke, 2003

12 Java E-Commerce © Martin Cooke, 2003
Using the bean Client Anything: webapp, app or another EJB Container Home interface Used to control the lifecycle methods of the bean: create, remove, find; uses JNDI Home interface create find remove Client app EJB Remote interface Exposes methods that make the bean tick Remote interface Business methods Deployment descriptor Deployment descriptor Defines security, transactional, … behaviour 11/04/2019 Java E-Commerce © Martin Cooke, 2003

13 Java E-Commerce © Martin Cooke, 2003
Two types of EJB Entity bean Represents persistent data Can be viewed as an object representation of a row in a database (but is more general) Think of it as a noun Session bean Represents transient data and processes Think of it as a verb 11/04/2019 Java E-Commerce © Martin Cooke, 2003

14 Java E-Commerce © Martin Cooke, 2003
Two types of EJB entity session Object representation of persistent data Identified by primary key Transactional Recoverable after system crash Persistence can be managed by container or by bean Egs: a booking, a customer Represents work being performed by client, and may span several method calls Created by client Exists for a single session Can be stateful or stateless May be transactional usually not recoverable Eg: a reservation handler 11/04/2019 Java E-Commerce © Martin Cooke, 2003

15 Session beans

16 Java E-Commerce © Martin Cooke, 2003
Typical uses Price quoting Order entry Compression Complex calculations Database operations Credit card verification ie business logic 11/04/2019 Java E-Commerce © Martin Cooke, 2003

17 Session beans: 2 flavours
Stateless don’t maintain state across method calls Any instance can be used at any time by any client Eg audio compression Stateful Maintain state within and between transactions Each is associated with a specific client Containers manage instance pool Eg shopping cart 11/04/2019 Java E-Commerce © Martin Cooke, 2003

18 Session bean lifecycle
Pooling Number of instances < number of current clients due to thinking time Eg threads, socket connections, database connections, … Concurrency All methods are thread-safe (one-at-a-time) Extra beans created to handle bottlenecks Source: middleware-company.com book 11/04/2019 Java E-Commerce © Martin Cooke, 2003

19 Java E-Commerce © Martin Cooke, 2003
A first example Simplest possible bean: stateless session “hello world” bean Recall that we need Home interface Remote interface Bean implementation Deployment descriptor Container create find remove Home interface Client app EJB Remote interface Business methods Deployment descriptor 11/04/2019 Java E-Commerce © Martin Cooke, 2003

20 Remote method invocation (RMI)
RMI enables invocation of methods on remote objects, passing in and receiving real Java objects as arguments and return values Remote objects implement a remote interface which specifies which methods can be invoked remotely Any object that implements java.rmi.remote is callable across the network EJB objects implement java.rmi.remote, hence all EJBs are networkable objects 11/04/2019 Java E-Commerce © Martin Cooke, 2003

21 Java E-Commerce © Martin Cooke, 2003
The home interface public interface HelloHome extends javax.ejb.EJBHome { Hello create() throws java.rmi.RemoteException, javax.ejb.CreateException; } All home interfaces extend javax.ejb.EJBHome All home interfaces must supply a create method which is used for initialisation and can take params, but not here (stateless) Note exceptions: all remote methods must throw a RemoteException Container provides implementation 11/04/2019 Java E-Commerce © Martin Cooke, 2003

22 Java E-Commerce © Martin Cooke, 2003
The remote interface public interface Hello extends javax.ejb.EJBObject { public String hello() throws java.rmi.RemoteException; } All remote interfaces extend javax.ejb.EJBObject hello is our single business method 11/04/2019 Java E-Commerce © Martin Cooke, 2003

23 Java E-Commerce © Martin Cooke, 2003
The bean class public class HelloBean implements javax.ejb.SessionBean { public void ejbCreate() {}   public void ejbRemove() {} public void ejbActivate() {} public void ejbPassivate() {} public void setSessionContext (javax.ejb.SessionContext ctx) {} public String hello() { return "Hello, World!"; } Business methods Container methods allow container to interact with bean ejbCreate must match create method of home interface ejbRemove called when bean is destroyed ejbActivate and ejbPassivate concepts don’t apply to stateless session beans setSessionContent allows beans to access certain resources eg info about caller identity 11/04/2019 Java E-Commerce © Martin Cooke, 2003

24 The deployment descriptor
<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" " <ejb-jar> <enterprise-beans> <session> <ejb-name>Hello</ejb-name> <home>examples.HelloHome</home> <remote>examples.Hello</remote> <ejb-class>examples.HelloBean</ejb-class> <session-type>Stateless</session-type> <transaction-type>Container</transaction-type> </session> </enterprise-beans> </ejb-jar> 11/04/2019 Java E-Commerce © Martin Cooke, 2003

25 Java E-Commerce © Martin Cooke, 2003
What’s missing Simplest possible bean; many features not covered or used Vendor-specific deployment information Packaging Deployment Client-access code (next) 11/04/2019 Java E-Commerce © Martin Cooke, 2003

26 Java E-Commerce © Martin Cooke, 2003
Client application import javax.naming.Context; import javax.naming.InitialContext; import java.util.Properties; public class HelloClient { public static void main(String[] args) throws Exception { Context ctx = new InitialContext(System.getProperties()); HelloHome home = (HelloHome) javax.rmi.PortableRemoteObject.narrow( ctx.lookup("HelloHome"), HelloHome.class); Hello hello = home.create(); System.out.println(hello.hello()); hello.remove(); } Steps Use of JNDI to find bean RMI-style cast Use home object to create EJB Call business method on bean Remove bean 11/04/2019 Java E-Commerce © Martin Cooke, 2003

27 Stateful session beans
Issue Not so easy to pool beans because of need to maintain conversational state: could easily run out of resources Solution cf operating systems and applications: Passivate: Swap out state and save Activate: Swap in when needed to restore state Bean instance receiving state might not be same as that previously used, but doesn’t matter Has effect of pooling beans 11/04/2019 Java E-Commerce © Martin Cooke, 2003

28 More on passivation & activation
Which? Container dependent, but least recently used is typical When passivated? Any time bean is not in a method call or in a transaction When activated? Container dependent, but often ‘just in time’ What is saved? All non transient member variables How used? ejbPassivate called just before saving, ejbActivate just after restoration. Gives bean chance to relinquish resources or reconstruct transient objects 11/04/2019 Java E-Commerce © Martin Cooke, 2003

29 Java E-Commerce © Martin Cooke, 2003
Example: counter public class CountBean implements SessionBean { public int val; public int count() { System.out.println("count()"); return ++val; } public void ejbCreate(int _val) throws CreateException { val = _val; etc 11/04/2019 Java E-Commerce © Martin Cooke, 2003

30 Deployment descriptor
<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" " <ejb-jar> <enterprise-beans> <session> <ejb-name>Count</ejb-name> <home>examples.CountHome</home> <remote>examples.Count</remote> <ejb-class>examples.CountBean</ejb-class> <session-type>Stateful</session-type> <transaction-type>Container</transaction-type> </session> </enterprise-beans> </ejb-jar> 11/04/2019 Java E-Commerce © Martin Cooke, 2003

31 Client application main
Context ctx = new InitialContext(System.getProperties()); CountHome home = (CountHome) javax.rmi.PortableRemoteObject.narrow( ctx.lookup("CountHome"), CountHome.class); Count count = home.create(0); System.out.println(count.count()); count.remove(); 11/04/2019 Java E-Commerce © Martin Cooke, 2003

32 Entity beans

33 Java E-Commerce © Martin Cooke, 2003
Reminder Entity beans provide an in-memory representation of persistent data Can read themselves from storage and persistent themselves back to storage Survive system failures Used to model the nouns of an application Customer Order 11/04/2019 Java E-Commerce © Martin Cooke, 2003

34 Java E-Commerce © Martin Cooke, 2003
Persistence Container-managed Simple to use Possibly very inefficient given current state-of-the-art in containers Bean-managed Customisable 11/04/2019 Java E-Commerce © Martin Cooke, 2003

35 Java E-Commerce © Martin Cooke, 2003
Transactional issues Like session beans, entity beans are single-threaded Multiple threads makes transactions very difficult Difficult to produce reliable thread-safe code Like session beans, containers can create multiple instances (pools) For entity beans, this raises the issue of multiple instances of the same data becoming out of synch. Dealt with using transactional isolation 11/04/2019 Java E-Commerce © Martin Cooke, 2003

36 Java E-Commerce © Martin Cooke, 2003
Pooling Like session beans, entity beans may be pooled ie implement ejbActivate() and ejbPassivate() In addition, state is saved just prior to passivation and loaded just prior to activation 11/04/2019 Java E-Commerce © Martin Cooke, 2003

37 Java E-Commerce © Martin Cooke, 2003
BMP vs CMP Source: the J2EE tutorial (2002), Sun 11/04/2019 Java E-Commerce © Martin Cooke, 2003

38 Java E-Commerce © Martin Cooke, 2003
Example entity bean From WebTomorrow tutorial (see resources) Models a CD Uses container-managed persistence 11/04/2019 Java E-Commerce © Martin Cooke, 2003

39 Java E-Commerce © Martin Cooke, 2003
The remote interface import javax.ejb.*; import java.rmi.RemoteException; public interface CD extends EJBObject {   public abstract String getTitle() throws RemoteException;   public abstract void setTitle(String title) throws RemoteException;   public abstract String getId() throws RemoteException;   public abstract void setId(String id) // … more get/set methods for other fields } 11/04/2019 Java E-Commerce © Martin Cooke, 2003

40 Java E-Commerce © Martin Cooke, 2003
The home interface import javax.ejb.*; import java.rmi.RemoteException; public interface CDHome extends EJBHome { public CD create(String id) throws RemoteException, CreateException; public CD findByPrimaryKey (String id) throws RemoteException, FinderException; } NB: this and other finder methods implemented by container 11/04/2019 Java E-Commerce © Martin Cooke, 2003

41 The bean implementation
import javax.ejb.*; import java.rmi.RemoteException; public class CDBean implements EntityBean { public String id; public String title; // other fields public String getTitle() { return title; } public void setTitle(String _title) { title = _title; 11/04/2019 Java E-Commerce © Martin Cooke, 2003

42 The bean implementation contd
public String ejbCreate(String _id) { id = _id; return null; } // mandatory methods public void ejbPostCreate(String id) {} public void setEntityContent(EntityContext ctx) {} public void ejbActivate() {} public void ejbPassivate() {} public void ejbLoad() {} public void ejbStore() {} public void ejbRemove() {} 11/04/2019 Java E-Commerce © Martin Cooke, 2003

43 The deployment descriptor
<enterprise-beans> <entity> <ejb-name>CD</ejb-name> <home>examples.CDHome</home> <remote>examples.CD</remote> <ejb-class>examples.CDBean</ejb-class> <persistence-type>Container</persistence-type> <prim-key-class>java.lang.String</prim-key-class> <reentrant>False</reentrant> <cmp-field><field-name>id</field-name></cmp-field> <cmp-field><field-name>title</field-name></cmp-field> <primkey-field>id</primkey-field> </entity> </enterprise-beans> 11/04/2019 Java E-Commerce © Martin Cooke, 2003

44 The deployment descriptor, contd
<assembly-descriptor> <container-transaction> <method> <ejb-name>CD</ejb-name> <method-name>*</method-name> </method> <trans-attribute>Required</trans-attribute> </assembly-descriptor> 11/04/2019 Java E-Commerce © Martin Cooke, 2003

45 Java E-Commerce © Martin Cooke, 2003
What else is in EJB? Message-driven beans EJB-QL Security Network services Tools for deployment 11/04/2019 Java E-Commerce © Martin Cooke, 2003

46 Java E-Commerce © Martin Cooke, 2003
jBoss jBoss.org Free widely-used 11/04/2019 Java E-Commerce © Martin Cooke, 2003

47 Java E-Commerce © Martin Cooke, 2003
Summary Distributed component models focus developers’ attention on business logic Other middleware services provided by container 2 principal bean types: session & entity, representing process and persistent data Some efficiency concerns at present, but better object-relational mapping tools will help 11/04/2019 Java E-Commerce © Martin Cooke, 2003

48 Java E-Commerce © Martin Cooke, 2003
Course summary eCommerce wasn’t a blip Now in a more mature phase, but new techniques, notations, tools, etc being developed at a rapid pace Beware investment of time in recent developments -- tendency to disappear just as quickly Don’t reinvent wheel: use 3rd party services for critical aspects (eg online monetary transactions) Distributed, multi-client computing is here to stay with its associated issues: concurrency, load-balancing, security, … Architectural design is key 11/04/2019 Java E-Commerce © Martin Cooke, 2003

49 Java E-Commerce © Martin Cooke, 2003
Resources Some of the examples in this lecture are derived from a book on EJBs which is in preparation, and available for public comments, at An excellent tutorial on the use of jBoss is given in Creating a distributed Web application in Java using jBoss and Tomcat by Kevin Boone (available at Another useful tutorial is Enterprise Java Beans Fundamentals from the IBM DeveloperWorks site. 11/04/2019 Java E-Commerce © Martin Cooke, 2003


Download ppt "Enterprise Java Beans: an introduction"

Similar presentations


Ads by Google