Professional Documents
Culture Documents
n Exemples
n Saisie d'une commande,
n Compression vidéo,
n Gestion d'un caddy, d'un catalogue de produits,
n Transactions bancaires…
1
Cours EJB/J2EE 19/10/2004
Copyright Michel Buffa
2
Cours EJB/J2EE 19/10/2004
Copyright Michel Buffa
3
Cours EJB/J2EE 19/10/2004
Copyright Michel Buffa
4
Cours EJB/J2EE 19/10/2004
Copyright Michel Buffa
5
Cours EJB/J2EE 19/10/2004
Copyright Michel Buffa
6
Cours EJB/J2EE 19/10/2004
Copyright Michel Buffa
7
Cours EJB/J2EE 19/10/2004
Copyright Michel Buffa
8
Cours EJB/J2EE 19/10/2004
Copyright Michel Buffa
Activation/Passivation callbacks
package examples ;
import javax.ejb.*;
import java.rmi.RemoteException;
/**
* These are CountBean ’s business logic methods.
*
* This interface is what clients operate on when they
* interact with EJB objects.The container vendor will
* implement this interface;the implemented object is
* the EJB object ,which delegates invocations to the
* actual bean.
*/
public interface Count extends EJBObject {
/**
* Increments the int stored as conversational state
*/
public int count()throws RemoteException;
}
9
Cours EJB/J2EE 19/10/2004
Copyright Michel Buffa
package examples ;
import javax.ejb.*;
/**
* Demonstration Stateful Session Bean.This Bean is initialized
* to some integer value,and has a business method which
//Business methods
//
/** Counts up */
public int count(){
System.out.println("count()");
return ++val;
}
//
// EJB-required methods
//
public void ejbCreate(int val)throws CreateException {
this.val =val;
System.out.println("ejbCreate()");
}
public void ejbRemove(){
System.out.println("ejbRemove()");
}
public void ejbActivate(){
System.out.println("ejbActivate()");
}
public void ejbPassivate(){
System.out.println("ejbPassivate()");
}
public void setSessionContext(SessionContext ctx){
}
}
10
Cours EJB/J2EE 19/10/2004
Copyright Michel Buffa
package examples ;
import javax.ejb.*;
import java.rmi.RemoteException;
/**
* This is the home interface for CountBean. This interface
* is implemented by the EJB Server ’s glue-code tools - the
* implemented object is called the Home Object , and serves
* as a factory for EJB Objects.
*
* One create() method is in this Home Interface, which
* corresponds to the ejbCreate() method in the CountBean file.
*/
public interface CountHome extends EJBHome {
/*
* This method creates the EJB Object.
* @param val Value to initialize counter to
* @return The newly created EJB Object .
*/
Count create(int val) throws RemoteException , CreateException;
}
11
Cours EJB/J2EE 19/10/2004
Copyright Michel Buffa
"http://java.sun.com/dtd/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>
12
Cours EJB/J2EE 19/10/2004
Copyright Michel Buffa
Client de l'EJB
package examples ;
import javax.ejb.*;
import javax.naming.*;
import java.util.Properties;
/** This class is a simple example of client code.
* We create 3 EJB Objects in this example, but we only allow
* the container to have 2 in memory. This illustrates how
* beans are passivated to storage. */
public class CountClient {
public static void main(String[] args) {
try {
/* Get System properties for JNDI initialization */
Properties props = System.getProperties();
/* Get a reference to the Home Object - the
* factory for EJB Objects */
Context ctx = new InitialContext(props);
CountHome home = (CountHome)
javax.rmi.PortableRemoteObject.narrow(
ctx.lookup ("CountHome"), CountHome.class );
Client de l'EJB
int countVal = 0;
/* Create and count() on each member of array */
System.out.println("Instantiating beans . . . ");
System.out.println(countVal);
/* Sleep for 1/2 second */
Thread.sleep(500);
} // try
...
13
Cours EJB/J2EE 19/10/2004
Copyright Michel Buffa
Client de l'EJB
ejbActivate () ejbActivate ()
count() ejbRemove ()
ejbPassivate() ejbRemove ()
ejbActivate ()
count()
ejbPassivate()
ejbActivate ()
count()
14
Cours EJB/J2EE 19/10/2004
Copyright Michel Buffa
15