HelloWorld session bean example.
1) Remote interface:
package com.ejb.session;
import java.rmi.RemoteException;
import javax.ejb.EJBObject;
public interface HelloWorldSession extends EJBObject {
int businessMethod(int a, int b) throws RemoteException;
}
2) Home Interface:
package com.ejb.session;
import java.rmi.RemoteException;
import javax.ejb.CreateException;import javax.ejb.EJBHome;
public interface HelloWorldHome extends EJBHome {
HelloWorldSession create()throws RemoteException,CreateException;
}
3).Bean class :
package com.ejb.session;
import java.rmi.RemoteException;
import javax.ejb.EJBException;import javax.ejb.SessionBean;import javax.ejb.SessionContext;
public class HelloWorldSessionBean implements SessionBean { private SessionContext sessionContext;
public HelloWorldSessionBean() {
super();
}
public int businessMethod(int a, int b) throws RemoteException{
return a+b;
}
public void setSessionContext(SessionContext arg0) throws EJBException, RemoteException {
this.sessionContext = sessionContext;
}
public void ejbCreate() {}
public void ejbRemove(){}
public void ejbActivate() {}
public void ejbPassivate() {}
}
4). ejb-jar.xml
5). Client file:
package com.ejb.session;
import java.util.Properties;
import javax.naming.Context;import javax.naming.InitialContext;import javax.rmi.PortableRemoteObject;
public class SessionClient {
public SessionClient() { super(); // TODO Auto-generated constructor stub }
/** * @param args */
public static void main(String[] args) {
try{
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory"); p.put(Context.PROVIDER_URL, "jnp://localhost:1099");
p.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces"); InitialContext ctx = new InitialContext(p);
Object obj = ctx.lookup("HelloWorldBean");
HelloWorldHome ejbHome = ( HelloWorldHome ) PortableRemoteObject.narrow(obj, HelloWorldHome .class);
HelloWorldSession sessionObj = ejbHome.create();
int k = sessionObj.businessMethod( 10,20);
System.out.println("----------Result is-------------"+k);
}catch(Exception e){
e.printStackTrace();
}
}
}
put ejb-jar.xml in META-INF folder.
No comments:
Post a Comment