How to write a simple EJB

A simple EJB contains remote/local interface, and a class which implements that interface along with an optional configuration file (XML). For the following example, JBoss Application Server 4.2.3.GA has been used where the EJB would be deployed and tested.

You have to add the following external JARs also from the JBoss Application Server client and server libraries to be able to import the javax.ejb.* libraries and the @Remote Annotation:

- From ...\jboss-4.2.3.GA\client\:
Add jbossall-client.jar and jnp-client.jar.

- From ...\\jboss-4.2.3.GA\server\default\lib\:
Add ejb3-persistence.jar and jboss-ejb3x.jar

Now, let's start writing the EJB:
  1. Remote/Local Interface:

    Consider the following interface:

    import javax.ejb.*;


    @Remote
    public interface MyRemoteInterface{
    public String SayHello(String name);

    }

  2. A Bean implementation class:
    Now, write the implementation of the interface as follows:

    import javax.ejb.*;

    @Stateless
    public class MyImplementation implements MyRemoteInterface{
    public String SayHello(String name){
    return "Hello "+name;
    }
    }

  3. XML configuration file (optional): This is an optional file to create. We won't create it for this example. It won't effect the operation of the Bean.
  • Deployment:
    Compile the interface and the implementation class developed above. And create a jar file for their classes. Open Command Prompt and execute the following command from the top level of the compiled class package:
    C:\....classes\> jar cvf MyBean.jar *.*

    This should create a jar file named "MyBean.jar". Now, copy this file into the deploy folder of JBoss Server in the directory: "...jboss-4.2.3.GA\server\default\deploy\".

  • Testing the Bean:
    Now, we need to test the Bean we just wrote. To do this, we can either write a console client or a web client. For this simple example, we will write a console client as follows:

    import java.util.Properties;
    import javax.naming.Context;

    import javax.naming.InitialContext;
    import javax.naming.NamingException;
    import javax.rmi.PortableRemoteObject;

    public class MyBeanClient{
    public static Context getInitialContext() throws javax.naming.NamingException {
    Properties p = new Properties();

    p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
    p.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
    p.put(Context.PROVIDER_URL, "jnp://localhost:1099");
    return new InitialContext(p);
    }
    public static void main(String[] args) {
    try {
    Context jndiContext = getInitialContext();
    Object ref = jndiContext.lookup("MyImplementation/remote");
    MyRemoteInterfacedao = (MyRemoteInterface)
    ortableRemoteObject.narrow(ref,
    MyRemoteInterface.class);
    System.out.println(dao.SayHello("Faraz Rafi"));
    }
    catch(ClassCastException cce){
    System.out.println("Class CAST Exception Caught: "+cce.getMessage());
    }
    catch (NamingException ne) {
    System.out.println("Naming Exception caught: " + ne.getMessage());
    }
    }
    }

    Now, start the JBoss Application Server, by executing the following command in the command prompt:
    C:\...\jboss-4.2.3.GA\bin> run.bat
    This should start the JBoss Application Server and should deploy our EJB onto the Server. Now execute the Client we just wrote, which should display the following Output:
    Hello Faraz Rafi
I hope, this helps.

Comments

Popular posts from this blog

Type casting problem while unmarshalling XML objects using JAXB

Second Interview at Seattle, Microsoft HQ (Part I)

My J2EE/Component Based Software Design Projects