Upload
s-r-krishnan
View
465
Download
3
Embed Size (px)
Citation preview
1
Hello World applicationwith EJB
2
Hello World application
z Simple server that prints the string “Hello, world !”
z Implemented using a stateless session bean
2
3
Steps to be followed
1. Component development * Describe Remote interface * Describe Home interface * Implement the Bean class
2. Write deployment descriptor(s)
3. Package in an archive (jar file) all EJB files
4. Deployment into the container
5. Implement the client application
4
Remote interface : HelloWorld.java
z Corresponds to the interface descriptionz Gives the list of the methods the client can call
import java.rmi.*;
import javax.ejb.*;
public interface HelloWorld extends EJBObject {
public String sayHello() throws RemoteException;
}
3
5
Home interface : HelloWorldHome.java
z Provides methods for the client to create, find or remove an EJB.z To use an EJB, a client must first get a reference to its Home
interface using JNDI (Java Naming and Directory Interface).z The create() method returns a reference to the EJB remote
interface (HelloWorld).
import java.rmi.RemoteException;
import java.ejb.CreateException;
import javax.ejb.EJBHome ;
public interface HelloWorldHome extends EJBHome {
public HelloWorld create() throwsCreateException, RemoteException;
}
6
Bean implementation class :HelloWorldBean.java
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
public class HelloWorldBean implements SessionBean {
// Methods of Remote interface
public String sayHello() {
return "Hello, world !";
}
// Methods of Home interface
public void ejbCreate() {}
4
7
Bean implementation class :HelloBean.java (cont.)
// Methods of SessionBean interface
protected SessionContext ctx;
public void setSessionContext(SessionContext ctx) {
this.ctx = ctx;
}
public void ejbRemove() {}
public void ejbActivate() {}
public void ejbPassivate() {}
}
8
Deployment Descriptor : ejb-jar.xml<ejb-jar>
<description>HelloWorld deployment descriptor</description>
<display-name>HelloWorld</display-name>
<enterprise-beans>
<session>
<description> HelloWorld deployment descriptor </description>
<display-name>HelloWorld</display-name>
<ejb-name>HelloWorld</ejb-name>
<home>HelloWorldHome</home>
<remote>HelloWorld</remote>
<ejb-class>HelloWorldBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
5
9
Deployment Descriptor : ejb-jar.xml (cont.)
</enterprise-beans>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>HelloWorld</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>
</ejb-jar>
10
Additional Deployment Descriptor(specific to Jonas) : jonas-ejb-jar.xml
<jonas-ejb-jar>
<jonas-session>
<ejb-name>HelloWorld</ejb-name>
<jndi-name>myHelloWorld</jndi-name>
</jonas-session>
</jonas-ejb-jar>
6
11
Packaging and deploying the EJB
z An EJB must be packaged in a “.jar” file containing
ª Class files
ª Deployment descriptors
z In order to be used, an EJB must first be deployed
ª Copy the jar file in a specific directory for the container to get it from
12
Client : HelloClient.java
import java.util.Properties;
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.transaction.UserTransaction;
import javax.rmi.PortableRemoteObject;
public class HelloClient {
public static void main(String args[]) {
...
7
13
Client : HelloClient.java (cont.)try {
Context initialContext = new InitialContext();
Object objref = initialContext.lookup("myHelloWorld");
HelloWorldHome home =(HelloWorldHome)PortableRemoteObject.narrow(objref,HelloWorldHome.class);
HelloWorld myHelloWorld = home.create();
String message = myHelloWorld.sayHello();
System.out.println(message);
} catch (Exception e) {
System.err.println(" Erreur : " + e);
System.exit(2);
}
}
}