A session bean implies a class and an interface.
1. Define the interface in a file named Salut.java
package firstBean;
import javax.ejb.Remote;
@Remote
public interface Salut{
public String saluta(String nume);
}
2.Define the class in a file named SalutEJB.java
package firstBean;
import javax.ejb.*;
@Stateless
public class SalutEJB implements Salut{
public String saluta(String nume){
return "Salut,"+nume+"!";}
}
3. Compile the class and the interface
>set CLASSPATH=.;C:\Sun\SDK\lib\javaee.jar
>javac -d . firstBean/*.java
4.Write a client for the session bean, in a file named FirstBeanClient.java
package firstBeanClient;
import firstBean.Salut;
import javax.naming.InitialContext;
public class FirstBeanClient{
public static void main(String[] args) throws Exception
{
InitialContext context = new InitialContext();
Salut beanInstance = (Salut) context.lookup("firstBean.Salut");
System.out.println(beanInstance.saluta(args[0]));
}
}
5.Compile the client
>javac -d . firstBeanClient/*.java
6.Build a jar archive containing the session bean (class and interface)
>jar cvf FirstBeanJar.jar firstBean\
7.Load the archive (FirstBeanJar.jar) to the application server (I use GlassFish and its graphical administration console)
8. Execute the client including appserv-rt.jar and javaee.jar in the classpath.
>java -classpath C:\Sun\SDK\lib\appserv-rt.jar; ->
->C:\Sun\SDK\lib\javaee.jar; ->
C:\EJB firstBeanClient.FirstBeanClient Cristina
No comments:
Post a Comment