Saturday, August 20, 2016

Shopping Cart Example with EJB 3 and GlassFish 3 - Stateful Session Bean and Remote Client

The cart example represents the operations of a shopping cart in online bookstore. This example uses

1. Java Enterprise Application (Cart)

2. Remote Business Interface (Cart.java)

3. Session Bean Class (CartBean.java)

4. Two Helper classes (BookException.java and IdVerifier.java)

5. A servlet client (CartClient.java)


Before continue this post, please go through previous post of Enterprise Application with EJB and Dependency Injection.


1. Java Enterprise Application (Cart)



Create Java EE application using  NetBeans. File -> New Project -> Java EE -> Enterprise Application. And name it Cart. And choose GlassFish server. This will create 3 nodes in Projects explorer: Cart, Cart-ejb, Cart-war.


2. Remote Business Interface (Cart.java)

Create Cart.java in cart.ejb package under Cart-ejb node.
Cart.java is a plain java interface that defines all business methods implemented in the bean class. If a bean class implements a single interface, this interface is assumed to be business interface. Otherwise if bean class implements more than 1 interface then the business interface must be explicitly annotated. The business  interface can be annotated with @Remote (javax.ejb.Remote) or @Local (javax.ejb.Local). If not annotated anything, the interface is local. 

In this example, the interface is remote. The following interfaces are excluded when any bean class implements more than 1 interface: java.io.Serializable , java.io.Externalizable and any inteface under javax.exjb package.

The source code of Cart.java interface:


package cart.ejb;

import cart.util.BookException;
import java.util.List;
import javax.ejb.Remote;

/**
 *
 * @author Khan.Ashik
 */
@Remote
public interface Cart {
   
    public void initialize(String person) throws BookException;
   
    public void initialize(String person, String id) throws BookException;
   
    public void addBook(String title);
   
    public void removeBook(String title) throws BookException;
   
    public List<String> getContents();
   
    public void remove();
   
}


3. Session Bean Class (CartBean.java)

Create CartBean.java in cart.ejb package under Cart-ejb node.

■ The class is annotated with @Stateful  and implements the methods of Cart.java interface.

■ Since it is a stateful session bean, it can implement any optional lifecycle callback methods annotated  with @POstConstruct, @PreDestroy, @PostActivate and @PrePassivate.

■ It also can implment any optional business method annotated @Remove.



The source code of CartBean.java class:


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cart.ejb;

import cart.util.BookException;
import cart.util.IdVerifier;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Remove;
import javax.ejb.Stateful;

/**
 *
 * @author Khan.Ashik
 */
@Stateful
public class CartBean implements Cart{

    List<String> contents;
    String customerId;
    String customerName;
   
    @Override
    public void initialize(String person) throws BookException {
        if(person==null)
        {
            throw new BookException("Null person not aalowed.");
        }
        else
        {
            customerName = person;
        }
        customerId = "0";
        contents = new ArrayList<String>();
    }

    @Override
    public void initialize(String person, String id) throws BookException {
        if(person==null)
        {
            throw new BookException("Null person not aalowed.");
        }
        else
        {
            customerName = person;           
        }
       
        IdVerifier idChecker = new IdVerifier();
       
        if(idChecker.validate(id))
        {
            customerId = id;
        }
        else
        {
            throw new BookException("Invalid Id: " + id);
        }
       
       
        contents = new ArrayList<String>();
    }

    @Override
    public void addBook(String title) {
        contents.add(title);
    }

    @Override
    public void removeBook(String title) throws BookException {
        boolean result = contents.remove(title);
       
        if(result==false)
        {
            throw new BookException("\"" + title + "\" not in the cart.");
        }
    }

    @Override
    public List<String> getContents() {
        return contents;
    }

    @Remove
    public void remove() {
        contents = null;
    }
   
}


4. Helper Classes (BookException.java and IdVerifier.java)

Create BookException.java and IdVerifier.java in cart.util package under Cart-ejb node.

CartBean has 2 helper classes. BookException is thrown by removeBook method and IdVerifier validates the customer Id.

The source code of BookException.java class:

package cart.util;

/**
 *
 * @author Khan.Ashik
 */
public class BookException extends Exception{

    public BookException() {
    }

    public BookException(String message) {
        super(message);
    }
   
   
   
}

The source code of IdVerifier.java class:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cart.util;

/**
 *
 * @author Khan.Ashik
 */
public class IdVerifier {

    public IdVerifier() {
    }
   
    public boolean validate(String id)
    {
        boolean result = true;
       
        for(int i=0; i        {
            if(Character.isDigit(id.charAt(i))==false)
            {
                result = false;
            }
        }
        return result;
    }
}


4. Servlet Client (CartClient.java)

Create CartClient.java in cart.client package under Cart-war node.

Here we inject CartBean by annotating  @EJB to Cart interface to get a new reference to the enterprise bean.

The code snippet is:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cart.client;

import cart.ejb.Cart;
import cart.util.BookException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Khan.Ashik
 */
@WebServlet(name="CartServlet", urlPatterns="/clientServlet")
public class CartClient extends HttpServlet{

    @EJB
    private Cart cart;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       
        PrintWriter writer = resp.getWriter();
        try {
            cart.initialize("MY BOOK", "123");
            cart.addBook("Head First");
            cart.addBook("Bean Developer");
            cart.addBook("Street Show");
           
            List bookList = cart.getContents();
           
            Iterator iterator = bookList.iterator();
           
            while(iterator.hasNext())
            {
                String title = iterator.next();
                writer.write("Retrieving book title from cart: " + title+"\n");
            }
           
            writer.write("Removing \"MY BOOK\" from cart\n");
           
            cart.removeBook("MY BOOK");
           
            cart.remove();
           
           
           
        } catch (BookException ex) {
            writer.write("Caught a BookException: " + ex.getMessage());
           
        } catch (Exception ex) {
            writer.write("Caught an unexpected exception!");
            ex.printStackTrace();
           
        }
    }
   
   
   
}


Deploy and run the application. Go to servlet URL to get the output of the bean.

And thats all about this example.









No comments:

Post a Comment