Friday, March 15, 2013

Java EE 6 - Part 2 - Writing Stateful Session Beans

Below is an example program using Java EE 6 APIs for Stateful Session beans.

This example shows the client using JNDI lookup directly to access the session bean remote proxy instance. The other way is to use @EJB or using CDI's @Inject

Project: EJB31-Common
import helloworld.vo.GreetingRequest;
import javax.ejb.Remote;

/**
 * Remote interface for stateful session bean.
 *
 * @author rwatsh
 */
@Remote
public interface StatefulHelloWorldBeanRemote {

    GreetingRequest sayHello();

    GreetingRequest[] sayGoodBye();
    
}
--------------

package helloworld.vo;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * Serializable type which holds the session state.
 *
 * @author rwatsh
 */
public class GreetingRequest implements Serializable {

    private Calendar requestTime;
    private String greeting;

    public GreetingRequest(String greet) {
        requestTime = Calendar.getInstance();
        greeting = greet;
    }

    public String toString() {
        SimpleDateFormat df = new SimpleDateFormat();
        String ds = df.format(requestTime.getTime());
        return "helloworld.vo.GreetingRequest [requestTime=" + ds + ", greeting=" + greeting + "]";
    }
}
----------------------
Project: EJB31-ejb
package helloworld.beans;

import helloworld.vo.GreetingRequest;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Remove;
import javax.ejb.Stateful;

/**
 * This is the stateful session bean implementation. 
 * Requires EJB31-Common.jar in its build path.
 *
 * @author rwatsh
 */
@Stateful
public class StatefulHelloWorldBean implements StatefulHelloWorldBeanRemote {
    private List greetingRequests;

    public StatefulHelloWorldBean() {
        greetingRequests = new ArrayList();
    }

    @Override
    public GreetingRequest sayHello() {
        String greeting = "Hello EJB World";
        GreetingRequest request = new GreetingRequest(greeting);
        greetingRequests.add(request);
        return request;
    }

    @Override
    @Remove
    public GreetingRequest[] sayGoodBye() {
        return greetingRequests.toArray(new GreetingRequest[]{});
    }
}

Project: StandAloneApp
package standaloneapp;

import helloworld.beans.StatefulHelloWorldBeanRemote;
import helloworld.vo.GreetingRequest;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 * A standalone Java application client to execute the EJB.
 * Note: it requires the appserver-rt.jar and the EJB31-Common.jar in its build path.
 *
 * @author rwatsh
 */
public class StatefulHelloWorldBeanTest {

    private static StatefulHelloWorldBeanRemote helloWorldBean;

    public static void main(String[] args) {
        System.err.println("standaloneapp.Main.main: Stand-alone App started");
        String jndiPath = "java:global/EJB31/EJB31-ejb/StatefulHelloWorldBean";
        try {
            Context ctx = new InitialContext();
            System.out.println("standaloneapp.Main.main: looking up bean at: "
                    + jndiPath);
            helloWorldBean = (StatefulHelloWorldBeanRemote) ctx.lookup(jndiPath);
            System.out.println("standaloneapp.Main.main: found HelloWorldBean: "
                    + helloWorldBean);
            System.out.println("standaloneapp.Main.main: calling sayHello");
            GreetingRequest greeting = helloWorldBean.sayHello();
            System.out.println("standaloneapp.Main.main: HelloWorldBean said: "
                    + greeting);
            GreetingRequest [] greetings = helloWorldBean.sayGoodBye();
        } catch (NamingException ex) {
            System.err.println(
                    "standaloneapp.Main.main: Could not find HelloWorldBeanRemote");
            System.err.println("standaloneapp.Main.main: JNDI path used for lookup: " + jndiPath);
            ex.printStackTrace();
        }
    }
}

No comments: