Title: EJB types
1EJB types
- entity
- session
- message-driven.
2EJB classes and interfaces
remote interface remote home interface local
interface local home interface endpoint
interface message interface bean class primary
key.
3EJB classes and interfaces
defines the beans business methods which can be
accessed from applications outside the EJB
container.
remote interface remote home interface local
interface local home interface endpoint
interface message interface bean class primary
key.
4EJB classes and interfaces
remote interface remote home interface local
interface local home interface endpoint
interface message interface bean class primary
key.
defines the beans life-cycle methods which can
be accessed from applications outside the EJB
container.
5EJB classes and interfaces
remote interface remote home interface local
interface local home interface endpoint
interface message interface bean class primary
key.
defines the business methods than can be used by
other beans in the same EJB container.
6EJB classes and interfaces
remote interface remote home interface local
interface local home interface endpoint
interface message interface bean class primary
key.
defines life-cycle methods that can be used by
other beans in the same EJB container.
7EJB classes and interfaces
remote interface remote home interface local
interface local home interface endpoint
interface message interface bean class primary
key.
defines business methods that can be accessed
from applications outside the EJB container via
SOAP (a distributed object protocol)
8EJB classes and interfaces
remote interface remote home interface local
interface local home interface endpoint
interface message interface bean class primary
key.
defines the methods by which messaging systems
can deliver messages to the bean.
9EJB classes and interfaces
remote interface remote home interface local
interface local home interface endpoint
interface message interface bean class primary
key.
implements the beans business and life-cycle
methods.
10EJB classes and interfaces
remote interface remote home interface local
interface local home interface endpoint
interface message interface bean class primary
key.
a class that provides a pointer into the
database. Only entity beans need a primary key.
11Why local and remote interfaces?
efficiency
- calls to methods in the local interface do not
involve RMI - 2. a bean is not required to provide a local
interface if you know (when you are developing)
that the bean will interact only with remote or
Web service clients
12The remote interface (for a Cabin EJB)
package com.titan.cabin import
java.rmi.RemoteException public interface
CabinRemote 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 getShipId() throws RemoteException
public void setShipId(int sp) throws
RemoteException public int getBedCount()
throws RemoteException public void
setBedCount(int bc) throws RemoteException
13The remote home interface
package com.titan.cabin import
java.rmi.RemoteException import
javax.ejb.CreateException import
javax.ejb.FinderException public interface
CabinHomeRemote extends javax.ejb.EJBHome
public CabinRemote create(Integer id)
throws CreateException, RemoteException
public CabinRemote findByPrimaryKey(Integer pk)
throws FinderException, RemoteException
14The bean class 1/3
import javax.ejb.EntityContext public abstract
class CabinBean implements javax.ejb.EntityBean
public Integer ejbCreate(Integer id)
setId(id) return null public
void ejbPostCreate(Integer id)
15The bean class 2/3
public abstract String getName() public
abstract void setName(String str) public
abstract int getDeckLevel() public abstract void
setDeckLevel() public abstract Integer
getId() public abstract void setID ()
16The bean class 3/3
public void setEntityContext(EntityContext ctx)
public void unsetEntityContext() public
void ejbActivate() public void ejbPassivate()
public void ejbLoad() public void
ejbStore() public void ejbRemove()
17Deployment descriptor (for EJB 2.1)
ltejb-jar xmlns"http//java.sun.com/xml/ns/j
2ee" xmlnsxsi"http//www.w3.org/2001/XMLSch
ema-instance" xsischemaLocation"http//java
.sun.com/xml/ns/j2ee
http//java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd"
version"2.1"gt ltenterprise-beansgt
ltentitygt ltejb-namegtCabinEJBlt/ejb-namegt
lthomegtcom.titan. CabinHomeRemotelt/homegt
ltremotegtcom.titan.CabinRemotelt/remotegt
ltejb-classgtcom.titan.CabinBeanlt/ejb-classgt
ltpersistence-typegtContainerlt/persistence-typegt
ltprim-key-classgtjava.lang.Integerlt/prim-key-cla
ssgt ltreentrantgtFalselt/reentrantgt
lt/entitygt lt/enterprise-beansgt lt/ejb-jargt
18Deployment descriptor (for EJB 2.1)
ltejb-jar the root of the XML deployment
descriptor xmlns"http//java.sun.com/xml/ns/
j2ee" xmlnsxsi"http//www.w3.org/2001/XMLSc
hema-instance" xsischemaLocation"http//jav
a.sun.com/xml/ns/j2ee
http//java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd"
version"2.1"gt ltenterprise-beansgt
declaration of all enterprise beans in this XML
file ltentitygt it can also be ltsessiongt or
ltmessage-drivengt ltejb-namegtCabinEJBlt/ejb-nam
egt the descriptive name for the whole bean
lthomegtcom.titan.CabinHomeRemotelt/homegt class name
for remote home interf. ltremotegtcom.titan.Ca
binRemotelt/remotegt class name for remote interf.
ltejb-classgtcom.titan.CabinBeanlt/ejb-classgt
class name of the bean class
ltpersistence-typegtContainerlt/persistence-typegt
later ltprim-key-classgtjava.lang.Integerlt/pri
m-key-classgt class name pr. key
ltreentrantgtFalselt/reentrantgt later lt/entitygt
lt/enterprise-beansgt lt/ejb-jargt
19All goes into a JAR
(Java ARchive)
20EJB object and EJB home (for entity and session
beans)
EJB server
Client
EJB container
21Entity beans a simple example
How to get information from an entity bean. We
want to display information about a particular
cruise. Let us assume that Ship, Cruise,
Ticket, Customer, Employee, are entity beans.
22Using entity beans 1/2
// by JNDI (Java Naming and Directory
Interface) CruiseHomeRemote cruiseHome //
get the cruise ID text field 1 String cruiseID
textField1.getText() // create an EJB primary
key from the cruise ID Integer pk new
Integer(cruiseID) // use the primary key to find
the cruise CruiseRemote cruise
cruiseHome.findByPrimaryKey(pk) // set text
field 2 to show the cruise name textField2.setText
(cruise.getName()) // get a remote interface to
the ship that will be used // for the cruise from
the cruise bean ShipRemote ship
cruise.getShip() // set text field 3 to show the
ships name text field3.setText(ship.getName())
23Using entity beans 2/2
// get all the cabins on the ship Collect
cabins ship.getCabins() Iterator cabinItr
cabins.iterator() // iterate through the
enumeration, adding // the name of each cabin to
a list box while (cabinItr.hasNext())
CabinRemote cabin (CabinRemote)cabinItr.next()
listBox1.addItem(cabin.getName())
24Session beans a simple example
How to book passengers on a cruise. TravelAgent
is the name of the session bean.
25Session beans their use
// get the credit card number from the text
field String creditCard textField1.getText() in
t cabinID Integer.parseInt(textField2.getTTText(
)) int cruiseID Integer.parseInt(textField3.get
Text()) // create a new Reservation session
passing // the reference to a customer entity
bean TravelAgent travelAgent travelAgentHome.cre
ate(customer) // set cabin and cruis
Ids travelAgent.setCabinID(cabinID) travelAgent.s
etCruiseID(creuiseID) // using the card number
and price, book passanger // this method returns
a Ticket object TicketDO ticket
travelAgent.bookPassage(creditCard, price)
26Session beans their code 1/2
public class TravelAgentBean implements
javax.ejb.SessionBean public CustomerRemote
customer public CruiseRemote cruise
public CabinRemote cabin public void
ejbCreate(CustomerRemote cust) customer
cust public TicketDO bookPassage(CreditCa
rdDO card, double price) throws
IncompleteConveresationalState if
(customernull cruisenull cabinnull)
throw new IncompleteConversationalState
()
27Session beans their code 2/2
try ReservationHomeRemote resHome
(ReservationHomeRemote) getHome(Reservation
Home, ReservationHomeRemote.class)
ReservationRemote reservation
resHome.create(customer, cruise, cabin, price,
new Date()) ProcessPaymentHomeRemote ppHome
(ProccessPaymentHomeRemote)
getHome(ProcessPaymentHome, ProcessPaymentHomeRe
mote.class) ProcessPaymentRemote process
ppHome.create() process.byCredit(customer,
card, price) TicketDO ticket new
TicketDo(customer, cruise, cabin, price)
catch(Exception e) throw new
EJBException(e)
28Stateless and stateful session beans
stateful session beans
maintain conversational state when used by a
client.
stateless session beans
do not maintain conversational state when used by
a client.