4.2. Writing the bean's code

The HelloWorld bean is divided in two parts. the business interface and the class implementing this interface.

4.2.1. Writing the Interface

The interface declares only one method, helloWorld() :

package org.objectweb.easybeans.tutorial.helloworld;

/**
 * Interface of the HelloWorld example.
 * @author Florent Benoit
 */
public interface HelloWorldInterface {

    /**
     * Hello world.
     */
    void helloWorld();

}
[Note]Note

Even if this interface is used as a remote interface, it doesn't need to extend java.rmi.Remote interface.

4.2.2. Writing the business code

The following code implements the existing interface :

package org.objectweb.easybeans.tutorial.helloworld;

/**
 * Business code for the HelloWorld interface.
 * @author Florent Benoit
 */
public class HelloWorldBean implements HelloWorldInterface {

    /**
     * Hello world implementation.
     */
    public void helloWorld() {
        System.out.println("Hello world !");
    }

}
[Note]Note

At this moment, the bean is not an EJB, this is only a class implementing an interface.

4.2.3. Defining it as a stateless session bean

Now that the code of the EJB has been written, it's the time to define the EJB application.

This bean will be a stateless session bean, so the class will be annotated with @Stateless annotation.

And the interface needs to be available for remote clients, so it will be a remote interface. This is done by using the @Remote annotation.

package org.objectweb.easybeans.tutorial.helloworld;

/**
 * Business code for the HelloWorld interface.
 * @author Florent Benoit
 */
@Stateless
@Remote(HelloWorldInterface.class)
public class HelloWorldBean implements HelloWorldInterface {

    /**
     * Hello world implementation.
     */
    public void helloWorld() {
        System.out.println("Hello world !");
    }

}
[Note]Note

If a class implements a single interface, this interface is defined as a local interface by default.

4.2.4. Packaging the bean

The two classes ( HelloWorldInterface and HelloWorldBean) have to be compiled.

Then, a folder named ejb3s/helloworld.jar/ needs to be created and classes have to go in this folder. They will be deployed and loaded automatically.