J2EE Concepts :

Download Report

Transcript J2EE Concepts :

J2EE Concepts

Why J2EE ?

Client Client Server Server Client Database J2EE Concepts 2

Why J2EE ?

Things to consider when building large business systems :  Load Balancing     Transparent fail-over Back end integration Transactions Threading Middleware      Dynamic Deployment Object Life cycle Resource pooling Caching Security J2EE Concepts 3

J2EE Concepts :

Definition : The Java 2 Enterprise Edition (J2EE) is a multi-tiered architecture for implementing enterprise-class applications and web based applications Model : It is based on component based application model. In this model such components use services provided by the container which would otherwise typically need to be incorporated in the application code.

J2EE Concepts 4

J2EE Components

Application clients and applets are components that run on the client.

Java Servlet and JavaServer Pages technology components are Web components that run on the web server. (Web Container) Enterprise JavaBeans components (enterprise beans) are business components that run on the application server. (EJB Container) J2EE Concepts 5

J2EE Components --

continued

J2EE Concepts 6

J2EE Containers

Definition : Containers are the interface between a component and the low-level platform-specific functionality that supports the component.

Types :     Enterprise JavaBeans (EJB) container Web container Application client container Applet container J2EE Concepts 7

J2EE Container --

continued

J2EE Concepts 8

J2EE APIs

             Enterprise JavaBeans Technology JDBC API Java Servlet Technology Java Server Pages Technology Java Message Service (JMS) Java Naming and Directory Interface (JNDI) Java Transaction API (JTA) Java Mail API Java API for XML Processing (JAXP) Java API for XML Registries (JAXR) Java API for XML-Based RPC (JAX-RPC) SOAP with Attachment API for Java (SAAJ) Java Authentication and Authorization Service J2EE Concepts 9

EJB Container

Isolates the enterprise bean from direct access by client applications.

A container may provide the following services:  Lifecycle management     Transactions Security Connection pooling Instance pooling J2EE Concepts 10

J2EE Application Packaging

A J2EE application is delivered in an Enterprise Archive (EAR) file with an extension “.ear” EAR file is a standard Java Archive file (JAR) with an extension “.ear” It consists of multiple J2EE modules.

J2EE Module => one or more J2EE components for the same container type + component deployment descriptor.

Deployment Descriptor => an XML document with an

.xml

extension that describes a component's deployment settings.

J2EE Concepts 11

Enterprise JavaBeans

Definition : Enterprise Javabeans (EJBs) standard is the component architecture for deployable server side components in Java.

Values :  It is Agreed Upon by the industry   Portability is easier Rapid application development J2EE Concepts 12

Roles In EJB

Enterprise Bean provider

creates and sells EJBs 

Application assembler

uses EJBs to build an application 

EJB Server provider

creates and sells EJB server 

EJB Container provider

creates and sells EJB containers server provider will likely provide containers J2EE Concepts 13

EJB Architecture

EJB CLIENT EJB SERVER EJB Container Enterprise Bean J2EE Concepts 14

Types Of Beans Session Beans

1.

Interact with Client 2.

3.

Model Business logic Do not survive system shutdown

Entity Beans

1.

Represent persistent data 2.

Survive system shutdown J2EE Concepts 15

Session Beans Stateless

1.

No Internal state 2.

Do not need to be passivated 3.

Lifetime is controlled by the container

Stateful

1.

Possess internal state 2.

Need to be passivated/activated 3.

Assigned to one client for a lifetime J2EE Concepts 16

Entity Beans Container Managed

1.

Container is responsible for saving state.

2.

In dd, specify container managed fields 3.

Persistence is data store independent .

Bean Managed

1.

Bean is responsible for saving state 2.

Container doesn’t need to generate DB calls 3.

Less Adaptable : Persistence is hard coded in the code.

J2EE Concepts 17

What constitutes an Enterprise Bean ?

1.

2.

Enterprise Bean Class –  Implements the methods defined in the following interfaces .

 Must implement either javax.ejb.SessionBean or javax.ejb.EntityBean

Home Interface –  Define methods for creating, destroying and finding EJB objects.

 Must extend javax.ejb.EJBHome

interface J2EE Concepts 18

What constitutes an Enterprise Bean ?

3.

4.

5.

6.

Remote Interface –  Defines business logic functions that the corresponding bean class implement.

 Must extend javax.ejb.EJBObject

Deployment Descriptor – interface.

An XML file that specifies information about the bean such as type of the bean, persistence etc .

Utility/Helper Classes – Vendor Specific files.

(jboss.xml & jaws.xml) J2EE Concepts 19

Writing a Session Bean Home Interface

: import java.io.Serializable; import java.rmi.RemoteException; import javax.ejb.CreateException; import javax.ejb.EJBHome; public interface CartHome extends EJBHome { Cart create(String person) throws RemoteException, CreateException; } Cart create(String person, String id) throws RemoteException, CreateException; J2EE Concepts 20

Writing a Session Bean –

continued

Remote Interface :

The remote interface, which extends javax.ejb.EJBObject, defines the business methods that a remote client may invoke. import java.util.*; import javax.ejb.EJBObject; import java.rmi.RemoteException; public interface Cart extends EJBObject { public void addBook(String title) throws RemoteException; public void removeBook(String title) throws BookException, RemoteException; } public Vector getContents() throws RemoteException; J2EE Concepts 21

Writing a Session Bean –

continued

Bean Class :

public class CartBean implements SessionBean { String customerName; String customerId; Vector contents; public void ejbCreate(String person) throws CreateException { if (person == null) { throw new CreateException("Null person not allowed."); } else { customerName = person; } customerId = "0"; contents = new Vector(); } J2EE Concepts 22

Writing a Session Bean –

continued

public void ejbCreate(String person, String id) throws CreateException { if (person == null) { throw new CreateException("Null person not allowed."); } else { customerName = person; } IdVerifier idChecker = new IdVerifier(); if (idChecker.validate(id)) { customerId = id; } else { throw new CreateException("Invalid id: "+ id); } contents = new Vector(); } J2EE Concepts 23

Writing a Session Bean –

continued

public void removeBook(String title) throws BookException { boolean result = contents.removeElement(title); if (result == false) { throw new BookException(title + "not in cart."); } } public void addBook(String title) { contents.addElement(title); } public Vector getContents() { return contents; } J2EE Concepts 24

Writing a Session Bean –

continued

public CartBean() {} public void ejbRemove() {} public void ejbActivate() {} public void ejbPassivate() {} public void setSessionContext(SessionContext sc) {} } J2EE Concepts 25

Writing a Session Bean –

continued

Helper Classes The CartEJB session bean has two helper classes: BookException and IdVerifier. The BookException is thrown by the removeBook method and the IdVerifier validates the customerId in one of the ejbCreate methods.

J2EE Concepts 26

Writing a Session Bean –

continued

Deployment Descriptor (ejb-jar.xml)

jBoss test application Test CartBean cartHome Cart CartBean Stateless Bean J2EE Concepts 27

Session Bean Client

public static void main(String[] args) { try { InitialContext jndiContext = new InitialContext(); Object ref = jndiContext.lookup(“ejb/CartBean"); // Get a reference from this to the Bean's Home interface CartHome home = (CartHome) PortableRemoteObject.narrow (ref, CartHome .class); Cart cart = home.create(); } catch(Exception e) { System.out.println(e.toString()); } } J2EE Concepts 28

Writing an Entity Bean (CMP)

Let’s write a entity bean CD that’s represents a music CD. It contains attributes like title code id and other properties of a music CD.

Remote Interface : public interface CD extends EJBObject { public Integer getId() throws RemoteException; public void setId(Integer id) throws RemoteException; public String getTitle() throws RemoteException; public void setTitle(String title) throws RemoteException; public String getArtist() throws RemoteException; public void setArtist(String artist) throws RemoteException; public String getType() throws RemoteException; public void setType(String type) throws RemoteException; public String getNotes() throws RemoteException; public void setNotes(String type) throws RemoteException; } J2EE Concepts 29

Writing an Entity Bean --

continued

Home Interface : /** * This interface defines the home interface for the CD Bean */ public interface CDHome extends EJBHome { public CD create(Integer id) throws RemoteException, CreateException; public CD findByPrimaryKey (Integer id) throws RemoteException, FinderException; public Collection findByType (String type) throws RemoteException, FinderException; } public Collection findAll() throws RemoteException, FinderException; J2EE Concepts 30

Writing an Entity Bean --

continued

Bean Class : public abstract class CDBean implements EntityBean { public CDBean() {} public void setEntityContext( EntityContext entityContext ) throws EJBException {} public void unsetEntityContext() throws EJBException {} public void ejbRemove() throws RemoveException, EJBException {} public void ejbActivate() throws EJBException {} public void ejbPassivate() throws EJBException{} public void ejbLoad() throws EJBException{} public void ejbStore() throws EJBException{} } public abstract Integer getId(); public abstract void setId( Integer id ); public abstract String getTitle(); public abstract void setTitle( String title ); public abstract String getType(); public abstract void setType( String type ); J2EE Concepts 31

Writing an Entity Bean --

continued

Deployment Descriptor (ejb-jar.xml) CDEJB CDHome CD CDBean Container java.lang.Integer False 2.x J2EE Concepts 32

Writing an Entity Bean --

continued

id title type artist id J2EE Concepts 33

Writing an Entity Bean --

continued

Jboss.xml

CDBean cd/CD J2EE Concepts 34

Writing an Entity Bean --

continued

JAWS – Just Another Web Store JAWS is the O/R mapper used by JBoss to manage CMP entity beans.

JAWS is configured in a file named standardjaws.xml, located in the conf/config-name directory in the JBoss distribution. This file configures JAWS for all JBoss. What can you do with JAWS.xml ?

Specify a datasource and the type-mappings to use with it Set a bunch of options concerning jaws behavior Specify how JAWS should build/use your tables Define finders to access you entity beans Define a type mapping J2EE Concepts 35

JAWS

java:/MySqlDS mySQL CDBean CLASS false id ID J2EE Concepts 36

JAWS

ClassBean ...

title Title VARCHAR VARCHAR(100) ... J2EE Concepts 37