Home‎ > ‎JBoss‎ > ‎JBoss Application Server‎ > ‎

EJB3 Session bean tutorial using JBoss and Eclipse

This "Hello World" tutorial shows how to develop deploy and test EJB3 Session bean in JBoss server and write a simple client and test it.

Application Server - JBosss 4.2.2 GA
IDE - Eclipse 3.4.1

File => New => Project => EJB Project


Give project a name and select JBoss as Target Run time. (Assume that you have already configured JBoss in Eclipse) and select finish.

Crete following Interfaces and classes.


IMyBean,java

import java.io.Serializable;
public interface IMyBean extends Serializable {   
    public void doSomething();
}

MyBeanRemote.java

import javax.ejb.Remote;
@Remote
public interface MyBeanRemote extends IMyBean{
}

MyBeanLocal.java

import javax.ejb.Local;
@Local
public interface MyBeanLocal extends IMyBean {
}

MyBean.java

import javax.ejb.Stateless;
@Stateless 
public class MyBean implements MyBeanLocal, MyBeanRemote {
    private static final long serialVersionUID = 9184424076718418234L;
    public void doSomething() {
        System.out.println("Hello World!");        
    }
}


Now select Menu option: Run As => Run on Server option to deploy the EJB project in JBoss server.

This will result in ProjectName.jar file in deploy directory in JBoss. i.e  jboss-4.2.2.GA\server\default\deploy

Now lets create a Java EE client project.

Select New => Java EE => Application Client Project

Give new project a name and select JBoss as target run time and select Finish.

Now add jboss-4.2.2.GA\server\default\deploy\ProjectName.jar into the class path of your client project.

Now create new Java class as follows;

MyBeanClient.java

import java.util.Properties;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class MyBeanClient {
    public static void main(String[] args) {
        try {
            Properties props = new Properties();
            props.setProperty("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
            props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming");
            props.setProperty("java.naming.provider.url", "127.0.0.1:1099");        
           
            InitialContext ctx = new InitialContext(props);           
            MyBeanRemote bean = (MyBeanRemote) ctx.lookup("MyBean/remote");
           
            bean.doSomething();
        } catch (NamingException e) {
            e.printStackTrace();
        }
    }
}


Make sure to change the value for "java.naming.provider.ur" to reflect your server name and port if necessary.

Now run your MyBeanClient class as a normal Java class.

You will get an out put like following in the console:

07:58:36,156 INFO  [STDOUT] Hello World!

Congratulations you have develop deploy and test your first EJB3 session bean using Jboss server and Eclipse IDE.

Reference projects are attacched.
- Session Bean project
- Client project

References:
- JBoss Application Server Documentation - Chapter 1. Enterprise Applications with EJB3 Services
- Jboss EJB3 Tutorial
- Jboss EJB3 Tutorial Sample - Stateless Session Bean
- First EJB 3 Tutorial showing a session and entity beans with annotations and JBoss.

ċ
n g,
Jun 3, 2009, 7:32 AM
ċ
n g,
Jun 3, 2009, 7:32 AM
Comments