Introduction To Session Beans
Introduction To Session Beans
Introduction To Session Beans
EJB
Architecture
Stateless session Beans
A stateless session bean does not maintain a
• conversational state for a particular client.
Must throw
RemoteException
Home Interface
/**
* This is the home interface for HelloBean. This interface
* is implemented by the EJB Server’s 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 HelloBean.
*/
public interface HelloHome extends javax.ejb.EJBHome
{
/*
* This method creates the EJB Object.
*
* @return The newly created EJB Object.
*/
Hello create() throws java.rmi.RemoteException,
javax.ejb.CreateException;
}
Bean Implementation
/**
* Demonstration stateless session bean.
*/
public class HelloBean implements javax.ejb.SessionBean {
private javax.ejb.SessionContext ctx;
//
// EJB-required methods
//
public void ejbCreate() { 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(javax.ejb.SessionContext ctx) {
this.ctx = ctx; }
//
// Business methods
//
public String hello() {
System.out.println(“hello()”);
return “Hello, World!”;
}
}
Client Implementation
import javax.naming.Context;
import javax.naming.InitialContext;
import java.util.Properties;
/**
* This class is an example of client code that invokes
* methods on a simple stateless session bean.
*/
public class HelloClient {
public static void main(String[] args) throws Exception {
/*
* Setup properties for JNDI initialization.
* These properties will be read in from the command line.
*/
Properties props = System.getProperties();
/*
* Obtain the JNDI initial context.
* The initial context is a starting point for
* connecting to a JNDI tree. We choose our JNDI
* driver, the network location of the server, etc.
* by passing in the environment properties.
*/
Context ctx = new InitialContext(props);
Client Implementation
/* Get a reference to the home object - the
* factory for Hello EJB Objects
*/
Object obj = ctx.lookup(“HelloHome”);
/* Home objects are RMI-IIOP objects, and so they must be cast
* into RMI-IIOP objects using a special RMI-IIOP cast.
*/
HelloHome home = (HelloHome)
javax.rmi.PortableRemoteObject.narrow(obj, HelloHome.class);
/* Use the factory to create the Hello EJB Object
*/
Hello hello = home.create();
/*Call the hello() method on the EJB object. The
* EJB object will delegate the call to the bean,
* receive the result, and return it to us.
* We then print the result to the screen.
*/
System.out.println(hello.hello());
/*
* Done with EJB Object, so remove it.
* The container will destroy the EJB object.
*/
hello.remove();
}
}
The logical architecture
Client Directory App server (container)
Machine Machine Machine
•
Client NamingService HomeInterface Pool Istanza
Find the
Home interface
Find
Give me an instance
Create or fetch
An instance
Method()
Deployment Descriptor
Client
packages
.class
jndi.properties Jboss
ejb.jar
packages
.java
ejb-jar.xml
jboss.xml
Introduction to Session beans
LOCAL BEANS
Local Interface
/**
* This is the HelloBean local interface.
*
* This interface is what local clients operate
* on when they interact with EJB local objects.
* The container vendor will implement this
* interface; the implemented object is the
* EJB local object, which delegates invocations
* to the actual bean.
*/
public interface HelloLocal extends javax.ejb.EJBLocalObject
{
/**
* The one method - hello - returns a greeting to the client.
*/
public String hello();
} May throw
EJBException
instead of
RemoteException
Local Home Interface
/**
* This is the home interface for HelloBean. This interface
* is implemented by the EJB Server’s 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 HelloBean.
*/
public interface HelloLocalHome extends javax.ejb.EJBLocalHome
{
/*
* This method creates the EJB Object.
*
* @return The newly created EJB Object.
*/
HelloLocal create() throws javax.ejb.CreateException;
}
Local Client
EJB 3.0
Remote Interface
EJB 2.1 =====================================================
package examples.session.stateless;
public interface Hello {
public String hello(); business
} interface
Bean Implementation
EJB 2.1 ===============================================================
public class HelloBean implements javax.ejb.SessionBean {
private javax.ejb.SessionContext ctx;
public void ejbCreate() { 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(javax.ejb.SessionContext ctx) {
this.ctx = ctx; }
public String hello() {
System.out.println(“hello()”); return “Hello, World!”;
}
}
EJB 3.0 ==============================================================
package examples.session.stateless;
import javax.ejb.Remote; import javax.ejb.Stateless;
@Stateless enterprise
@Remote(Hello.class) bean
public class HelloBean implements Hello { instance
public String hello() {
System.out.println(“hello()”); return “Hello, World!”;
}
}
The remote client – 3.0
package examples.session.stateless;
import javax.naming.Context;
import javax.naming.InitialContext;
public class HelloClient {
public static void main(String[] args) throws Exception {
/*
* Obtain the JNDI initial context.
*
* The initial context is a starting point for
* connecting to a JNDI tree.
*/
Context ctx = new InitialContext();
Hello hello = (Hello)
ctx.lookup(“examples.session.stateless.Hello”);
/*
* Call the hello() method on the bean.
* We then print the result to the screen.
*/
System.out.println(hello.hello());
}
}
ejb-jar.xml – 3.0
<?xml version=”1.0” encoding=”UTF-8” ?>
<ejb-jar xmlns=”http://java.sun.com/xml/ns/j2ee”
xmlns:xsi=”http://www.w3.org/2001/XMLSchemainstance”
xsi:schemaLocation=”http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/ejb-jar_3_0.xsd”
version=”3.0”>
<enterprise-beans>
</enterprise-beans>
</ejb-jar>
Keep in mind these terms…
• The enterprise bean instance is a plain old Java object instance of an
enterprise bean class. It contains business method implementations of the
methods defined in the remote/local business interface, for session beans.
• The business interface is a plain old Java interface that enumerates the
business methods exposed by the enterprise bean. Depending on the
client view supported by the bean, the business interface can be further
classified into a local business interface or a remote business interface.
Click on Service=JNDI
Choose List