2 Replies Latest reply on Mar 3, 2010 1:00 PM by robert walker

    my jboss cache is not replicating

    robert walker Newbie

      Hi all, I got example 12 from jboss in action working with the clustering, very fun indeed.

      basically one client sends in numbers 0-30  to jboss cluster, (2 nodes) and each node

      gets the request randomly.

       

      i decided to take it a step further and add jboss cache to it. So when the clients sends a number to a random node (node1 or node2) I print it out AND then add it to my arraylist cache, which should get replicated to the other node.

       

      when sending numbers to cluster is done, the client makes one last call that says print cache. I expect either node (1 or 2) , whomever get chosen to service the request, to print out the arraylist (my jboss cache) showing all the numbers, ones handled/added by node1 and the numbers handled/added by node2.  BUT, the cache is not replicating, i.e, if node1 is printing and inserting 15 into it's cache, it does not replicate this to node2 cache.

       

      here is my code, can someone help (this is jboss 5.01, jdk1.5)


      =========================

      package com.manning.jbia;

       

      import javax.ejb.Stateless;
      import org.jboss.ejb3.annotation.Clustered;
      import javax.annotation.PostConstruct;

       

      import javax.annotation.PreDestroy;
      import org.apache.log4j.Logger;
      import org.jboss.cache.Cache;
      import org.jboss.cache.CacheFactory;
      import org.jboss.cache.DefaultCacheFactory;
      import org.jboss.cache.Fqn;
      import org.jboss.cache.Node;
      import java.util.ArrayList;
      import java.util.Iterator;


      @Stateless

      @Clustered
      public class CounterBean implements Counter

      {
           public static boolean jbossCacheCreated = false;

           private static Node numberCache = null;

       

           public static Node getNumbersCache()
           {

                if(numberCache==null)
                {
                     System.out.println("Initializing the cache");
                     initializeCache();
                }
                return numberCache;
           }

       

      public void printCount(int countNumber)
      {
           System.out.print(countNumber +" ");
           // add number to cache, but does not replicated to other node's cache
           Node numberCache = CounterBean.getNumbersCache();
           ArrayList al = (ArrayList)numberCache.get("numbersList");
           al.add(countNumber);

      }

       

      public static void initializeCache()
      {   

           Configuration config = new Configuration();
                      config.setCacheMode(CacheMode.REPL_SYNC);
                      config.setClusterName("mycluster");
                     CacheFactory factory = new  DefaultCacheFactory();

                     Cache cache =  factory.createCache(config);

       

                     cache.start();

       

           Node rootNode = cache.getRoot();

       

           // JBoss Cache stores data in a tree structure.
           // All nodes in the tree structure are identified by Fqn objects.
           Fqn cersFqn =
                Fqn.fromString("/jboss/cache/numbers");

       

           // Create a new Node
           numberCache = rootNode.addChild(cersFqn);

       

           // let's store some data in the node
           numberCache.put("numbersList", new ArrayList());
      }

       

      public void printCache()
      {
           System.out.println("start
           CounterBean.printCache()\n");
           ArrayList al =
                (ArrayList)numberCache.get("numbersList");
           Iterator alIterator = al.iterator();
           System.out.println("cache=[");
           while(alIterator.hasNext())
           {
                System.out.print(alIterator.next() +" ");
           }
           System.out.println("]");

       

           System.out.println("\n\nend CounterBean.printCache()");
      }

       

      @PostConstruct
      public void initialize ()
      {
           // Initialize here objects which will be used
           // by the session bean     
           System.out.println("CounterBean initialize()");
      }


      @PreDestroy
      public void destroyBean()
      {
           // Free here resources acquired by the session bean
           System.out.println("CounterBean destroyBean()");
      }

       

      }

        • 1. Re: my jboss cache is not replicating
          robert walker Newbie

          i updated the above code to make sure my cahce mode was set to replicating, but it is still not

          replicated to the other nodes in the cluster. please help

          • 2. Re: my jboss cache is not replicating
            robert walker Newbie

            dang it, i found it

             

            i changed the code around a bunch of times trying to get it to work, so going to post a working example
            of ch12 jboss in action with the already implemented clustering described in the book. VERY educational

            if you want to learn about this technology. I added to the example such that now a cache exists and is
            being replicated amongst the node1 and node2 jboss instances running

             

            basically I needed a

             

            cache.put(cersFqn, "numbersList", al);

             

            at the end of
            public void printCount(int countNumber)  method, it seems to
            push it to replication

             

            I had thought just changing the ArrayList located at tree node would do it,
            but it needs this additional step which i guess makes sense.

             

            I am giddy i got this working, goingto use it in my

            production application where I track logged on users

            in a web app

             

            =================================

            package com.manning.jbia;

             

            import javax.ejb.Stateless;
            import org.jboss.ejb3.annotation.Clustered;
            import javax.annotation.PostConstruct;
            import javax.annotation.PreDestroy;
            import org.apache.log4j.Logger;
            import org.jboss.cache.Cache;
            import org.jboss.cache.CacheFactory;
            import org.jboss.cache.DefaultCacheFactory;
            import org.jboss.cache.Fqn;
            import org.jboss.cache.Node;
            import java.util.ArrayList;
            import java.util.Iterator;
            import org.jboss.cache.config.Configuration;
            import java.util.Set;

             

            @Stateless
            @Clustered
            public class CounterBean implements Counter
            {
            public static Cache cache = null;
            public static Node rootNode = null;
            public static Node numbersNode = null;

             

            // JBoss Cache stores data in a tree structure.
            // All nodes in the tree structure are identified by Fqn objects.
            public static final Fqn cersFqn = Fqn.fromString("myNumbers");

             

            public static Cache getCache()
            {
            if(cache==null)
            {
            initializeCache();
            }
            return cache;
            }

            public static void initializeCache()
            {
            try
            {
            if(cache==null)
            {
            Configuration config = new Configuration();       
            CacheFactory factory = new DefaultCacheFactory();
            config.setCacheMode(org.jboss.cache.config.Configuration.CacheMode.REPL_ASYNC);
            cache = factory.createCache(config);
            cache.start();
            }

             

            rootNode = cache.getRoot();
            if(rootNode.dataSize()==0)
            {           
            // Create a new Node
            numbersNode = rootNode.addChild(cersFqn);           
            ArrayList arrayList = (ArrayList)numbersNode.get("numbersList");
            if(arrayList==null)
            {
            arrayList = new ArrayList();
            }
            // let's store some data in the node
            numbersNode.put("numbersList", arrayList);   
            }
            else
            {
            load(cersFqn);
            }
            }
            catch (Exception e) { e.printStackTrace(); }

            }
            public void printCount(int countNumber)
            {
            System.out.println("\n");        
            Cache myCache = CounterBean.getCache();        
            Node numbersNode = myCache.getRoot().getChild(cersFqn);

            ArrayList al = (ArrayList)numbersNode.get("numbersList");
            al.add(countNumber);

             

            cache.put(cersFqn, "numbersList", al);

            System.out.println("******** printCount(" +countNumber +") ****************");

            }
            public  void printCache()
            {
            System.out.println();
            System.out.println();

            //load(cersFqn);
            Node rootNode = getCache().getRoot();        
            Node numbersNode = rootNode.getChild(cersFqn);
            ArrayList al = (ArrayList)numbersNode.get("numbersList");

            Iterator alIterator = al.iterator();
            System.out.print("printCache() : numbersNode=[");
            while(alIterator.hasNext())
            {
            System.out.print(alIterator.next() +" ");
            }
            System.out.println("]");
            }
            private static void load(Fqn fqn)
            {
            try
            {
            // this will cause the cache to load the relevant node from a cache loader.
            cache.getRoot().getChild(fqn);
            }
            catch (Exception e)
            {
            e.printStackTrace();
            }
            }

             

            @PostConstruct
            public void initialize ()
            {
            // Initialize here objects which will be used
            // by the session bean    
            System.out.println("CounterBean initialize()");
            }       
            @PreDestroy
            public void destroyBean()
            {
            // Free here resources acquired by the session bean
            System.out.println("CounterBean destroyBean()");
            }
            }
            ==========================
            package com.manning.jbia;

             

            import javax.ejb.Remote;

             

            @Remote
            public interface Counter {
            public void printCount(int messageNumber);
            public void printCache();
            }
            ===============================
            package com.manning.jbia;

             

            import java.util.Hashtable;

             

            import javax.naming.Context;
            import javax.naming.InitialContext;
            import org.jboss.cache.config.ConfigurationException;

             


            public class Client
            {
            public static void main(String[] args) throws Exception
            {
            Hashtable<String, String> properties = new Hashtable<String, String>();
            properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
            //          properties.put(Context.URL_PKG_PREFIXES, "org.jboss.naming");
            properties.put(Context.PROVIDER_URL, "192.168.1.140:1100,192.168.1.141:1100");     
            InitialContext ctx = new InitialContext(properties);
            Counter s = (Counter) ctx.lookup("CounterBean/remote");
            System.out.println("Sending 0-9 to server");
            for (int i = 0; i < 10; i++) {
            s.printCount(i);
            Thread.sleep(1000);
            }
            System.out.println("Sending request to print cache");
            s.printCache();
            }
            }
            ============================
            and a stand alone app to request what is currently in the cache
            from whatever node services the request

             

            package com.manning.jbia;

             

            import java.util.Hashtable;

             

            import javax.naming.Context;
            import javax.naming.InitialContext;
            import org.jboss.cache.config.ConfigurationException;

             

            public class PrintCache
            {
            public static void main(String[] args) throws Exception
            {
            Hashtable<String, String> properties = new Hashtable<String, String>();
            properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
            properties.put(Context.PROVIDER_URL, "192.168.1.140:1100,192.168.1.141:1100");          
            InitialContext ctx = new InitialContext(properties);
            Counter s = (Counter) ctx.lookup("CounterBean/remote");          
            System.out.println("Sending request to print cache");
            s.printCache();
            }
            }