brad rippe fullerton college. what you need to get started? jdk 1.3 standard for compilation j2ee -...

61
Brad Rippe Fullerton College

Upload: marilyn-blankenship

Post on 27-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

Brad RippeFullerton College

Page 2: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web

Container Example uses Jboss 2.4 with

Tomcat 3.2.3 A good editor

Page 3: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

What is a EJB? JavaBean? Java Classes? GUI? Are EJBs part of the J2EE? What is J2EE?

Page 4: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

What is a EJB? Enterprise JavaBeans (EJBs) are

distributed object that are hosted in Enterprise JavaBean Containers and provide remote services for clients distributed throughout the network.

This components encapsulate business logic

Page 5: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

Why EJB? (Distributed Computing)

Enterprise Applications Development Costs Deployment Costs Maintenance Costs Service More Clients More Bang for the Buck!

Page 6: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

J2EE Architecture

This is an illustration of the architecture set forth by Sun. See

http://java.sun.com/blueprints/

Page 7: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

Containers and Services EJB Containers provide additional

services for the (EJBs). Life-Cycle Management,

Transaction Management, Security, Persistence, Resource Management.

Page 8: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

EJB Container Similar to the Web Container EJBs require a container to function The contain isolates the EJB from

direct access from client application. Manages remote access to EJBs. Provided by an Application Server.

JBoss, WebLogic, JRun, Borland App Server, etc.

Page 9: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

EJB Advantages Productivity

Container services are provided automatically. Developer can focus on the business logic

without Infrastructure

Container management is inherently robust. Supports scalability

Portability EJB Spec provides a well-defined contract for EJB

Containers

Page 10: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

How does the EJB get the services? EJBs can access container services

through one of three ways

Callback methods The EJBContext interface Java Naming and Directory Interface

Page 11: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

CallBack Methods Each EJB is required to implement a

subtype of EnterpriseBean interface which defines callback methods. Each callback method provides a way for

the container to notify the EJB about an event in the bean’s lifecycle, i.e. removing a bean from memory.

The callback methods give the EJB a chance to do some internal housework before or after an event occurs.

These are the bean’s event handlers.

Page 12: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

EJBContext Every EJB obtains an EJBContext

object which is a reference directly to the EJB Container.

The EJBContext interface provides methods for interacting with the container so that the EJB can request information about its environment.

Page 13: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

Java Naming and Directory Interface (JNDI) JNDI is the standard extension to the Java

platform for accessing naming systems like LDAP, NetWare, NDS, file systems.

Every EJB automatically has access to a special naming system called the Environmental Naming Context (ENC).

The ENC is managed by the container. It allows an EJB to access resources like JDBC

connection , other EJBs, and its own properties.

Page 14: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

Roles of Application Builders Bean Provider

an application developer and, often, a domain expert – builds reusable components without focusing on the framework.

Assembler Combines finished EJBs into modules and

combines those and other J2EE building blocks into applications, making container neutral-decisions.

Deployer Deploys J2EE applications in a specific

environment, and make container-specific decisions.

Page 15: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

Two Main Types of Beans Session Beans

Stateless calculating sales tax, or processing and order

Stateful Common Shopping cart component

Entity Beans CMP (Container-Managed Persistence) BMP (Bean-Managed Persistence)

Message-Driven Beans (Introduced in 2.0) This is a JMS bean. Designed for sending and

receiving JMS messages.

Page 16: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

Creating EJBs All ejbs implement a subtype of EnterpriseBean. Either SessionBean, EntityBean, or MessageDrivenBean.

Each of the subInterfaces declares callback methods for the container.

To create an EJB a developer provides: A home interface

defines the life-cycle methods of the bean A remote interface

defines the business methods of the bean class. A bean class

business logic, the meat is here

Page 17: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

Conceptual Model

Bean Class

Bean Class

Home

RemoteClient

Home

Remote

Application Server

EJB Container

Page 18: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

Home Interface Extends javax.ejb.EJBHome This interface declares create and

find methods. EJB Container implements this

interface Clients use JNDI to locate the

vendors home class.

Page 19: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

Remote Interface Extends javax.ejb.EJBObject Client view and get access to the EJB

through the bean’s remote interface. Methods a client can call are declared

here. The actually implementation of those

business methods is located in the bean class.

Gives a client a handle to the EJB

Page 20: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

How the do interfaces work? The container creates a class that

implements the Home interface and makes it available to JNDI.

The container creates a class that implements the Remote interface which acts like a middleman between the bean and the client.

Page 21: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

Bean Class Implements javax.ejb.EntityBean if

it is an EntityBean Implements javax.ejb.SessionBean

if it is an SessionBean This class defines the methods

declared in the Home and Remote interfaces.

Defines finder, create and business methods of the EJB.

Page 22: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

Enity Beans versus Session Beans Entity Beans

Persistent Part of permanent storage Should communicate with Session Beans Should not communicate with clients Read and write access to the data store

Session Beans Not persistent Does not survive server crash Can access the database for queries Communicates with client via interfaces

Page 23: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

Session Bean Session Beans should be used for short

requests that can be satisfied with one method.

Session Beans require low resource costs Easy for the container to manage Promotes fast response back to the client Can be stateful or stateless

Client receive only one stateful bean for service Clients share stateless beans

Two type of transaction modes CMT – Container Managed Transaction BMT – Bean Managed Transaction

Page 24: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

Entity Bean Has a direct relation to database row. Database data types are converted into java data

types and encapsulated into the Entity Bean. Entity beans must have a defined primary key

data type or compound object as its primary key. Require more overhead to maintain state

between the database and the EJB object. Entity bean have persistent data. Persistence can be one of two persistence modes:

CMP – Container-Managed Persistence BMP – Bean-Managed Persistence

Page 25: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

Scenario 1 – Session Bean Example

Client Session Bean Data Store

Session Bean

Page 26: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

Scenario 2 – Entity Bean Example

Client

Session Bean

Data Store

Session Bean

Entity Bean

Entity Bean

Page 27: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

Building a Session Bean Example uses one Session Bean, two

different clients to access the bean’s business methods

Business method – calculateStockPrice( String ticker, int numShares ) Calculates the price of stock for four different

companies.

Page 28: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

Where to begin? First, download Jboss –

http://www.jboss.org or some other app server. App Server must support EJBs. Version integrated with Tomcat is preferred!

Second, unzip the archive into the directory where it will be located permanently on your server. I chose a directory like “e:\appServer\”

Third, on to the code…

Page 29: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

StockBalanceBean Example Requires a home interface Requires a remote interface Requires the bean class Requires the Deployment Descriptor Some packaging? A client or two Deploy

Page 30: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

StockBalanceBean Home Interface

package edu.fullcoll.exampleEJB;

import java.rmi.RemoteException;

public interface StockBalanceHome extends EJBHome {

StockBalance create() throws CreateException, EJBException, RemoteException;

}

Page 31: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

StockBalanceHome Home Interface Provides lifecycle methods for

creating, destorying and locating EJBs Separate from the remote interface

because the home interface is not associated with one instance of an EJB

Home interface extend javax.ejb.EJBHome interface

Page 32: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

StockBalance Remote Interface

package edu.fullcoll.exampleEJB;import java.rmi.RemoteException;

public interface StockBalance extends EJBObject{

double calculateStockPrice(String ticker, int numShares) throws RemoteException, EJBException;

}

Page 33: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

StockBalance Remote Interface Declares the business methods

available in EJB class. The this is the clients way of

communicating with the EJB. The EJB container creates an object that

implements this remote interface and returns it to the client.

Can be associated with one instance of an EJB.

Page 34: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

StockBalanceBean – Bean Class

public class StockBalanceBean implements SessionBean {public double calculateStockPrice(String ticker, int numShares){

if( ticker != null) { ticker = ticker.toUpperCase(); if(ticker.equals( "AAPL" )) { return numShares * 20.42; } else if(ticker.equals( "MSFT" )) { return numShares * 64.84;

} else if(ticker.equals( "YHOO" )) { return numShares * 16.70;

} else if(ticker.equals( "SUNW" )) { return numShares * 13.84; } } return 0.0; }}

Page 35: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

StockBalanceBean Implements SessionBean interfacepublic void ejbActivate() { }

public void ejbPassivate() { }

public void setSessionContext(SessionContext ctx) {}

public void ejbRemove() {}

public void ejbCreate() {}

Page 36: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

EJBs and RMI The remote and home interfaces are

types of Java RMI remote interfaces. This means that the EJB, even though

instantiated in the EJB container, can have its methods invoked as a result of a request from an outside application.

The RMI stub and skeleton hide the communication specifics from the client.

Page 37: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

EJBs and RMI

StubStub

Skeleton

EJB ObjectEJB

Object

Client Network App Server

Page 38: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

Final Steps Home, Remote and Bean class are

created. Create a deployment descriptor Package the EJBs Deploy the beans to the App

Server

Page 39: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

ejb-jar.xml Must be stored in the jar’s META-INF

directory. XML document. Describes the EJB setup, transaction mode,

JNDI name, security, and persistence. Can be created by hand, not

recommended. J2EE deploytool Another GUI tool to generate the xml…

Recommended…

Page 40: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

ejb-jar.xml Let’s take a look…

Page 41: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

Packaging the EJBs Again you can use a tool like Together’s

Control Center or the deploytool. Or Create a jar file with your ejb classes in

it and the deployment descriptor in the META-INF directory. This file will have a .jar extension.

Name the jar an arbitrary name, StockBeanEJBs.jar

Page 42: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

Creating a client - local Let’s take a look…

Page 43: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

Creating a client - Remote Let’s take a look…

Page 44: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

How does a client lookup a bean? A client needs to know two things:

The JNDI name of the Bean The vendor-specific syntax for getting

the InitialContext.

Page 45: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

Deployment To deploy the bean in jboss, simply

copy the EJB jar to the “deploy” directory.

2.4 handles hot deploy, so if you update your EJBs you can copy the new jar into the “deploy” directory and the EJBs will be updated…

Page 46: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

Info about client compilation Your client code must be compiled with

the following jars. ejb.jar – standard javax.ejb.* jaas.jar – Java security classes jbosssx-client.jar – JBossSX security

classes jboss-client.jar – EJB container proxy and

stub classes jnp-client.jar – jboss JNDI provider client

classes

Page 47: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

Entity Beans Provides object representation of

data. One entity bean can represent data

for multiple clients. Represents a row in the database. Model business objects, nouns,

Person, Seat, Room, etc. Container handles persistence,

transactions, and access control

Page 48: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

Two types of Entity Beans Bean-Managed Persistence (BMP)

Develop provides all of the code for managing persistence between the object and the database. The container will notify the bean when its necessary to update or read from the database.

Container-Managed Persistence (CMP) The EJB Container handles the relationship

between the bean and the database. Bean developer can focus on the data and the business process.

Page 49: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

Using Entity Beans CMP

Recommended for beginners Handles simple relationships with the

database. (one row) BMP

Used for more complex relationships. Beans that represent multiple rows or table

joins. This code is implemented by the developer

Page 50: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

Entity Bean Requirements Home Interface Remote Interface Bean class – implements EntityBean

Primary Key – can be a java class or primitive type. Points to a unique record in the database.

All Entity beans must have a primary key that is serializable.

Page 51: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

Creation The entity bean’s home interface declares

a method create(). The bean class must define methods ejbCreate() and ejbPostCreate().

ejbCreate() and ejbPostCreate() must have the same parameters as create() from the home interface.

create() Inserts a row into the database. ejbPostCreate()- provides a method for

accessing the EJB’s remote method (this).

Page 52: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

Callback Methods setEntityContext() unsetEntityContext() ejbLoad() ejbStore() ejbActivate() ejbPassivate() ejbRemove() CMP - the container decides when to call these methods

and their implementation. BMP - the container decides when to call these methods

and the developer provide the implementation.

Page 53: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

Find Methods Find methods in the Home interface are used to

query the EJB server for specific entity beans. CMP the find methods are implemented by the

container. There isn’t any code in the bean class. Clients can call find methods to obtain a reference

to a particular bean’s remote interface. Can return a single reference or an Enumeration or

Collection of references.

FCStudentBean brad = (FCStudentBean) home.findStudent(00001149);

Page 54: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

Entity Beans Example - Home

package edu.fullcoll.schedule;import java.rmi.RemoteException;Import javax.ejb.EJBHome;

public interface FCStudentHome extends EJBHome {public FCStudentRemote create(int pidm) throws

CreateException,RemoteException;

public FCStudentRemote findByPrimaryKey(int pk) throws FinderException, RemoteException;

}

Page 55: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

Entity Beans Example - Remote

package edu.fullcoll.schedule;import java.rmi.RemoteException;Import javax.ejb.EJBObject;

public interface FCStudentRemote extends EJBObject {

public String getName() throws RemoteException;

public void setName(String n) throws RemoteException;

}

Page 56: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

Entity Beans Example – Bean Class

public class FCStudent implements EntityBeans{

public int pidm;public String name;public int ejbCreate(int pidm) {

this.pidm = pidm;return null;

}public String getName() { return name; }public void setName(String n) { name = n; }// all callback methods must be defined, but blank for CMP

}

Page 57: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

J2EE Comprised of many different

technologies JSP/Servlets JDBC JNDI JTA JMS, jetc, jetc

Page 58: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

Summary Similar to other technologies, EJBs have

there place in software development. EJBs are not a solution for all

development problems. They are meant for transactional, secure business applications, reservation systems, student registration, online purchasing.

They are highly scalable components meant for use in complex, mission-critical applications.

Page 59: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

Resources Jboss – http://www.jboss.org Tomcat – http://jakarta.apache.org/tomcat J2EE Web Site – http://java.sun.com/j2ee J2EE Tutorial –

http://java.sun.com/j2ee/tutorial EJB Spec – http://java.sun.com/products/ejb Lecture available at

http://staffwww.fullcoll.edu/brippe/cis226

Page 60: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

Resources J2EE Developer’s Guide –

http://java.sun.com/j2ee/j2sdkee/techdocs/guides/ejb/html/DevGuideTOC.html

J2EE Tutorial – http://java.sun.com/j2ee/tutorial/1_3-fcs/index.html

Server Side Programming – http://www.theserverside.com

Page 61: Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container

The End! Thanks for you time!