There are five steps involved in EJB Session bean Development and deploy process.
1. Create the Home Interface
2. Create the Remote Interface
3. Create the Session Bean
4. Define the resources in Deployment Descriptor
5. Deploy on the server
Rules for EJB Home Interface:
1. EJB Home must extend EJBHome Interface
2. Must have at least one create() Method
3. The create method should return the Component interface type (Remote interface)
4. create() method should throw “CreateException, RemoteException”.
1. Create a Home Interface
package com.test;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;
public interface CounterHome extends EJBHome {
public Counter create(String name)throws CreateException, RemoteException;
}
Rules for Remote Interface
1. Remote must extend EJBObject Interface
2. Should contains the business methods
3. Every business method should throw RemoteException
2. Create a Remote Interface
package com.test;
import java.rmi.RemoteException;
import javax.ejb.EJBObject;
public interface Counter extends EJBObject {
public void startCounter() throws RemoteException;
public int getCounter() throws RemoteException;
}
Rules for SessionBeans
1. Every SessionBean should extend SessionBean Interface
2. In case of Stateless session bean there must be only one create method and that should must prefix with “ejbCreate”. And should be with no-arg approach.
3. Every bean should save the SessionContext copy in the “setSessionContext” method, as this bean called only once when bean gets created; saving this reference is always useful.
4. Incase of Stateful session bean, do not need to have no-arg create method. And also you can create as many as createMethod.
e.g ejbCreate(), ejbCreate(String), createCustomerPortfolio(String) etc.
e.g ejbCreate(), ejbCreate(String), createCustomerPortfolio(String) etc.
3. Create Stateful session bean
package com.test;
import java.io.Serializable;
import java.rmi.RemoteException;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
public class HandleCounter implements SessionBean, Serializable {
private static final long serialVersionUID = 1L;
private String userName=”";
private SessionContext ctx;
private int counter=0;
public void ejbActivate() throws EJBException, RemoteException {
System.out.println(“Inside ejbActivate”);
}
public void ejbPassivate() throws EJBException, RemoteException {
System.out.println(“Inside ejbPassivate”);
}
public void ejbRemove() throws EJBException, RemoteException {
System.out.println(“Inside ejbRemove”);
}
public void setSessionContext(SessionContext arg0) throws EJBException,
RemoteException {
this.ctx=arg0;
}
public void startCounter() throws RemoteException{
counter=counter+1;
}
public int getCounter()throws RemoteException{
return counter;
}
public void ejbCreate(String name) throws RuntimeException{
userName=name;
}
}
4. Deployment Descriptors of EJB Components (ejb-jar.xml)
ejb-name : Give any name for the EJB
home: EJBHome class FQDN (Fully qualified defined name)
remote: Remote class FQDN (Fully qualified defined name)
ejb-class : Session bean class FQDN (Fully qualified defined name)
session-type: Stateful or Stateless (in our case Stateful)
transaction-type: Container | Bean (let container handle this)
home: EJBHome class FQDN (Fully qualified defined name)
remote: Remote class FQDN (Fully qualified defined name)
ejb-class : Session bean class FQDN (Fully qualified defined name)
session-type: Stateful or Stateless (in our case Stateful)
transaction-type: Container | Bean (let container handle this)
As we are deploying on weblogic , we need weblogic-ejb-jar.xml so that weblogic can treat this as an EJB Component and set the configuration at deploy time(JNDI name, security related etc).
ejb-name: make sure to use the same ejb-name defined in ejb-jar.xml
jndi-name: this is the name of the JNDI given the EJB component.
jndi-name: this is the name of the JNDI given the EJB component.
5. Deployment
To deploy simply extract the jar of the EJB classes and deploy in the admin console of weblogic.
Structure of the EJB Jar:
———————————————————————————–|
com.test
META-INF
– ejb-jar.xml
– weblogic-ejb-jar.xml
– manifest
————————————————————————————|
———————————————————————————–|
com.test
META-INF
– ejb-jar.xml
– weblogic-ejb-jar.xml
– manifest
————————————————————————————|
In case of stateless, keep everything same and change the bean create method to no-arg.
And change the stateless
Client to test
public final static String JNDI_FACTORY = “weblogic.jndi.WLInitialContextFactory”;
public static void main(String[] args) throws Exception {
InitialContext ic = getInitialContext(“t3://localhost:7001″);
CounterHome home = (CounterHome) PortableRemoteObject.narrow(ic
.lookup(“TestStateful“), CounterHome.class);
Counter adv = home.create(“ejbMarathon“);
adv.startCounter();
adv.startCounter();
System.out.println(adv.getCounter());
}
private static InitialContext getInitialContext(String url)
throws NamingException {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, url);
env.put(Context.SECURITY_PRINCIPAL, “ejbuser“);
env.put(Context.SECURITY_CREDENTIALS, “PASSWORD123“);
env.put(“weblogic.jndi.createIntermediateContexts”, “true”);
return new InitialContext(env);
}
----------------------------------------- END -----------------------------------------
No comments:
Post a Comment