[jboss-user] [JBoss Cache] New message: "Re: my jboss cache is not replicating"

robert walker do-not-reply at jboss.com
Wed Mar 3 13:00:11 EST 2010


User development,

A new message was posted in the thread "my jboss cache is not replicating":

http://community.jboss.org/message/529701#529701

Author  : robert walker
Profile : http://community.jboss.org/people/robertwalker

Message:
--------------------------------------------------------------
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();
 }
}

--------------------------------------------------------------

To reply to this message visit the message page: http://community.jboss.org/message/529701#529701




More information about the jboss-user mailing list