Page 1 of 1

Simple EJB 3.0 Tutorial Rate Topic: -----

#1 javadork  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 32
  • View blog
  • Posts: 135
  • Joined: 21-August 10

Posted 01 September 2010 - 09:58 PM

The following tutorial will show you how to create a stateless EJB 3.0 using Java 6 on JBoss 5.1. It does not attempt to explain the details and intricacies of Java EE or EJBs, but is rather designed to get you up and running quickly and to provide a springboard to further tinkering and research.

To begin with, you'll want to create a directory to place the source code, which for this example c:\calcejb\example will be used. In this directory create the remote interface, CalcRemote.java:

package example;

import javax.ejb.Remote;

@Remote
public interface CalcRemote {
  
  public int add(int x, int y);
  
  public int sub(int x, int y);
  
}



Next, create the bean, CalcBean.java:

package example;

import javax.ejb.Stateless;

@Stateless
public class CalcBean implements CalcRemote {
  
  public CalcBean() {}
  
  public int add(int x, int y) {
    return x + y;
  }
  
  public int sub(int x, int y) {
    return x - y;
  }
  
}



And finally a class to test the EJB, TestCalcEJB.java:

package example;

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

public class TestCalcEJB {
  
  public static void main(String[] args) {
    
    try {
      Context con = new InitialContext();
      CalcRemote cr = (CalcRemote)con.lookup("CalcBean/remote");
      System.out.println("1 + 1 = " + cr.add(1, 1));  // 2
    }
    catch (Exception x) {
      x.printStackTrace();
    }
    
  }
  
}



Now, compile the EJB from the c:\calcejb\example directory with the following command:

javac -cp c:\jboss-5.1.0.GA\client\jbossall-client.jar;. *.java


Be sure to specify the correct path to your JBoss installation.

We'll package the EJB into a JAR file for deployment. Change to the c:\calcejb directory and issue the JAR command:

jar cvf calcjeb.jar example\CalcRemote.class example\CalcBean.class


To deploy the EJB, start JBoss and then drop the calcejb.jar into the autodeploy directory, which, if you have installed and run JBoss in the default manner, will be located at %JBOSS_HOME%\server\default\deploy. (Note: If you've defined the JBOSS_HOME environment variable, which is highly recommended, you could have substituted %JBOSS_HOME% for c:\jboss-5.1.0.GA in the compile command. I'll use this syntax below.)

If successful, in the JBoss logs you should see something along these lines:

[EJBContainer] STARTED EJB: example.CalcBean ejbName: CalcBean


With the EJB successfully deployed, we can now test it. In the c:\calcejb directory, create a jndi.properties file:

java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1099



Then, run the test class:

java -cp %JBOSS_HOME%\client\jbossall-client.jar;. example.TestCalcEJB


If all went well, you'll see

1 + 1 = 2


(Note: If you receive log4j warnings, you can remove them by creating a log4j.properties in the same directory as jndi.properties or include %JBOSS_HOME%\client\policy.jar in the classpath.)

While a more complex Java EE subject then say JSPs or servlets, EJBs are relatively painless to understand and work with once you get used to them. (EJB 3.0 and 3.1 at least, versions prior like EJB 2.1 can be icky.) There are a ton of resources out there on the web, so use Google judiciously.

Is This A Good Question/Topic? 1
  • +

Page 1 of 1