Deploying a EJB 2.1 Stateful Bean on Weblogic 10.3

Hi,

Deploying an EJB2.1 Stateful bean is quite very easy in weblogic. First of all develop the Stateful bean and extract a jar out of it. I will show how to build and deploy the EJB in web logic 10.3 step by step.

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 DD

5.       Deploy on the server


1.       Create a Home Interface.


package
com.test.stf;

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;

}


2.       Create a Component Interface(Remote)

package com.test.stf;

import java.rmi.RemoteException;

import javax.ejb.EJBObject;

public interface Counter extends EJBObject {

public void startCounter() throws RemoteException;

public int getCounter() throws RemoteException;

}

3. Create Stateful session bean


package com.test.stf;

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;

}

}

Below is the list of some high Level rules which we have ensure while developing the EJB objects.

-> Rules for EJB Home Interface:

1.       Should must extend EJBHome Interface

2.       Must have atlest one createMethod

3.       The name of create method must begin with create e.g createClientPortfolio etc.

4.       The Create method should return the Component interface type(Remote interface)

5.       Create method should throw “CreateException, RemoteException”.

-> Rules for Remote Interface

1.       Should Must extends EJBObject Interface

2.       Should contains the business methods

3.       Every business method should throw 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.

4.  Now let’s prepare the DD of EJB Component

<?xml version=“1.0” encoding=“UTF-8”?>

<ejb-jar id=“ejb-jar_ID” version=“2.1” xmlns=http://java.sun.com/xml/ns/j2ee&#8221; xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance&#8221; xsi:schemaLocation=http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd&#8221;>

<display-name>

TestStateful</display-name>

<enterprise-beans>

<session>

<description>Test Stateful on Weblogic</description>

<ejb-name>TestStateful</ejb-name>

<home>com.test.stf.CounterHome</home>

<remote>com.test.stf.Counter</remote>

<ejb-class>com.test.stf.HandleCounter</ejb-class>

<session-type>Stateful</session-type>

<transaction-type>Container</transaction-type>

</session>

</enterprise-beans>

</ejb-jar>

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)

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).

<?xml version=“1.0” encoding=“UTF-8”?>

<weblogic-ejb-jar

xmlns=http://www.bea.com/ns/weblogic/90&#8221; xmlns:j2ee=http://java.sun.com/xml/ns/j2ee&#8221; xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance&#8221; xsi:schemaLocation=http://www.bea.com/ns/weblogic/90 http://www.bea.com/ns/weblogic/90/weblogic-ejb-jar.xsd&#8221;>

<weblogic-enterprise-bean>

<ejb-name>TestStateful</ejb-name>

<jndi-name>TestStateful</jndi-name>

</weblogic-enterprise-bean>

</weblogic-ejb-jar>
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.

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
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.

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<String, String> env = new Hashtable<String, String>();

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);

}

Hope This helps.

Thanks
R Vashi

Advertisements

3 thoughts on “Deploying a EJB 2.1 Stateful Bean on Weblogic 10.3

  1. Taha

    Hello,

    I try to deploy ejb2 on weblogic … the weblogic can’t feel the new changes in ejb … i mean i added new method which can’t be found by the weblogic on runtime it gives nosuchmethod exception … do i have to clear the cache somewhere or what?

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s