Java E-Commerce Martin Cooke, 2002 1



Similar documents
EJB & J2EE. Component Technology with thanks to Jim Dowling. Components. Problems with Previous Paradigms. What EJB Accomplishes

Enterprise JavaBeans Fundamentals

Introduction to Entity Beans

Aufgabenstellung. Aufgabenstellung

How To Write A Bean In Java (Java) (Java 2.4.2) (Java.Net) (Javax) 2 (Java Bean) ( Java Bean) 2-4.5

J2EE Web Development. Agenda. Application servers. What is J2EE? Main component types Application Scenarios J2EE APIs and Services.

WebOTX Application Server

Automatic generation of distributed dynamic applications in a thin client environment

EJB 3.0 and IIOP.NET. Table of contents. Stefan Jäger / stefanjaeger@bluewin.ch

Distributed Objects and Components

Java 2 Platform, Enterprise Edition (J2EE): Enabling Technologies for EAI

Java 2 Platform, Enterprise Edition (J2EE) Bruno Souza Java Technologist, Sun Microsystems, Inc.

IBM Rational Rapid Developer Components & Web Services

CGI Vs. Java - Which is Better For Marketing

MDA Overview OMG. Enterprise Architect UML 2 Case Tool by Sparx Systems by Sparx Systems

How to Build an E-Commerce Application using J2EE. Carol McDonald Code Camp Engineer

CACHÉ: FLEXIBLE, HIGH-PERFORMANCE PERSISTENCE FOR JAVA APPLICATIONS

25 May Code 3C3 Peeling the Layers of the 'Performance Onion John Murphy, Andrew Lee and Liam Murphy

What Is the Java TM 2 Platform, Enterprise Edition?

Glassfish, JAVA EE, Servlets, JSP, EJB

Load Balancing in Cluster

Contents. Client-server and multi-tier architectures. The Java 2 Enterprise Edition (J2EE) platform

Chapter 4. Architecture. Table of Contents. J2EE Technology Application Servers. Application Models

Smalltalk in Enterprise Applications. ESUG Conference 2010 Barcelona

Virtual Credit Card Processing System

Web Application Architecture (based J2EE 1.4 Tutorial)

Java EE 5, 6 et les EJBs 3.1

Enterprise Application Integration

How To Test A Load Test On A Java Testnet 2.5 (For A Testnet) On A Test Server On A Microsoft Web Server On An Ipad Or Ipad (For An Ipa) On Your Computer Or Ipa

Clustering with Tomcat. Introduction. O'Reilly Network: Clustering with Tomcat. by Shyam Kumar Doddavula 07/17/2002

Oracle WebLogic Server

Oracle WebLogic Foundation of Oracle Fusion Middleware. Lawrence Manickam Toyork Systems Inc

Enterprise Applications

JBS-102: Jboss Application Server Administration. Course Length: 4 days

Module 13 Implementing Java EE Web Services with JAX-WS

Remote Method Invocation in JAVA

Java EE 7: Back-End Server Application Development

Applets, RMI, JDBC Exam Review

Password-based authentication

The Evolution to Components. Components and the J2EE Platform. What is a Component? Properties of a Server-side Component

Chapter 5 Application Server Middleware

Sun Certified Enterprise Architect for J2EE Technology Study Guide

Services. Custom Tag Libraries. Today. Web Development. Role-Based. Development. Code Reuse. Tag Libraries Custom Tags. Tag Lifecycle.

Client-Server Architecture & J2EE Platform Technologies Overview Ahmed K. Ezzat

SOFT 437. Software Performance Analysis. Ch 5:Web Applications and Other Distributed Systems

JSLEE and SIP-Servlets Interoperability with Mobicents Communication Platform

A framework for web-based product data management using J2EE

White paper. IBM WebSphere Application Server architecture

Java E-Commerce Martin Cooke,

How To Protect Your Computer From Being Hacked On A J2Ee Application (J2Ee) On A Pc Or Macbook Or Macintosh (Jvee) On An Ipo (J 2Ee) (Jpe) On Pc Or

Mobile Application Languages XML, Java, J2ME and JavaCard Lesson 04 Java

Detailed Table of Contents

CONSUMER DEMAND MONITORING AND SALES FORECASTING (CDMFS) SYSTEM

CrownPeak Java Web Hosting. Version 0.20

JAVA ENTERPRISE IN A NUTSHELL. Jim Farley and William Crawford. O'REILLY 4 Beijing Cambridge Farnham Koln Paris Sebastopol Taipei Tokyo.

COM 440 Distributed Systems Project List Summary

A Comparison of Software Architectures for E-Business Applications

Case Studies of Running the Platform. NetBeans UML Servlet JSP GlassFish EJB

How To Understand The Architecture Of Java 2Ee, J2Ee, And J2E (Java) In A Wordpress Blog Post

Monitoring Pramati EJB Server

Network Communication

Chapter 2 TOPOLOGY SELECTION. SYS-ED/ Computer Education Techniques, Inc.

Building Web Services with Apache Axis2

JSP Java Server Pages

JAVA Technologies QUARTER 1 DESKTOP APPLICATIONS - ESSENTIALS QUARTER 2 NETWORKING AND OPERATING SYSTEMS ESSENTIALS. Module 1 - Office Applications

H2O A Lightweight Approach to Grid Computing

ON-LINE BOOKING APPLICATION NEIL TAIT

Moving beyond hardware

PERFORMANCE MONITORING OF JAVA COMPONENT-ORIENTED DISTRIBUTED APPLICATIONS

OptimalJ Foundation. PSM EJB Model. Roadmap. What is the EJB model? EJB model as a PSM model Mapping the EJB model Model elements and code generation

Java EE Introduction, Content. Component Architecture: Why and How Java EE: Enterprise Java

Expert One-on-One J2EE Design and Development

An Overview of Java. overview-1

Tomcat 5 New Features

Japan Communication India Skill Development Center

Developing an EJB3 Application. on WebSphere 6.1. using RAD 7.5

Building Web Applications, Servlets, JSP and JDBC

Lecture 7: Java RMI. CS178: Programming Parallel and Distributed Systems. February 14, 2001 Steven P. Reiss

Internet Engineering: Web Application Architecture. Ali Kamandi Sharif University of Technology Fall 2007

Java Server Programming: Principles and Technologies. Subrahmanyam Allamaraju, Ph.D.

Japan Communication India Skill Development Center

Understanding Application Servers

Java Network. Slides prepared by : Farzana Rahman

Transcription:

Java E-Commerce Martin Cooke, 2002 1 Enterprise Java Beans: an introduction Today s lecture Why is enterprise computing so complex? Component models and containers Session beans Entity beans Why is enterprise computing so complex? Core code transactions persistence authorisation session management 13/02/2004 Java E-Commerce Martin Cooke, 2003 2 13/02/2004 Java E-Commerce Martin Cooke, 2003 3 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 Drawback Loss of control can impact upon efficiency A solution container enterprise bean business logic persistence transactions authorisation sessions object pooling load-balancing network services middleware services 13/02/2004 Java E-Commerce Martin Cooke, 2003 4 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) 13/02/2004 Java E-Commerce Martin Cooke, 2003 5 Containers and components

Java E-Commerce Martin Cooke, 2002 2 Definitions Definitions Component model Component reusable software building block often customisable Container shell in which the component executes provides services all communication with component goes via container The relationship between a container and its components is analogous to that between a webserver and servlets a browser and applets Defines structure of interfaces and mechanisms by which it interacts, both with container and with client applications Components are called Enterprise Java Beans 13/02/2004 Java E-Commerce Martin Cooke, 2003 7 13/02/2004 Java E-Commerce Martin Cooke, 2003 8 13/02/2004 Java E-Commerce Martin Cooke, 2003 9 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 13/02/2004 Java E-Commerce Martin Cooke, 2003 10 Comparison with java beans Java bean Typically used for widgets, controls Manipulated using visual tools Configured at compile time Run on one machine, in one address space Enterprise java bean Typically used for largergrained business processes and objects Can be configured at deploy time 13/02/2004 Java E-Commerce Martin Cooke, 2003 11 Client Anything: webapp, app or another EJB Home interface Used to control the lifecycle methods of the bean: create, remove, find; uses JNDI Remote interface Exposes methods that make the bean tick Deployment descriptor Defines security, transactional, behaviour Using the bean Client app create find remove Business methods Container Home interface Remote interface 13/02/2004 Java E-Commerce Martin Cooke, 2003 12 EJB Deployment descriptor

Java E-Commerce Martin Cooke, 2002 3 Two types of EJB 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 entity 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 session 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 Session beans 13/02/2004 Java E-Commerce Martin Cooke, 2003 13 13/02/2004 Java E-Commerce Martin Cooke, 2003 14 Typical uses Session beans: 2 flavours Session bean lifecycle Price quoting Order entry Compression Complex calculations Database operations Credit card verification ie business logic 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 Source: middleware-company.com book Pooling Number of instances < number of current clients due to thinking time Eg threads, socket connections, database connections, Concurrency All methods are threadsafe (one-at-a-time) Extra beans created to handle bottlenecks 13/02/2004 Java E-Commerce Martin Cooke, 2003 16 13/02/2004 Java E-Commerce Martin Cooke, 2003 17 13/02/2004 Java E-Commerce Martin Cooke, 2003 18

Java E-Commerce Martin Cooke, 2002 4 A first example Remote method invocation (RMI) The home interface Simplest possible bean: stateless session hello world bean Recall that we need Home interface Remote interface Bean implementation Deployment descriptor Client app create find remove Business methods Container Home interface Remote interface EJB Deployment descriptor 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 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 13/02/2004 Java E-Commerce Martin Cooke, 2003 19 13/02/2004 Java E-Commerce Martin Cooke, 2003 20 13/02/2004 Java E-Commerce Martin Cooke, 2003 21 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 13/02/2004 Java E-Commerce Martin Cooke, 2003 22 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 13/02/2004 Java E-Commerce Martin Cooke, 2003 23 The deployment descriptor <!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejbjar_2_0.dtd"> <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> 13/02/2004 Java E-Commerce Martin Cooke, 2003 24

Java E-Commerce Martin Cooke, 2002 5 What s missing Client application Stateful session beans Simplest possible bean; many features not covered or used Vendor-specific deployment information Packaging Deployment Client-access code (next) 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 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 13/02/2004 Java E-Commerce Martin Cooke, 2003 25 13/02/2004 Java E-Commerce Martin Cooke, 2003 26 13/02/2004 Java E-Commerce Martin Cooke, 2003 27 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 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 Deployment descriptor <!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/j2ee/dtds/ejb-jar_2_0.dtd"> <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> 13/02/2004 Java E-Commerce Martin Cooke, 2003 28 13/02/2004 Java E-Commerce Martin Cooke, 2003 29 13/02/2004 Java E-Commerce Martin Cooke, 2003 30

Java E-Commerce Martin Cooke, 2002 6 Client application main Reminder 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(); Entity beans 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 13/02/2004 Java E-Commerce Martin Cooke, 2003 31 13/02/2004 Java E-Commerce Martin Cooke, 2003 33 Persistence Container-managed Simple to use Possibly very inefficient given current state-of-theart in containers Bean-managed Customisable Transactional issues Like session beans, entity beans are singlethreaded 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 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 Pooling 13/02/2004 Java E-Commerce Martin Cooke, 2003 34 13/02/2004 Java E-Commerce Martin Cooke, 2003 35 13/02/2004 Java E-Commerce Martin Cooke, 2003 36

Java E-Commerce Martin Cooke, 2002 7 BMP vs CMP Example entity bean The remote interface From WebTomorrow tutorial (see resources) Models a CD Uses container-managed persistence 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) throws RemoteException; // more get/set methods for other fields Source: the J2EE tutorial (2002), Sun 13/02/2004 Java E-Commerce Martin Cooke, 2003 37 13/02/2004 Java E-Commerce Martin Cooke, 2003 38 13/02/2004 Java E-Commerce Martin Cooke, 2003 39 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 13/02/2004 Java E-Commerce Martin Cooke, 2003 40 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; 13/02/2004 Java E-Commerce Martin Cooke, 2003 41 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() { 13/02/2004 Java E-Commerce Martin Cooke, 2003 42

Java E-Commerce Martin Cooke, 2002 8 The deployment descriptor The deployment descriptor, contd What else is in EJB? <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> <assembly-descriptor> <container-transaction> <method> <ejb-name>cd</ejb-name> <method-name>*</method-name> </method> <trans-attribute>required</trans-attribute> <container-transaction> </assembly-descriptor> Message-driven beans EJB-QL Security Network services Tools for deployment 13/02/2004 Java E-Commerce Martin Cooke, 2003 43 13/02/2004 Java E-Commerce Martin Cooke, 2003 44 13/02/2004 Java E-Commerce Martin Cooke, 2003 45 jboss.org Free widely-used jboss 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 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 13/02/2004 Java E-Commerce Martin Cooke, 2003 46 13/02/2004 Java E-Commerce Martin Cooke, 2003 47 13/02/2004 Java E-Commerce Martin Cooke, 2003 48

Java E-Commerce Martin Cooke, 2002 9 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 www.middleware-company.com 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 www.web-tomorrow.com) Another useful tutorial is Enterprise Java Beans Fundamentals from the IBM DeveloperWorks site. 13/02/2004 Java E-Commerce Martin Cooke, 2003 49