Transcript PowerPoint
A Case Study on the J2EE Platform
CPSC550 Graduate Student Seminar Presentation
by Jeffrey A. Brown
History
Mainframe To Enterprise
Mainframe
Dumb Terminal
Model #1
• All programs and data reside on the same machine.
– Scheme for handling many concurrent users efficiently was needed.
SOLUTION: transaction processor
Client
Database Server
Model #2
• Applications run on the client machine.
– Typically contains both presentation and business rules logic.
– Changes in business logic requires redistribution of client code.
Client
Middle
Tier
Database Server
• Database server ultimate repository of information.
• Client remains responsible for presentation logic.
• Middle tier server responsible for business rules logic.
Model #3
Transaction Processor
• Solves the following business needs:
– Guarantees transactional integrity.
• Several statements executed together as a logical unit.
• Either all statements execute successfully or none are executed.
• Uses BEGIN, COMMIT, and ROLLBACK primitives.
– Manages the execution of programs and the sharing of
resources.
• Can maintain a pool of running program instances.
– Hands instances out to users as needed.
– Size of pool based on current user load.
• Allows a system to support many more concurrent users than
possible if providing duplicate resources for each user.
What is Java?
• From The Java Language: An Overview:
Java: A simple, object-oriented, network-savvy,
interpreted, robust, secure, architecture neutral,
portable, high-performance, multithreaded,
dynamic language.
http://java.sun.com/docs/overviews/java/java-overview-1.html
Java is Network-Savvy
• Designed to support applications on the network.
• Supports various levels of network connectivity.
– URL class allows access to remote objects on the
Internet.
– Socket class supports creation of distributed clients
and servers using reliable stream network connections.
– DatagramPacket and DatagramSocket are used
to send and receive datagrams.
“Java offers the promise that the network will become the computer.”
Java is Multithreaded
• Designed to support multiple threads of execution
that can handle different tasks.
• Improves interactive performance of GUI apps.
• Provides built-in language support . . .
– Thread class in java.lang package.
• used to start/run/stop/query threads.
– Includes set of synchronization primitives . . .
• synchronized keyword.
• wait()and notify() methods.
“Java makes programming with threads much easier . . .”
Goal
The Goal of J2EE
• Enterprise application development is hard!
• J2EE comes to the rescue by simplifying enterprise
application development!
Definitions
J2EE Definitions
• JavaServer Pages (JSP)
– Provides the ability to “put snippets of Java code
directly into a text-based document. A JSP page is a
text-based document that contains two types of text:
static template data, which can be expressed in any
text-based format such as HTML, WML, and XML, and
JSP elements, which determine how the page constructs
dynamic content.”
• Servlets
– Provides the ability to “define HTTP-specific servlet
classes. A servlet class extends the capabilities of
servers that host applications accessed by way of a
request-response programming model.”
Selected J2EE Component Definitions
•
Java Messaging Service (JMS)
–
•
“A messaging standard that allows J2EE application
components to create, send, receive, and read
messages. It enables distributed communication that
is loosely coupled, reliable, and asynchronous.”
Enterprise JavaBeans (EJB)
–
Components that implement the business logic of an
enterprise application. They execute within a
“container” which provides numerous services that
simplify their development.
Selected J2EE Component Definitions
•
Java Naming and Directory Interface (JNDI)
–
•
“Provides naming and directory functionality. It
provides applications with methods for performing
standard directory operations, such as associating
attributes with objects and searching for objects
using their attributes”.
Remote Method Invocation (RMI)
–
Provides the ability for “an object running in one
Java Virtual Machine (JVM) to invoke methods on an
object running in another JVM. RMI provides for
remote communication between programs written in
the Java programming language”.
Selected J2EE Component Definitions
•
Java API for XML – Remote Procedure Call
(JAX-RPC)
–
•
“Uses the SOAP standard and HTTP so client
programs can make XML-based remote procedure
calls (RPCs) over the Internet. JAX-RPC also
supports WSDL so you can import and export WSDL
documents”.
SOAP with Attachments API for Java (SAAJ)
–
Provides “a low-level API upon which JAX-RPC
depends. It enables the production and consumption
of messages that conform to the SOAP 1.1
specification and SOAP with Attachments note”.
Selected J2EE Component Definitions
•
Java API for XML - Registries (JAXR)
–
•
Provides ability to “access business and generalpurpose registries over the Web. JAXR supports the
ebXML Registry/Repository standards and the
emerging UDDI specifications. By using JAXR,
developers can learn a single API and get access to
both of these important registry technologies”.
Java Management Extension (JMX)
–
Provides the tools “for building distributed, Webbased, modular and dynamic solutions for managing
and monitoring devices, applications, and servicedriven networks”.
Selected J2EE Component Definitions
•
Common Object Request Broker Architecture
(CORBA)
–
An open, vendor-independent architecture and
infrastructure created by the Object Management
Group (OMG) that allows a CORBA-based program
from any vendor, on almost any computer, operating
system, programming language, and network, can
interoperate with another CORBA-based program
from the same or another vendor, on almost any other
computer, operating system, programming language,
and network.
A paraphrase taken from the CORBA FAQ
See http://www.omg.org/gettingstarted/corbafaq.htm
Features
Features of J2EE
•
The J2EE platform provides support for the
following features:
–
–
–
–
–
–
–
Distributed processing.
Component life-cycle management.
Transaction processing.
Pooling of numerous resource types.
Security management.
Data persistence.
Interoperability with legacy systems.
Structure
J2EE Platform Architecture Diagram
How To Use
Enterprise Beans Overview
TX_NOT_SUPPORTED
Responsible for
managing
processes or tasks.
TX_BEAN_MANAGED
Enterprise
Bean
Transaction
Support?
TX_REQUIRED
TX_REQUIRED_NEW
TX_SUPPORTS
TX_MANDATORY
Session
Bean
Message-Driven
Bean
Entity
Bean
Processes
Asynchronous
Messages
Stateful or
Stateless?
Models business concepts that can
be expressed as nouns.
Bean Managed
Persistence?
Container Managed
EJB Architectural Structure
EJB Server
EJB Container
Home interface
EJB home stub
Client
EJB home
EJB object
bean class
EJB remote stub
Remote interface
EJB developer
EJB container
EJB Classes and Interfaces
• Remote interface
– defines the bean’s business methods.
• extends javax.ejb.EJBObject which extends java.rmi.Remote.
• Home interface
– defines the bean’s life cycle methods.
• creating, removing, and finding beans.
• extends javax.ejb.EJBHome which extends java.rmi.Remote.
• Bean class
– implements the bean’s business methods.
• Primary key class
– provides a pointer into the database (entity beans only)
An Entity Bean Example
package com.titan.cabin;
import java.rmi.RemoteException;
public interface Cabin extends javax.ejb.EJBObject {
public String getName() throws RemoteException;
public void setName(String str) throws RemoteException;
public int getDeckLevel() throws RemoteException;
public void setDeckLevel(int level) throws RemoteException;
public int getShip() throws RemoteException;
public void setShip(int sp) throws RemoteException;
public int getBedCount() throws RemoteException;
public void setBedCount(int bc) throws RemoteException;
}
Cabin bean remote interface
An Entity Bean Example
package com.titan.cabin;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
public interface CabinHome extends javax.ejb.EJBHome {
public Cabin create(int id)
throws CreateException, RemoteException;
public Cabin findByPrimaryKey(CabinPK pk)
throws FinderException, RemoteException;
}
Cabin bean home interface
An Entity Bean Example
package com.titan.cabin;
import javax.ejb.EntityContext;
public class CabinBean implements javax.ejb.EntityBean {
public
public
public
public
public
int id;
String name;
int deckLevel;
int ship;
int bedCount;
public CabinPK ejbCreate(int id){
this.id = id;
return null;
}
Cabin bean class
An Entity Bean Example
public void ejbPostCreate(int id){
// Do nothing. Required.
}
public String getName(){
return name;
}
public void setName(String str){
name = str;
}
public int getShip(){
return ship;
}
public void setShip(int sp) {
ship = sp;
}
Cabin bean class (cont.)
An Entity Bean Example
public int getBedCount(){
return bedCount;
}
public void setBedCount(int bc){
bedCount = bc;
}
public int getDeckLevel(){
return deckLevel;
}
public void setDeckLevel(int level ){
deckLevel = level;
}
public void setEntityContext(EntityContext ctx){
// Not implemented.
}
public void unsetEntityContext(){
// Not implemented.
}
Cabin bean class (cont.)
An Entity Bean Example
public
//
}
public
//
}
public
//
}
public
//
}
public
//
}
void ejbActivate(){
Not implemented.
void ejbPassivate(){
Not implemented.
void ejbLoad(){
Not implemented.
void ejbStore(){
Not implemented.
void ejbRemove(){
Not implemented.
}
Cabin bean class (cont.)
An Entity Bean Example
package com.titan.cabin;
public class CabinPK implements java.io.Serializable {
public int id;
public int hashCode( ){
return id;
}
public boolean equals(Object obj){
if(obj instanceof CabinPK)
return (id == ((CabinPK)obj).id);
return false;
}
public String toString(){
return String.valueOf(id);
}
}
Cabin bean primary key class
An Session Bean Example
package com.titan.travelagent;
import java.rmi.RemoteException;
import javax.ejb.FinderException;
public interface TravelAgent extends javax.ejb.EJBObject {
// String elements follow the format "id, name, deck level"
public String [] listCabins(int shipID, int bedCount)
throws RemoteException;
}
TravelAgent bean remote interface
An Session Bean Example
package com.titan.travelagent;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
public interface TravelAgentHome extends javax.ejb.EJBHome {
public TravelAgent create()
throws RemoteException, CreateException;
}
TravelAgent bean home interface
An Session Bean Example
package com.titan.travelagent;
import
import
import
import
import
import
import
import
import
com.titan.cabin.Cabin;
com.titan.cabin.CabinHome;
com.titan.cabin.CabinPK;
java.rmi.RemoteException;
javax.naming.InitialContext;
javax.naming.Context;
java.util.Properties;
java.util.Vector;
javax.ejb.EJBException;
public class TravelAgentBean implements javax.ejb.SessionBean {
public void ejbCreate() {
// Do nothing.
}
TravelAgent bean class
An Session Bean Example
public String [] listCabins(int shipID, int bedCount)
throws EJBException{
try {
javax.naming.Context jndiContext = new InitialContext();
Object obj = jndiContext.lookup("ejb/CabinHome");
CabinHome home = (CabinHome)
javax.rmi.PortableRemoteObject.narrow(obj,
CabinHome.class);
Vector vect = new Vector();
CabinPK pk = new CabinPK();
Cabin cabin;
for(int i = 1; ; i++){
pk.id = i;
try {
cabin = home.findByPrimaryKey(pk);
}
TravelAgent bean class (cont.)
An Session Bean Example
catch(javax.ejb.FinderException fe){
break;
}
// Check to see if the bed count and ship ID match.
if (cabin.getShip() == shipID &&
cabin.getBedCount() == bedCount){
String details =
i+","+cabin.getName()+","+cabin.getDeckLevel();
vect.addElement(details);
}
}
String [] list = new String[vect.size()];
vect.copyInto(list);
return list;
}
TravelAgent bean class (cont.)
An Session Bean Example
catch(javax.naming.NamingException ne){
throw new EJBException(ne);
} catch(java.rmi.RemoteException re){
throw new EJBException(re);
}
}
private javax.naming.Context getInitialContext()
throws javax.naming.NamingException{
Properties p = new Properties();
// ... Specify the JNDI properties specific to the vendor.
return new javax.naming.InitialContext(p);
}
public void ejbRemove(){}
public void ejbActivate(){}
public void ejbPassivate(){}
public void setSessionContext(javax.ejb.SessionContext cntx){}
}
TravelAgent bean class (cont.)
An EJB Client Example
package com.titan.travelagent;
import com.titan.cabin.CabinHome;
import com.titan.cabin.Cabin;
import com.titan.cabin.CabinPK;
import
import
import
import
import
import
javax.naming.InitialContext;
javax.naming.Context;
javax.naming.NamingException;
javax.ejb.CreateException;
java.rmi.RemoteException;
java.util.Properties;
public class Client {
public static int SHIP_ID = 1;
public static int BED_COUNT = 3;
An EJB Client Example
public static void main(String [] args){
try {
Context jndiContext = getInitialContext();
Object obj = jndiContext.lookup("ejb/TravelAgentHome");
TravelAgentHome home = (TravelAgentHome)
javax.rmi.PortableRemoteObject.narrow(obj,
TravelAgentHome.class);
TravelAgent reserve = home.create();
// Get a list of all cabins on ship 1 with a
// bed count of 3.
String list [] = reserve.listCabins(SHIP_ID,BED_COUNT);
for(int i = 0; i < list.length; i++){
System.out.println(list[i]);
}
} catch(java.rmi.RemoteException re){re.printStackTrace();}
catch(Throwable t){t.printStackTrace();}
}
An EJB Client Example
static public Context getInitialContext() throws Exception {
Properties p = new Properties();
// ... Specify the JNDI properties specific to the vendor.
return new InitialContext();
}
}
J2EE: Applications
Web Application Using J2EE
J2EE Container
Web Container
UI Tier
Servlets / Web Tier Classes
Business Interface
Middle Tier
(Business Logic)
Implementation
EIS Tier
DBMS
Legacy System
Web Application Using J2EE
•
Strengths of this approach:
–
–
–
–
•
Simplicity
Speed
Easy to test
Scales well
Weaknesses of this approach:
–
–
–
Only supports web interface
Can’t use EJB transaction support
No built-in support for concurrent programming
Distributed Application Using J2EE
Web Container
UI Tier
Servlets / Web Tier Classes
Business Interface
Business Delegate
Middle Tier
J2EE Container
RMI
(Business Logic)
EJB Container
Session EJB
Entity EJB
EIS Tier
DBMS
Legacy System
Distributed Application Using J2EE
•
Strengths of this approach:
–
–
–
•
Supports all J2EE client types
Provides a shared middle tier
Permits distribution of application components across
different physical servers
Weaknesses of this approach:
–
–
–
Complex
Performance overhead
Hard to test and debug
J2EE: Significant Points
J2EE Significant Points
•
J2EE simplifies enterprise development but it's
still much harder than developing with POJO
–
–
•
Use XDoclet or EJBGen for help
Harder to test and debug EJBs than POJO
Distributed or not distributed?
–
Use local interfaces with non-distributed approach
•
•
Would be nice if EJB container could determine whether to
use local or remote interfaces based on context!
Design for scalability from start to finish
–
Keep state requirements small to non-existent
•
Stateless components much more scalable!
J2EE: Summary
J2EE Summary
• J2EE helps simplify the development of enterprise
applications.
• J2EE platform consists of numerous components
including:
– JSP, Servlets, EJB, and JMS
• J2EE provides helpful underlying services
including:
– life-cycle management, transaction processing, resource
pooling, security, and data persistence.
• J2EE can be used to create distributed applications.
References
References
• Java 2 Platform Enterprise Edition Specification, v1.4
http://java.sun.com/j2ee/j2ee-1_4-fr-spec.pdf
• The J2EE 1.4 Tutorial
http://java.sun.com/j2ee/1.4/docs/tutorial/doc/index.html
• Enterprise JavaBeans, 3rd Edition
Richard Monson-Haefel, O'Reilly & Associates, ISBN 0596002262
• Enterprise Javabeans: Developing Component-Based Distributed Applications
Thomas C. Valesky, Addison-Wesley, ISBN 0201604469
• Expert One-on-One J2EE Design and Development
Rod Johnson, WROX Press, ISBN 0764543857
• Core J2EE Patterns: Best Practices and Design Strategies
Deepak Alur, et. al., Prentice Hall, ISBN 0130648841