rfwalker (8) [Avatar] Offline
#1
Hi all, I got example 12 working with the clustering, very fun indeed. i decided to takeit a step further and add jboss cache to it. So when the clients sends a number to the 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
=========================
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()
");
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("

end 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()");
}



}

Message was edited by:
rfwalker

Message was edited by:
rfwalker

Message was edited by:
rfwalker
rfwalker (8) [Avatar] Offline
#2
Re: i added cache to ex12 clustering, but its not replicating
dang it, i found it

i changed the code around a bunch of times, so going to post a working example
of ch12 with the already implemented clustering described, and now a cache
being replicated amongst the node1 and node2 jboss instances running

basically I need

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.
=========================================
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("
");
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();
}
}
javidjamae (28) [Avatar] Offline
#3
Re: i added cache to ex12 clustering, but its not replicating
Glad you got it. I'll give it a shot myself when I get a chance.