Deploying a EJB 2.1 Stateful Bean on Weblogic 10.3


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.


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



public void startCounter() throws RemoteException{



public int getCounter()throws RemoteException{

return counter;


public void ejbCreate(String name) throws RuntimeException{




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=; xmlns:xsi=; xsi:schemaLocation=;>





<description>Test Stateful on Weblogic</description>










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”?>


xmlns=; xmlns:j2ee=; xmlns:xsi=; xsi:schemaLocation=;>





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:
– 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“);





private static InitialContext getInitialContext(String url)

throws NamingException {

Hashtable<String, String> env = new Hashtable<String, String>();


env.put(Context.PROVIDER_URL, url);

env.put(Context.SECURITY_PRINCIPAL, “ejbuser“);


env.put(“weblogic.jndi.createIntermediateContexts”, “true”);

return new InitialContext(env);


Hope This helps.

R Vashi

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

  1. Taha


    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?


Leave a Reply

Please log in using one of these methods to post your comment: Logo

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

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.