Wednesday, January 12, 2011

Hello world EJB 3.1 with JBOSS

I’ve been in the Microsoft world for a while, there are a lot distributed technology in the Microsoft stack. DCOM, COM+,Remoting, ASMX/WCF.  What’s the equivalent term for EJB? I would say that’s dot net remoting.

for a typical remoting project, we need define the MBR Object, Host it ( single call, singleton or Client control activation.) using different options, (IIS or in-process hosting. the runtime will take care the proxy/sub stuff.)

here I will put a hello world EJB. Jboss will be the role of IIS.

like remoting,First define a Interface (shared as a contract between server and client. )

public interface ICalculatorBusinessObjects {
     public int Add(int ... args);

then Implement a MBR like remoting. 

public class CalculatorBeanBase implements ICalculatorBusinessObjects {

    public int Add(int... args) {
        // TODO Auto-generated method stub
        int i=0;
        System.out.println("------------------" + "get HIt on Server side, My hashcode" + this.hashCode() );
        for (int j : args) {
        return i;



like the Microsoft attribute, those @xx are annotations.  the runtime will check thos annotation and enforce some ad-hoc logic. like provide different hosting control, transaction control,etc.

Once Done, need deployed it to a container or server . for .net , it’s just a assembly or a dll. for J2ee, a zip file called EJB jar.  you can click eclipse and export the build to a jar file firstEjb.jar


then deploy the jar to Jboss.  drag and drop the jar to c:\jboss6\server\default\deploy folder. ( for standard configuration,  c:\jboss6\server\all\deploy  if you run with –c all option.)

the server discovers new bits and host it, also register a JDNI entry.  like the remoting .rem stuff.
the ejb is hosted and ready for client connection now.

for client code, just find the proxy by querying the JNDI server and call the biz logic.

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class main {

    public static void main(String[] args) throws NamingException {
        // TODO Auto-generated method stub
        Context context=new  InitialContext();
        ICalculatorBusinessObjects o=(ICalculatorBusinessObjects)context.lookup("java:CalculatorBeanBase/remote");


when you run the jboss, Jboss is also one JNDI server listening one a specific port , 1099 by default.

so you need tell the client, which jndi server we should talk to by put some environment variables like the following.



When you run the client, you will get the result as you expected, a simple calculator. but all logic runs on server, the Jboss jvm.


Jboss log.


here concludes the basic tutorial. have fun.


Marcelo Mrack said...

Hi, nice, but @Override are incorrect here, since the bean arent overriding anything, but only implementing the biz interface.

androidyou said...


@override here just make sure it overrides the method defined in the interface.

Indicates that a method declaration is intended to override a method declaration in a superclass. If a method is annotated with this annotation type but does not override a superclass method, compilers are required to generate an error message.

Locations of visitors to this page