Download Hello World application with EJB

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Hello World application
with EJB
Hello World application
z
Simple server that prints the string “Hello, world !”
z
Implemented using a stateless session bean
2
1
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
3
Remote interface : HelloWorld.java
z
z
Corresponds to the interface description
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;
}
4
2
Home interface : HelloWorldHome.java
z
z
z
Provides methods for the client to create, find or remove an EJB.
To use an EJB, a client must first get a reference to its Home
interface using JNDI (Java Naming and Directory Interface).
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() throws
CreateException, RemoteException;
}
5
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() {}
6
3
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() {}
}
7
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>
8
4
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>
9
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>
10
5
Packaging and deploying the EJB
z
z
An EJB must be packaged in a “.jar” file containing
ª
Class files
ª
Deployment descriptors
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
11
Client : HelloClient.java
import
import
import
import
import
java.util.Properties;
javax.naming.InitialContext;
javax.naming.Context;
javax.transaction.UserTransaction;
javax.rmi.PortableRemoteObject;
public class HelloClient {
public static void main(String args[]) {
...
12
6
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);
}
}
}
13
7
Related documents