Home > EJB3, SCBCD, java, programowanie | > EJB3 Tutorial part 01 – environment setup and first EJB with JBoss Tools and JBoss AS

EJB3 Tutorial part 01 – environment setup and first EJB with JBoss Tools and JBoss AS

September 25th, 2009

As a part of preparations for my SCBCD exam I will try to present a series of tutorials on EJB3 and JPA. It will help me to test my knowledge and maybe someone else will find it useful.

In the first part we will prepare our environment for development. We will create a sample EJB with a remote interface and a standalone client that connects to it.

Assumptions

  1. Some knowledge of Java.
  2. Some familiarity with Eclipse.
  3. Basic understanding what EJB3 is.

Required Software

  1. Eclipse Galileo. I use the Java Developers package.
  2. JBoss Tools. I use the 3.1 version. At the time of writing this article it was still beta for Eclipse 3.5. I've used the update site to install all of the JBoss Tools components. Not all of them are needed for EJB development but I still wanted to have them for other tests and experiments.
  3. JBoss AS. JBoss Application Server will be our target platform for deploying EJBs. I use 5.1 GA.
  4. JDK ;-)

Eclipse and JBoss AS come in packages that just need to be unpacked. After unpacking them to location of your choice and installing JBoss Tools via update site (or manual download if you prefer) you can start development. Start your Eclipse and open the Java EE perspective (Window -> Open perspective -> Other ... and select Java EE).

First, create new project for EJB. Go to File -> New -> EJB Project. Type Tutorial1 as project name. Click New next to the Target Runtime section. We will configure a new runtime for our project. Select Jboss 5.1 Runtime and hit next. Choose the path where you've unpacked your JBoss, select JRE and choose default from Configuration list. Click Finish. You're now back in project configuration. Check if the EJB module version had been changed to 3.0. Hit next. We will leave the folders configuration as it is for now. Hit finish.

Now we will add our first stateless bean. Select File -> New -> Session Bean (EJB 3.x). Provide a package name, Calculator as class name, leave type as Stateless, deselect local interface, select a remote one. Hit finish.

Open the CalculatorRemote interface and declare new method called add that returns int and takes two int parameters. Your interface should look similar to this:

package pl.mrozewski;
import javax.ejb.Remote;
 
@Remote
public interface CalculatorRemote {
    int add(int a, int b);
}

Open Calculator class and implement the missing method. I'm sure you know how :)

After that find the Servers view. We need to configure a new server instance. Right click in the servers pane and select New -> Server. Select JBoss AS 5.1 and click next two times. Add Tutorial1 project to Configured list and click Finish. Now start your server. If everything went fine there should be no exceptions in the console and the output should finish with something similar to this:

00:05:25,901 INFO  [ServerImpl] JBoss (Microcontainer) [5.1.0.GA (build: SVNTag=JBoss_5_1_0_GA date=200905221634)] Started in 39s:957ms
00:05:31,054 INFO  [Ejb3DependenciesDeployer] Encountered deployment AbstractVFSDeploymentContext@27767789{vfsfile:/home/mati/java-workspace/.metadata/.plugins/org.jboss.ide.eclipse.as.core/JBoss_5.1_Runtime_Server/deploy/Tutorial1.jar/}
00:05:31,073 INFO  [Ejb3DependenciesDeployer] Encountered deployment AbstractVFSDeploymentContext@27767789{vfsfile:/home/mati/java-workspace/.metadata/.plugins/org.jboss.ide.eclipse.as.core/JBoss_5.1_Runtime_Server/deploy/Tutorial1.jar/}
00:05:31,130 INFO  [JBossASKernel] Created KernelDeployment for: Tutorial1.jar
00:05:31,130 INFO  [JBossASKernel] installing bean: jboss.j2ee:jar=Tutorial1.jar,name=Calculator,service=EJB3
00:05:31,130 INFO  [JBossASKernel]   with dependencies:
00:05:31,131 INFO  [JBossASKernel]   and demands:
00:05:31,131 INFO  [JBossASKernel]     jboss.ejb:service=EJBTimerService
00:05:31,131 INFO  [JBossASKernel]   and supplies:
00:05:31,131 INFO  [JBossASKernel]     jndi:Calculator/remote-pl.mrozewski.CalculatorRemote
00:05:31,131 INFO  [JBossASKernel]     jndi:Calculator/remote
00:05:31,131 INFO  [JBossASKernel]     Class:pl.mrozewski.CalculatorRemote
00:05:31,131 INFO  [JBossASKernel] Added bean(jboss.j2ee:jar=Tutorial1.jar,name=Calculator,service=EJB3) to KernelDeployment of: Tutorial1.jar
00:05:31,132 INFO  [EJB3EndpointDeployer] Deploy AbstractBeanMetaData@e2f5a3{name=jboss.j2ee:jar=Tutorial1.jar,name=Calculator,service=EJB3_endpoint bean=org.jboss.ejb3.endpoint.deployers.impl.EndpointImpl properties=[container] constructor=null autowireCandidate=true}
00:05:31,177 INFO  [SessionSpecContainer] Starting jboss.j2ee:jar=Tutorial1.jar,name=Calculator,service=EJB3
00:05:31,178 INFO  [EJBContainer] STARTED EJB: pl.mrozewski.Calculator ejbName: Calculator
00:05:31,185 INFO  [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:

Calculator/remote - EJB3.x Default Remote Business Interface
Calculator/remote-pl.mrozewski.CalculatorRemote - EJB3.x Remote Business Interface

Now we will create a client application. Select File -> New -> Application Client Project. Type Tutorial1Client as project name and hit finish. Go to Properties of your project, in Java Build Path -> Projects add reference to Tutorial1 project. Now open the Main class and implement the main method:

public static void main(String[] args) {
    Properties props=new Properties();
    props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
    props.put(Context.PROVIDER_URL, "localhost:1099");
    props.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
 
    try {
        Context ctx=new InitialContext(props);
 
        CalculatorRemote calc = (CalculatorRemote) ctx.lookup("Calculator/remote");
        int result = calc.add(1, 2);
        System.out.println(result);
    } catch (NamingException e) {
        e.printStackTrace();
    }
}

Run the application. Again, if everything went smoothly we should see the result of our complex calculations ;)

As you can see this example is fairly simple. I didn't go into explaining details of created EJB and client (see Assumptions at the beginning). The functionality of our "application" is almost none but it let us check if the environment is set up properly and everything works fine for further development.

Download tutorial1.tar

EJB3, SCBCD, java, programowanie |

  1. didxga
    4.15 pm at 4.15 pm | #1

    Nice tut! thanks for share your knowledge.

  2. 6.03 pm at 6.03 pm | #2

    Hi.

    I get exception when try to run client appliation:

    Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
    javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
    at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
    at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
    at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325)
    at javax.naming.InitialContext.lookup(InitialContext.java:392)
    at Main.main(Main.java:19)

    I’m see to Jboss console and find Consolbe EJB bean in it, but i can’t access to it from client

  3. 7.30 pm at 7.30 pm | #3

    @Blaze: check if you have modified the build path for the client project as described and if you don’t have any typos in your client code (especially around setting the properties).

  4. 9.33 pm at 9.33 pm | #4

    @mati

    Thanks, Mati.

    I’m check my client code and found that write Context ctx = new InitialContext(); instead of Context ctx=new InitialContext(props);

  1. No trackbacks yet.