4 Replies Latest reply on Nov 8, 2005 12:52 AM by Alan Ho

    Clustering, Failover, Replication Examples

    Alan Ho Newbie

      I went through the DVD Store TrailBlazer in JBoss site hoping to find some example on how to implement Clustering, Failover and Replication.

      In a section of the trailbalzer, it points out that :

      In a distributable web application, entities can be placed in the HTTP session by a web component. In the event of fail over, the entities could continue to be used on the new machine and persisted back to the database with no special action taken on the part of the application.

      In the DVD Store example, both the Web and EJB container reside in the same machine. If one fails, the load balancer redirect the request to the second sever. By the time the servlet in the second machine takes the request it has it's own HTTP session and will have no reference to the entity bean. How can this work. Is my assumption correct? Hope some one can clarify.

      What about session beans? In the event of fail over, how can session beans could continue to be used on the new machine? Would appreciate some one can give examples or specify what needs to be done.

      Many thanks
      - aho

        • 1. Re: Clustering, Failover, Replication Examples
          Ben Wang Master

          I'd imagine you will need to look up either your session or entity bean for the first time around. Maybe then you can cache the ID to httpsession.

          -Ben

          • 2. Re: Clustering, Failover, Replication Examples
            Alan Ho Newbie

            Dear Ben,

            I create a small shopping cart stateful session bean and a jsp to test out the http session replication. I am using Apace2 as load balancer, JBoss 4.0.3RC1 in two machines. I started two JBoss AS, go to the brwoser to acess the jsp and I got hold of the session id. After shutting down the JBoss instance which I am accessing and go to the browser to continue with the jsp page, it redirects to the other JBoss instance, giving me the same session id, but the shopping card content is lost which means that the reference to the shopping cart stateful session bean which I stored in the http sesssion could not be retreived in the other machine. Why is it that, the session id reamains but the object stored in the http session is lost. I set the worker.loadbalancer.sticky_session=1 in workers.properties. I read somewhere that for sticky session, replication is not implemented,
            Here are my codes.

            package stateful.bean;

            import java.io.Serializable;
            import java.util.*;
            import javax.ejb.Remove;
            import javax.ejb.Stateful;
            import javax.ejb.Remote;
            import org.jboss.annotation.ejb.Clustered;

            <<<< Stateful session Beans
            @Stateful
            @Clustered
            @Remote(ShoppingCart.class)
            public class ShoppingCartBean implements ShoppingCart, Serializable
            {
            private Vector cart = new Vector();

            public void buy(String product, int quantity)
            {
            if (cart.indexOf(product) < 0)
            {
            cart.add(product);
            }
            }

            public Vector getCartContents()
            {
            return cart;
            }

            @Remove
            public void checkout()
            {
            System.out.println("To be implemented");
            }
            }


            <<< JSP >>>

            <%@ page import="stateful.bean.*, javax.naming.*, java.util.*" %>

            <%
            String msg = "";
            String product;
            String list = "";
            int index;
            int quantity;
            if ((request.getParameter ("product") != null) && (request.getParameter ("quantity") != null)) {
            product = request.getParameter ("product");
            quantity = Integer.parseInt(request.getParameter ("quantity"));
            try {
            ShoppingCart cart = (ShoppingCart) session.getAttribute("sfsb_shoppingcart");
            if (cart == null) {
            msg = msg + "cart is null - " + request.getSession().getId();
            InitialContext ctx = new InitialContext();
            cart = (ShoppingCart) ctx.lookup(ShoppingCart.class.getName());
            session.setAttribute ("sfsb_shoppingcart", cart);
            }

            if (quantity == 0) {
            cart.checkout();
            session.removeAttribute ("sfsb_shoppingcart");
            } else {
            cart.buy(product, quantity);
            list = "";

            Vector fullCart = cart.getCartContents();
            for (index = 0; index < fullCart.size(); index++){
            list = list + (String)fullCart.get(index) + "";
            }
            }

            } catch (Exception e) {
            msg = e.toString();
            }
            } else {
            msg = "Not posted";
            }
            %>


            <body bgcolor=blue>

            Shopping Cart

            Product =
            Quantity =




            <%=msg%><%=request.getSession().getId() %>

            Product List
            <%=list%>



            <<<< workers.properties >>>
            # Define list of workers that will be used
            # for mapping requests
            worker.list=loadbalancer,status
            # Define Node1
            worker.node1.port=8009
            worker.node1.host=192.168.0.54
            worker.node1.type=ajp13
            worker.node1.lbfactor=1
            #worker.node1.local_worker=1
            worker.node1.cachesize=10

            # Define Node2
            worker.node2.port=8009
            worker.node2.host=192.168.0.107
            worker.node2.type=ajp13
            worker.node2.lbfactor=1
            #worker.node2.local_worker=1 (1)
            worker.node2.cachesize=10

            # Load-balancing behaviour
            worker.loadbalancer.type=lb
            worker.loadbalancer.balance_workers=node1,node2
            worker.loadbalancer.sticky_session=1
            worker.loadbalancer.local_worker_only=1
            worker.list=loadbalancer

            # Status worker for managing load balancer
            worker.status.type=status

            • 3. Re: Clustering, Failover, Replication Examples
              Manik Surtani Master

              have you tried this with sticky_sessions set to 0?

              • 4. Re: Clustering, Failover, Replication Examples
                Alan Ho Newbie

                I have tried setting the sticky_session = 0 but it did not solve the problem. The only different is that each post request alternates between the two machine. I have also tried to save a serialised object in the http session but it is still not replicated when the request is re-directed to the next machine after a fail-over.

                Is there a way to verify http session replication is implemented in the JBoss console screen?

                Many Thanks,
                - aho