Technologies for Grids and eBusiness Enterprise JavaBeans 04'12'07 - PowerPoint PPT Presentation

1 / 46
About This Presentation
Title:

Technologies for Grids and eBusiness Enterprise JavaBeans 04'12'07

Description:

Technologies for Grids and eBusiness Enterprise JavaBeans 04'12'07 – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 47
Provided by: bob4137
Category:

less

Transcript and Presenter's Notes

Title: Technologies for Grids and eBusiness Enterprise JavaBeans 04'12'07


1
Technologies for Grids and eBusinessEnterprise
JavaBeans04.12.07
  • Dr. Ramin YahyapourComputer Engineering
    InstituteUniversity Dortmund

2
EJB CartBean Example (1)
  • import java.util.
  • import javax.ejb.
  • 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"

3
EJB CartBean Example (2)
  • public void addBook(String title)
  • contents.addElement(title)
  • public void removeBook(String title) throws
    BookException
  • boolean result contents.removeElement(title)
  • if (result false)
  • throw new BookException(title "not in
    cart.")
  • public Vector getContents()
  • return contents
  • public CartBean()

4
CartBean 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

5
CartBean Remote Interface
  • 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

6
Savings Account -Database Definition
  • CREATE TABLE savingsaccount
  • (id VARCHAR(3)
  • CONSTRAINT pk_savingsaccount PRIMARY KEY,
  • firstname VARCHAR(24),
  • lastname VARCHAR(24),
  • balance NUMERIC(10,2))

7
Java Application Server
8
Deployment Descriptor
lt?xml version"1.0" encoding"UTF-8"?gt ltejb-jargt
ltdescriptiongtJBoss Hello World
Applicationlt/descriptiongt ltdisplay-namegtHello
World EJBlt/display-namegt ltenterprise-beansgt
ltsessiongt ltejb-namegtHelloWorldlt/ejb-namegt lthom
egtcom.mastertech.sample.HelloWorldHomelt/homegt ltr
emotegtcom.mastertech.sample.HelloWorldlt/remotegt
ltejb-classgtcom.mastertech.sample.HelloWorldBeanlt/e
jb-classgt ltsession-typegtStatelesslt/session-typegt
lttransaction-typegtBeanlt/transaction-typegt
lt/sessiongt lt/enterprise-beansgt lt/ejb-jargt
9
Deployment Descriptorwith Query Definition
ltquerygt ltquery-methodgt ltmethod-namegtfindByN
amelt/method-namegt ltmethod-paramsgt
ltmethod-paramgtjava.lang.Stringlt/method-paramgt
lt/method-paramsgt lt/query-methodgt ltejb-qlgt
select object(p) From CEntityBean2 as p where
p.prof ?1 lt/ejb-qlgt lt/querygt
10
Message Driven-Beans
11
Lifecycle of a Stateful Session Bean
12
Lifecycle of a Stateless Session Bean
13
Lifecycle of a Message-Driven Bean
14
Lifecycle of an Entity Bean
15
(No Transcript)
16
(No Transcript)
17
(No Transcript)
18
(No Transcript)
19
Abstract Schema of a Bean Application
  • Bean Descriptor contains information about
  • Persistent Fields
  • Relationship fields between Beans

20
Client Access Remote and Local
  • Client access is defined with interfaces
  • Remote Interface
  • Home Interface

21
(No Transcript)
22
ExampleEnterprise Bean Class
import java.rmi.RemoteException import
javax.ejb.SessionBean import javax.ejb.SessionCon
text import java.math. public class
ConverterBean implements SessionBean
BigDecimal yenRate new BigDecimal("121.6000")
BigDecimal euroRate new BigDecimal("0.0077")
public BigDecimal dollarToYen(BigDecimal
dollars) BigDecimal result
dollars.multiply(yenRate) return
result.setScale(2,BigDecimal.ROUND_UP)
public BigDecimal yenToEuro(BigDecimal yen)
BigDecimal result yen.multiply(euroRate)
return result.setScale(2,BigDecimal.ROUND_UP)
public ConverterBean() public void
ejbCreate() public void ejbRemove()
public void ejbActivate() public void
ejbPassivate() public void
setSessionContext(SessionContext sc)
23
ExampleEnterprise Bean Remote Interface
import javax.ejb.EJBObject import
java.rmi.RemoteException import
java.math. public interface Converter extends
EJBObject public BigDecimal
dollarToYen(BigDecimal dollars) throws
RemoteException public BigDecimal
yenToEuro(BigDecimal yen) throws
RemoteException
24
ExampleEnterprise Bean Home Interface
import java.rmi.RemoteException import
javax.ejb.CreateException import
javax.ejb.EJBHome public interface
ConverterHome extends EJBHome Converter
create() throws RemoteException,
CreateException
25
ExampleJSP Web Client (1)
lt_at_ page import"Converter,ConverterHome,javax.ejb
., javax.naming., javax.rmi.PortableRemoteObject
, java.rmi.RemoteException" gt lt! private
Converter converter null public void
jspInit() try InitialContext ic
new InitialContext() Object objRef
ic.lookup(" javacomp/env/ejb/TheConverter
") ConverterHome home
(ConverterHome)PortableRemoteObject.narrow(
objRef, ConverterHome.class) converter
home.create() catch (RemoteException ex)
... ... gt lthtmlgt ltheadgt
lttitlegtConverterlt/titlegt lt/headgt
26
ExampleJSP Web Client (2)
ltbody bgcolor"white"gt lth1gtltcentergtConverterlt/ce
ntergtlt/h1gt lthrgt ltpgtEnter an amount to
convertlt/pgt ltform method"get"gt ltinput
type"text" name"amount" size"25"gt ltbrgt ltpgt ltinp
ut type"submit" value"Submit"gt ltinput
type"reset" value"Reset"gt lt/formgt lt String
amount request.getParameter("amount") if (
amount ! null amount.length() gt 0 )
BigDecimal d new BigDecimal (amount) gt
ltpgtlt amount gt dollars are lt
converter.dollarToYen(d) gt Yen. ltpgtlt amount
gt Yen are lt converter.yenToEuro(d) gt
Euro. lt gt lt/bodygt lt/htmlgt
27
Running Example
http//lthostgt8080/converter
28
Session Bean CartBean (1)
import java.util. import javax.ejb. 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()
29
Session Bean CartBean (2)
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() public void
addBook(String title) contents.addElement(ti
tle)
30
Session Bean CartBean (3)
public void removeBook(String title) throws
BookException boolean result
contents.removeElement(title) if (result
false) throw new BookException(title
"not in cart.") public Vector
getContents() return contents
public CartBean() public void ejbRemove()
public void ejbActivate() public void
ejbPassivate() public void
setSessionContext(SessionContext sc)
31
Session Bean Home Interface CartHome
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
32
Session Bean Remote Interface Cart
  • The method definitions in a remote interface must
    follow these rules
  • Each method in the remote interface must match a
    method implemented in the enterprise bean class.
  • The signatures of the methods in the remote
    interface must be identical to the signatures of
    the corresponding methods in the enterprise bean
    class.
  • The arguments and return values must be valid RMI
    types.
  • The throws clause must include the
    java.rmi.RemoteException.

import java.util. import javax.ejb.EJBObject i
mport 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
33
Bean Managed Persistence Data Fields
The entity bean illustrated in this section
represents a simple bank account. The state of
SavingsAccountBean is stored in the
savingsaccount table of a relational database.
The savingsaccount table is created by the
following SQL statement
CREATE TABLE savingsaccount (id VARCHAR(3)
CONSTRAINT pk_savingsaccount PRIMARY KEY,
firstname VARCHAR(24), lastname VARCHAR(24),
balance NUMERIC(10,2))
34
Bean Managed Persistence - Entity Bean (1)
The ejbCreate method of SavingsAccountBean
inserts the entity state into the database by
invoking the private insertRow method, which
issues the SQL INSERT statement. Here is the
source code for the ejbCreate method
public String ejbCreate(String id, String
firstName, String lastName, BigDecimal
balance) throws CreateException if
(balance.signum() -1) throw new
CreateException ("A negative initial
balance is not allowed.") try
insertRow(id, firstName, lastName, balance)
catch (Exception ex) throw new
EJBException("ejbCreate "
ex.getMessage()) this.id id
this.firstName firstName this.lastName
lastName this.balance balance return
id
35
Bean Managed Persistence - Entity Bean (2)
A client deletes an entity bean by invoking the
remove method. This invocation causes the EJB
container to call the ejbRemove method, which
deletes the entity state from the database. In
the SavingsAccountBean class, the ejbRemove
method invokes a private method named deleteRow,
which issues an SQL DELETE statement.
public void ejbRemove() try
deleteRow(id) catch (Exception ex)
throw new EJBException("ejbRemove "
ex.getMessage())
36
Bean Managed Persistence - Entity Bean (3)
In the SavingsAccountBean class, ejbLoad invokes
the loadRow method, which issues an SQL SELECT
statement and assigns the retrieved data to the
instance variables. The ejbStore method calls the
storeRow method, which stores the instance
variables in the database using an SQL UPDATE
statement.
public void ejbLoad() try loadRow()
catch (Exception ex) throw new
EJBException("ejbLoad "
ex.getMessage()) public void ejbStore()
try storeRow() catch (Exception
ex) throw new EJBException("ejbStore "
ex.getMessage())
37
Savings Account Finder Methods
  • SavingsAccount jones home.findByPrimaryKey("836"
    )
  • ...
  • Collection c home.findByLastName("Smith")
  • ...
  • Collection c home.findInRange(20.00, 99.00)
  • For every finder method available to a client,
    the entity bean class must implement a
    corresponding method that begins with the prefix
    ejbFind. The SavingsAccountBean class, for
    example, implements the ejbFindByLastName method
    as follows

public Collection ejbFindByLastName(String
lastName) throws FinderException
Collection result try result
selectByLastName(lastName) catch (Exception
ex) throw new EJBException("ejbFindByLastNam
e " ex.getMessage()) return
result
38
Bean Managed Persistence - Entity Bean
The finder methods that are specific to your
application are optional, but the
ejbFindByPrimaryKey method is required. As its
name implies, the ejbFindByPrimaryKey method
accepts as an argument the primary key, which it
uses to locate an entity bean. In the
SavingsAccountBean class, the primary key is the
id variable.
public String ejbFindByPrimaryKey(String
primaryKey) throws FinderException
boolean result try result
selectByPrimaryKey(primaryKey) catch
(Exception ex) throw new EJBException("ejbFi
ndByPrimaryKey " ex.getMessage())
if (result) return primaryKey
else throw new ObjectNotFoundException
("Row for id " primaryKey " not found.")

39
Bean Managed PersistenceEntity Bean Business
Methods
public void debit(BigDecimal amount) throws
InsufficientBalanceException if
(balance.compareTo(amount) -1) throw
new InsufficientBalanceException()
balance balance.subtract(amount) public
void credit(BigDecimal amount) balance
balance.add(amount) public String
getFirstName() return firstName
public String getLastName() return
lastName public BigDecimal getBalance()
return balance
40
Bean Managed PersistenceEntity Bean Business
Methods
BigDecimal zeroAmount new BigDecimal("0.00") Sa
vingsAccount duke home.create("123", "Duke",
"Earl", zeroAmount) ... duke.credit(new
BigDecimal("88.50")) duke.debit(new
BigDecimal("20.25")) BigDecimal balance
duke.getBalance()
41
Bean Managed PersistenceEntity Bean Home
Interface
import java.util.Collection import
java.math.BigDecimal import java.rmi.RemoteExcept
ion import javax.ejb. public interface
SavingsAccountHome extends EJBHome public
SavingsAccount create(String id, String
firstName, String lastName, BigDecimal
balance) throws RemoteException,
CreateException public SavingsAccount
findByPrimaryKey(String id) throws
FinderException, RemoteException public
Collection findByLastName(String lastName)
throws FinderException, RemoteException
public Collection findInRange(BigDecimal low,
BigDecimal high) throws
FinderException, RemoteException public
void chargeForLowBalance(BigDecimal
minimumBalance, BigDecimal charge)
throws InsufficientBalanceException,
RemoteException
42
Bean Managed PersistenceEntity Bean Remote
Interface
import javax.ejb.EJBObject import
java.rmi.RemoteException import
java.math.BigDecimal public interface
SavingsAccount extends EJBObject
public void debit(BigDecimal amount)
throws InsufficientBalanceException,
RemoteException public void
credit(BigDecimal amount) throws
RemoteException public String
getFirstName() throws RemoteException
public String getLastName() throws
RemoteException public BigDecimal
getBalance() throws RemoteException
43
Container Managed Persistence Example
44
Container Managed PersistenceFinder Methods
Finder method findall() SELECT OBJECT(p) FROM
Player p Data retrieved All players.
Finder method public Collection
findByPosition(String position) throws
FinderException SELECT DISTINCT
OBJECT(p) FROM Player p WHERE p.position ?1
Data retrieved The players with the position
specified by the finder method's parameter.
public Collection findByPositionAndName(String
position, String name) throws
FinderException SELECT DISTINCT OBJECT(p) FROM
Player p WHERE p.position ?1 AND p.name ?2
Data retrieved The players having the
specified positions and names.
45
Tools for Automatic Data Persistence
46
Extended Examples available in the Web Tutorial
Write a Comment
User Comments (0)
About PowerShow.com