User Tools

Site Tools


at-m42:lecture15

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
at-m42:lecture15 [2009/04/25 09:35] eechrisat-m42:lecture15 [2009/04/25 11:37] eechris
Line 1: Line 1:
 ~~SLIDESHOW~~ ~~SLIDESHOW~~
-====== Business-tier Services ======+====== Enterprise Integration Tier Services ======
    
 In principle, the business logic of an enterprise application could be performed by //domain objects// which implement a model of the business concept as Plain-Old Java Objects (POJOs). In principle, the business logic of an enterprise application could be performed by //domain objects// which implement a model of the business concept as Plain-Old Java Objects (POJOs).
Line 15: Line 15:
   * [[#The key business tier services]]   * [[#The key business tier services]]
   * [[#The Java solution: Enterprise Java Beans]]   * [[#The Java solution: Enterprise Java Beans]]
-  * [[#Reimplementing PerfectTime as a session bean]]+  * [[#Reimplementing TimeHere as a session bean]]
   * [[#Is the Java solution a good solution?]]   * [[#Is the Java solution a good solution?]]
  
Line 107: Line 107:
   * **[[#The key business tier services]]**   * **[[#The key business tier services]]**
   * [[#The Java solution: Enterprise Java Beans]]   * [[#The Java solution: Enterprise Java Beans]]
-  * [[#Reimplementing PerfectTime as a session bean]]+  * [[#Reimplementing TimeHere as a session bean]]
   * [[#Is the Java solution a good solution?]]   * [[#Is the Java solution a good solution?]]
  
Line 136: Line 136:
     * Or simply to be stored in non-volatile memory.     * Or simply to be stored in non-volatile memory.
   * Your business objects want this to //just happen//!   * Your business objects want this to //just happen//!
-    * **Transparent persistence**, as discussed in the [[lecture14|last lecture]], is obviously important,+    * **Transparent persistence**, as discussed in the [[lecture15|last lecture]], is obviously important,
     * but persistence should also be a key part of transaction management and is a cornerstone of data integrity.     * but persistence should also be a key part of transaction management and is a cornerstone of data integrity.
  
Line 172: Line 172:
   * [[#The key business tier services]]   * [[#The key business tier services]]
   * **[[#The Java solution: Enterprise Java Beans]]**   * **[[#The Java solution: Enterprise Java Beans]]**
-  * [[#Reimplementing PerfectTime as a session bean]]+  * [[#Reimplementing TimeHere as a session bean]]
   * [[#Is the Java solution a good solution?]]   * [[#Is the Java solution a good solution?]]
  
Line 273: Line 273:
   * [[#The key business tier services]]   * [[#The key business tier services]]
   * [[#The Java solution: Enterprise Java Beans]]   * [[#The Java solution: Enterprise Java Beans]]
-  * **[[#Reimplementing PerfectTime as a session bean]]**+  * **[[#Reimplementing TimeHere as a session bean]]**
   * [[#Is the Java solution a good solution?]]   * [[#Is the Java solution a good solution?]]
  
Line 282: Line 282:
   * Each method in the remote interface must declare java.rmi.RemoteException in its throws clause in addition to any application-specific exceptions.    * Each method in the remote interface must declare java.rmi.RemoteException in its throws clause in addition to any application-specific exceptions. 
   * Any object passed as an argument or return value (either directly or embedded within a local object) must be a valid RMI-IIOP data type (this includes other EJB objects).   * Any object passed as an argument or return value (either directly or embedded within a local object) must be a valid RMI-IIOP data type (this includes other EJB objects).
-  * In EJB 3, the details can be left to the container: interface is a plain-old-Java interface //annotated//  with ''javax.ejb.Remote''. 
-   
-===== Remote interface for the DateHere EJB ===== 
  
-<code java 1|Example 1: Remote interface for the DateHereBean (at-m42/Examples/lecture14/TimeHere.java)> +===== Remote interface for the DateHere EJB (EJB 2 version) ===== 
-extern> http://cpjobling.org.uk/~eechris/at-m42/Examples/lecture14/TimeHere.java+ 
 +<code java 1| Example 1: Remote interface for the DateHereBean (EJB 2)> 
 +import java.rmi.*; 
 +import javax.ejb.*; 
 + 
 +package uk.ac.swan.atm42.ejb; 
 + 
 +public interface TimeHere extends EJBObject { 
 +  public String getTimeHere() throws RemoteException; 
 +}
 </code> </code>
 +
 +===== Remote interface for the DateHere EJB (EJB 3 version) =====
 +
 +In EJB 3, the details can be left to the container: interface is a plain-old-Java interface //annotated//  with ''javax.ejb.Remote''.
 +  
 +<code java 1|Example 2: Remote interface for the DateHereBean (at-m42/Examples/lecture15/TimeHere.java)>
 +extern> http://cpjobling.org.uk/~eechris/at-m42/Examples/lecture15/TimeHere.java
 +</code>
 +
  
 ===== The Home interface ===== ===== The Home interface =====
Line 295: Line 310:
   * It can define create methods, to create instances of EJBs   * It can define create methods, to create instances of EJBs
   * //finder// methods, which locate existing EJBs and are used for Entity Beans only.   * //finder// methods, which locate existing EJBs and are used for Entity Beans only.
 +  
 ===== The Home Interface Specification ===== ===== The Home Interface Specification =====
  
Line 303: Line 319:
   - The return value of a //finder method// (Entity Beans only) must be a Remote Interface or //java.util.Enumeration// or //java.util.Collection//   - The return value of a //finder method// (Entity Beans only) must be a Remote Interface or //java.util.Enumeration// or //java.util.Collection//
   - Any object passed as an argument (either directly or embedded within a local object) must be a valid RMI-IIOP data type (this includes other EJB objects)   - Any object passed as an argument (either directly or embedded within a local object) must be a valid RMI-IIOP data type (this includes other EJB objects)
-  - In EJB 3 the home interface is provided automatically by the container.+  - In EJB 3 the Home interface is provided automatically by the container and you don't need to provide code. 
 + 
 +===== Home interface for the TimeHere (EJB 2) ===== 
 +<code java 1|Example 3: Remote interface for the DateHereBean (at-m42/Examples/lecture15/TimeHereHome.java)> 
 +extern> http://cpjobling.org.uk/~eechris/at-m42/Examples/lecture15/TimeHereHome.java 
 +</code> 
 + 
 +===== Business Logic =====
  
-Home interface for the TimeHere (EJB 2) 
- // Home Interface of PerfectTimeBean. import java.rmi.*; import javax.ejb.*; public interface PerfectTimeHome extends EJBHome {   public PerfectTime create() throws     CreateException, RemoteException; } 
-Business Logic 
 You can now implement the business logic. When you create your EJB implementation class, you must follow these guidelines: You can now implement the business logic. When you create your EJB implementation class, you must follow these guidelines:
-The class must be public.  +  - The class must be ''public''.  
-The class must implement an EJB interface (either javax.ejb.SessionBean or javax.ejb.EntityBean).  +  The class must implement an EJB interface (either ''javax.ejb.SessionBean'' or ''javax.ejb.EntityBean'').  
-The class should define methods that map directly to the methods in the Remote interface. Note that the class does not implement the Remote interface; it mirrors the methods in the Remote interface but does not throw java.rmi.RemoteException.  +  The class should define methods that map directly to the methods in the Remote interface. Note that the class does not implement the Remote interface; it mirrors the methods in the Remote interface but does not throw ''java.rmi.RemoteException''  - Define one or more ''ejbCreate()'' methods to initialize your EJB.  
-Define one or more ejbCreate( ) methods to initialize your EJB.  +  The return value and arguments of all methods must be valid RMI-IIOP data types.
-The return value and arguments of all methods must be valid RMI-IIOP data types. +
-PerfectTimeBean (Session Bean) +
- // Simple Stateless Session Bean // that returns current system time. import java.rmi.*; import javax.ejb.*; public class PerfectTimeBean implements SessionBean {   private SessionContext sessionContext;   //return current time   public long getPerfectTime() {     return System.currentTimeMillis();   }   // EJB methods   public void ejbCreate() throws CreateException {}   public void ejbRemove() {}   public void ejbActivate() {}   public void ejbPassivate() {}   public void setSessionContext(SessionContext ctx) {     sessionContext = ctx;   } +
-  } +
-Deployment Descriptor +
-An XML file that describes the EJB component. Should be stored in a file called ejb-jar.xml. <?xml version="1.0" encoding="Cp1252"?> <!DOCTYPE ejb-jar PUBLIC '-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 1.1//EN' 'http://java.sun.com/j2ee/dtds/ejb-jar_1_1.dtd'> <ejb-jar>   <description>Example for Chapter 15</description>   <display-name></display-name>   <small-icon></small-icon>   <large-icon></large-icon>   <enterprise-beans>     <session>       <ejb-name>PerfectTime</ejb-name>       <home>PerfectTimeHome</home>       <remote>PerfectTime</remote>       <ejb-class>PerfectTimeBean</ejb-class>       <session-type>Stateless</session-type>       <transaction-type>Container</transaction-type>     </session>   </enterprise-beans>   <ejb-client-jar></ejb-client-jar> </ejb-jar> +
-Deploying the EJB +
-The files must be archived inside a standard Java Archive (JAR) file. The deployment descriptors should be placed inside the /META-INF sub-directory of the Jar file. +
-Once the EJB component is defined in the deployment descriptor, the deployer should then deploy the EJB component into the EJB Container. +
-The deployment process is quite “GUI intensive” and specific to the individual EJB Container. +
-The deployment process creates some client stubs for calling the EJB component. These classes should be placed on the classpath of the client application. +
-When a client program wishes to invoke an EJB, it must look up the EJB component inside JNDI and obtain a reference to the home interface of the EJB component. The Home interface is used to create an instance of the EJB.  +
-The Client +
-Here a simple Java program but could just as easily be a servlet, a JSP or even a CORBA or RMI distributed object. // Client program for PerfectTimeBean public class PerfectTimeClient {   public static void main(String[] args) throws Exception {     // Get a JNDI context using     // the JNDI Naming service:     javax.naming.Context context = new       javax.naming.InitialContext();     // Look up the home interface in the     // JNDI Naming service:     Object ref = context.lookup("perfectTime");     // Cast the remote object to the home interface:     PerfectTimeHome home = (PerfectTimeHome)       javax.rmi.PortableRemoteObject.narrow(         ref, PerfectTimeHome.class); +
-The Client … cont +
-    // Create a remote object from the home interface:     PerfectTime pt = home.create();     // Invoke getPerfectTime()     System.out.println(       "Perfect Time EJB invoked, time is: " +         pt.getPerfectTime());   } } +
  
-=====  Lecture Content =====+===== TimeHereBean (Session Bean) EJB 2 Version =====
  
-  * [[#An example]] +<code java 1| Example 4SessionBean implemented using EJB 2 conventions> 
-  * [[#The key business tier services]] +// Simple Stateless Session Bean 
-  * [[#The Java solutionEnterprise Java Beans]] +// that returns current system time.
-  * [[#Reimplementing PerfectTime as a session bean]] +
-  * **[[#Is the Java solution a good solution?]]**+
  
 +package uk.ac.swan.atm42.ejb;
  
 +import java.rmi.*;
 +import javax.ejb.*;
  
-===== Is the Java Solution a Good Solution? ===== +public class TimeHereBean implements SessionBean { 
-During 2004, the developer community decided that the answer is probably no. +  private SessionContext sessionContext; 
-We will discuss this issue, and some of the alternatives in the final lecture+  //return time here   
-Eckel’s PerfectTime session EJB example gives a flavour of the complexity! To run the example, you’ll need an implementation of a J2EE container to which you can deploy the bean. +  public String getTimeHere() { 
-It’s a good example of a bad design if nothing else!+    return new Date().toString(); 
 +    
 +  // EJB methods  
 +  public void ejbCreate() throws CreateException {}   
 +  public void ejbRemove() {} 
 +  public void ejbActivate() {} 
 +  public void ejbPassivate() {} 
 +  public void setSessionContext(SessionContext ctx) { 
 +    sessionContext = ctx; 
 +  } 
 +} 
 +</code>
  
-=====  Lecture Content =====+===== Version 3 EJBs =====
  
-  * [[#An example]] +  * Most of the code in previous example is "//boiler plate//" providiing "hooks" for the container.  
-  * [[#The key business tier services]] +  * EJB uses Java 5 annotations to enable the container to provide its own hooks. 
-  * [[#The Java solution: Enterprise Java Beans]] +  * EJB is now "Plain Old Java Object" (POJO)) annotated with ''javax.ejb.Stateless''
-  * [[#Reimplementing PerfectTime as session bean]] +
-  * [[#Is the Java solution a good solution?]]+
  
 +===== TimeHereBean (Session Bean) EJB 3 Version =====
 +<code java 1| Example 5: SessionBean implemented using EJB 3 conventions (at-m42/Examples/lecture15/TimeHereBean.java)>
 +extern> http://www.cpjobling.org.uk/~eechris/at-m42/Examples/lecture15/TimeHereBean.java
 +</code>
  
 +===== Deployment Descriptor (EJB 2) =====
  
-----+An XML file that describes the EJB component. Should be stored in a file called ''ejb-jar.xml''
 +<code xml 1|Example 6: Deployment Descriptor for the TimeHereBean (not required in EJB 3 containers) (at-m42/Examples/lecture15/ejb-jar.xml)> 
 +extern> http://www.cpjobling.org.uk/~eechris/at-m42/Examples/lecture15/ejb-jar.xml 
 +</code> 
 +===== Deploying the EJB =====
  
-[[Home]] [[lecture14|Previous Lecture]] | [[Lectures]] | [[lecture16|Next Lecture]] +  * The files must be archived inside a standard Java Archive (JAR) file. The deployment descriptors (if used) should be placed inside the ''/META-INF'' sub-directory of the Jar file. 
 +  * Once the EJB component is defined in the deployment descriptor, the deployer should then deploy the EJB component into the EJB Container. 
 +  * The deployment process is quite "GUI intensive2 and specific to the individual EJB Container. 
 +  * The deployment process creates some client stubs for calling the EJB component. These classes should be placed on the CLASSPATH of the client application. 
 +  * When a client program wishes to invoke an EJB it must look up the EJB component inside JNDI and obtain a reference to the home interface of the EJB component. The Home interface is used to create an instance of the EJB.  
 + 
 +===== The Client ===== 
 + 
 +  * Here a simple Java program but could just as easily be a servlet, a JSP or even a CORBA or RMI distributed object. 
 + 
 +<code java 1|Example 7: Client program for TimeHereBean (at-m42/Examples/lecture15/TimeHereClient.java)> 
 +extern> http://www.cpjobling.org.uk/~eechris/at-m42/Examples/lecture15/TimeHereClient.java 
 +</code> 
 +<code java 1Example 7: Client program for TimeHereBean (at-m42/Examples/lecture15/TimeHereClient.java)>  
 +extern> http://www.cpjobling.org.uk/~eechris/at-m42/Examples/lecture15/TimeHereClient.java 
 +</code>
  
 =====  Lecture Content ===== =====  Lecture Content =====
Line 365: Line 403:
   * [[#The key business tier services]]   * [[#The key business tier services]]
   * [[#The Java solution: Enterprise Java Beans]]   * [[#The Java solution: Enterprise Java Beans]]
-  * [[#Reimplementing PerfectTime as a session bean]]+  * [[#Reimplementing TimeHere as a session bean]]
   * **[[#Is the Java solution a good solution?]]**   * **[[#Is the Java solution a good solution?]]**
- 
- 
  
 ===== Is the Java Solution a Good Solution? ===== ===== Is the Java Solution a Good Solution? =====
-During 2004, the developer community decided that the answer is probably no. 
-We will discuss this issue, and some of the alternatives in the final lecture. 
-Eckel’s PerfectTime session EJB example gives a flavour of the complexity! To run the example, you’ll need an implementation of a J2EE container to which you can deploy the bean. 
-It’s a good example of a bad design if nothing else! 
  
-=====  Lecture Content =====+  * During 2004, the developer community decided that the answer is probably no. 
 +  * EJB 3 was a response to this, and uses annotations to simplify it. 
 +  * We will discuss this issue, and some of the alternatives in the final lecture. 
 +  * The TimeHere session EJB example gives a flavour of the complexity! To run the example, you’ll need an implementation of a Java EE container (e.g. Glassfish) to which you can deploy the bean. 
 + 
 +=====  Lecture Summary =====
  
   * [[#An example]]   * [[#An example]]
   * [[#The key business tier services]]   * [[#The key business tier services]]
   * [[#The Java solution: Enterprise Java Beans]]   * [[#The Java solution: Enterprise Java Beans]]
-  * [[#Reimplementing PerfectTime as a session bean]]+  * [[#Reimplementing TimeHere as a session bean]]
   * [[#Is the Java solution a good solution?]]   * [[#Is the Java solution a good solution?]]
  
Line 388: Line 425:
 ---- ----
  
-[[Home]] | [[lecture14|Previous Lecture]] | [[Lectures]] | [[lecture16|Next Lecture]] +[[Home]] | [[lecture15|Previous Lecture]] | [[Lectures]] | [[lecture16|Next Lecture]]  
 + 
at-m42/lecture15.txt · Last modified: 2011/01/14 12:45 by 127.0.0.1