EJB stateless
Calculate.java
import javax.ejb.*;
import java.rmi.*;
public interface Calculate extends EJBObject{
public int multiply(int x, int y) throws RemoteException;
public float divide(int x, int y) throws RemoteException;
}
CalculateBean.java
import javax.ejb.*;
import java.rmi.*;
public class CalculateBean implements SessionBean {
SessionContext ctx;
public CalculateBean(){
System.out.println("Bean Instantiated");
}
public void setSessionContext(SessionContext context) {
this.ctx = context;
System.out.println(" This Context Associated with the ContainerContext ");
}
public void ejbCreate(){
System.out.println("ejbCreate");
}
public void ejbActivate(){}
public void ejbPassivate(){}
public void ejbRemove(){
System.out.println("ejbRemove");
}
public int multiply(int x, int y) {
System.out.println("multiply");
return x*y;
}
public float divide(int x, int y) {
System.out.println("divide");
float z= x/y;
return z;
}
}
CalculateClient.java
import javax.ejb.*;
import javax.naming.*;
import javax.rmi.*;
public class CalculateClient{
public static void main(String args[]){
try{
Context ctx = new InitialContext();
Object obj = ctx.lookup("app2JNDI");
CalculateHome home = (CalculateHome)PortableRemoteObject.narrow(obj,CalculateHome.class);
Calculate cal = home.create();
System.out.println("Create called");
int x = Integer.parseInt(args[0]);
int y = Integer.parseInt(args[1]);
int s=cal.multiply(x,y);
System.out.println("Product = " + s);
float t=cal.divide(x,y);
System.out.println("Quotient = " + t);
}catch(Exception e){
System.out.println(e);
}
}
}
CalculateHome.java
import javax.ejb.*;
import java.rmi.*;
public interface CalculateHome extends EJBHome{
Calculate create() throws CreateException, RemoteException;
}