1 Reply Latest reply on Feb 26, 2008 2:07 PM by genman

    New Bie JBoss Cache Replication

    chandra_88

      Hi,

      I have two jboss instances running on a single machine with different ports
      VM1 - port 8280
      VM2 - port 8380
      The jboss instances are participating in a cluster. I have a simple war containing a servlet and i have deployed in the farm folder of VM1 so that the war gets replicated in the other VM2 instance.

      I have a simple servlet which creates a cache and echoes its value.

      public class MyServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
       static final long serialVersionUID = 1L;
       TreeCacheMBean cache;
       private int count = 0;
       public MyServlet() {
       super();
       init();
       }
      
       public void init()
       {
       MBeanServer server=MBeanServerLocator.locateJBoss();
      
       try{
       cache=(TreeCacheMBean)MBeanProxyExt.create(TreeCacheMBean.class, "jboss.cache:service=TreeCache", server);
      
       }
       catch(Exception exp)
       {
       exp.printStackTrace();
       }
      
       }
      
       protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       // TODO Auto-generated method stub
       try{
       count++;
       cache.put("/a/b/c","Key1",count);
       cache.put("/a/b/d","Key2",count);
       response.setContentType("text/html");
       PrintWriter out = response.getWriter();
       out.println("<HTML><HEAD><TITLE>");
       Integer cachevalue = (Integer)cache.get("/a/b/c", "Key1");
       out.println(cachevalue.intValue());
      


      From this code i expected when i do http://:8280 the cache to be created and put some values in the cache . Say i made a request 10 times to this location the count val should be 10 .

      Now when i hit the url http://:8380 the value should be 10 it echoes 0 . Is it a wrong test case to test if so how i can make sure the cache gets replicated in this scenario. Note I have copied a rep-Syncservice.xml in the deploy folders of both the vm instanaces.

      let me know how do i fix this
      - Many Thanks


        • 1. Re: New Bie JBoss Cache Replication
          genman

          Here's the thing. Machine 1 might store 10, but then if you call Machine 2, it might store 0. What you need to do is more like:

          Integer count = (Integer)cache.get("/a/b/c", "Key1");
          if (count == null) // initialize
           count = 0;
          count = count + 1;
          cache.put("/a/b/c", "Key1", count);
          

          But there's a potential race condition here. Use pessimistic locking and transactions to ensure this is atomic.