1 package org.jboss.cache.loader; 2 3 import org.jboss.cache.CacheImpl; 4 import org.jboss.cache.Fqn; 5 import org.jboss.cache.config.CacheLoaderConfig; 6 import org.jboss.cache.config.Configuration; 7 import org.jboss.cache.factories.XmlConfigurationParser; 8 import org.jboss.cache.misc.TestingUtil; 9 import org.jboss.cache.xml.XmlHelper; 10 import org.w3c.dom.Element ; 11 12 16 public class RpcDelegatingCacheLoaderTests 17 { 18 CacheImpl cache; 19 20 21 public static void main(String [] args) 22 { 23 boolean client = false; 24 25 for (int i = 0; i < args.length; i++) 26 { 27 String arg = args[i]; 28 if (arg.equals("client")) 29 { 30 client = true; 31 continue; 32 } 33 if (arg.equals("server")) 34 { 35 client = false; 36 continue; 37 } 38 help(); 39 return; 40 } 41 42 try 43 { 44 new RpcDelegatingCacheLoaderTests().start(client); 45 } 46 catch (Exception e) 47 { 48 e.printStackTrace(); 49 } 50 } 51 52 private static void help() 53 { 54 System.out.println("RpcDelegatingCacheLoadeTest [client / server]"); 55 } 56 57 private void start(boolean client) throws Exception 58 { 59 cache = new CacheImpl(); 60 cache.getConfiguration().setClusterName("test-cluster"); 61 cache.getConfiguration().setInitialStateRetrievalTimeout(3000); 62 cache.getConfiguration().setCacheMode(Configuration.CacheMode.REPL_ASYNC); 63 cache.getConfiguration().setFetchInMemoryState(true); 64 cache.getConfiguration().setCacheLoaderConfig(getCacheLoaderConfig()); 65 cache.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 66 cache.start(); 67 68 if (client == false) 69 { 70 System.out.println("started as SERVER"); 71 System.out.println("populating the cache"); 72 cache.getTransactionManager().begin(); 73 cache.put("/a/b/c", "bela", "ban"); 74 cache.put("/1/2/3", "ben", "wang"); 75 cache.getTransactionManager().commit(); 76 System.out.println("the cache is " + cache.toString() + ", locks: " + cache.printLockInfo()); 77 78 while (true) 79 { 80 TestingUtil.sleepThread(2000); 81 cache.getTransactionManager().begin(); 82 cache.put("/x/y/z", "d", "d"); 83 cache.getTransactionManager().commit(); 84 } 85 } 86 87 System.out.println("started as CLIENT"); 88 System.out.println("initial cache (fetched from SERVER) is " + cache.printLockInfo()); 89 97 TestingUtil.sleepThread(1000); 99 100 System.out.println("evicting /a and /1 locally"); 101 cache.evict(Fqn.fromString("/a/b/c")); 102 cache.evict(Fqn.fromString("/a/b")); 103 cache.evict(Fqn.fromString("/a")); 104 cache.evict(Fqn.fromString("/1/2/3")); 105 cache.evict(Fqn.fromString("/1/2")); 106 cache.evict(Fqn.fromString("/1")); 107 System.out.println("cache contents after eviction: " + cache.printLockInfo()); 108 109 System.out.println("accessing /a and /1. this will trigger a fetch from the remote SERVER"); 110 Object val = cache.get("/a/b/c", "bela"); 111 System.out.println("value is " + val); 112 113 val = cache.get("/1/2/3", "ben"); 114 System.out.println("value is " + val); 115 116 System.out.println("the cache is " + cache.toString() + ", locks: " + cache.printLockInfo()); 117 } 118 119 protected CacheLoaderConfig getCacheLoaderConfig() throws Exception 120 { 121 String xml = " <config>\n" + 122 " \n" + 123 " <passivation>false</passivation>\n" + 124 " <preload></preload>\n" + 125 "\n" + 126 " <cacheloader>\n" + 127 " <class>org.jboss.cache.loader.RpcDelegatingCacheLoader</class>\n" + 128 " <async>false</async>\n" + 129 " <fetchPersistentState>false</fetchPersistentState>\n" + 130 " <ignoreModifications>false</ignoreModifications>\n" + 131 " </cacheloader>\n" + 132 " \n" + 133 " </config>"; 134 Element element = XmlHelper.stringToElement(xml); 135 return XmlConfigurationParser.parseCacheLoaderConfig(element); 136 } 137 } 138 | Popular Tags |