Friday, March 15, 2013

Java EE 6 - Part 3 - Writing Singleton Session Beans

Project: EJB31-Common

package helloworld.beans;

import helloworld.vo.GreetingRequest;
import javax.ejb.Remote;

 * Remote interface for singleton bean.
 * @author rwatsh
public interface SingletonHelloWorldBeanRemote {

    GreetingRequest sayHello();

    GreetingRequest[] auditRequests();

Project: EJB31-ejb

package helloworld.beans;

import helloworld.vo.GreetingRequest;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PreDestroy;
import javax.ejb.EJB;
import javax.ejb.Singleton;

 * Singleton bean implementation.
 * Note: It depends on the TimeBasedHelloWorldBean (not shown in this post) which is
 * another singleton bean. The TimeBasedHelloWorldBean will be initialized before this bean.
 * @author rwatsh
public class SingletonHelloWorldBean implements SingletonHelloWorldBeanRemote {

    private TimeBasedHelloWorldBean timeBasedHelloWorldBean;
    private List greetingRequests;

    public SingletonHelloWorldBean() {
        greetingRequests = new ArrayList();
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")

    public GreetingRequest sayHello() {
        String greeting = timeBasedHelloWorldBean.sayHello();
        GreetingRequest request = new GreetingRequest(greeting);
        return request;

    public GreetingRequest[] auditRequests() {
        return greetingRequests.toArray(new GreetingRequest[]{});

    private void destroy() {
        System.out.println("helloworld.beans.SingletonHelloWorldBean: @PreDestroy");
        for (GreetingRequest gr : greetingRequests) {
        greetingRequests = null;

Project: StandAloneApp

package standaloneapp;

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

 * Singleton bean test client.
 * @author rwatsh
public class SingletonHelloWorldBeanTest {

    private static SingletonHelloWorldBeanRemote helloWorldBean;

    public static void main(String[] args) {
        System.err.println("standaloneapp.Main.main: Stand-alone App started");
        String jndiPath = "java:global/EJB31/EJB31-ejb/SingletonHelloWorldBean";
        try {
            Context ctx = new InitialContext();
            System.out.println("standaloneapp.Main.main: looking up bean at: "
                    + jndiPath);
            helloWorldBean = (SingletonHelloWorldBeanRemote) 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[] audit = helloWorldBean.auditRequests();
            System.out.println("SingletonHelloWorldBeanTest.main: number of sayHello requests made on Singleton: " + audit.length);
        } catch (NamingException ex) {
                    "standaloneapp.Main.main: Could not find HelloWorldBeanRemote");
            System.err.println("standaloneapp.Main.main: JNDI path used for lookup: " + jndiPath);

No comments:

Book notes: Designing Data-Intensive Applications: The Big Ideas Behind Reliable, Scalable, and Maintainable Systems, by Martin Kleppmann

My notes from the excellent book on how software has evolved to handle data from hierarchical databases to the NoSQL -  https://www.goodrea...