KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > tests > loader > RpcDelegatingCacheLoaderTests


1 package org.jboss.cache.tests.loader;
2
3 import org.jboss.cache.Fqn;
4 import org.jboss.cache.TreeCache;
5
6 /**
7  * @author Bela Ban
8  * @version $Id: RpcDelegatingCacheLoaderTests.java,v 1.1.1.1 2005/03/31 10:15:09 belaban Exp $
9  */

10 public class RpcDelegatingCacheLoaderTests {
11    TreeCache cache;
12
13
14    public static void main(String JavaDoc[] args) {
15       boolean client=false;
16
17       for(int i=0; i < args.length; i++) {
18          String JavaDoc arg=args[i];
19          if(arg.equals("client")) {
20             client=true;
21             continue;
22          }
23          if(arg.equals("server")) {
24             client=false;
25             continue;
26          }
27          help();
28          return;
29       }
30
31       try {
32          new RpcDelegatingCacheLoaderTests().start(client);
33       }
34       catch(Exception JavaDoc e) {
35          e.printStackTrace();
36       }
37    }
38
39    private static void help() {
40       System.out.println("RpcDelegatingCacheLoadeTest [client / server]");
41    }
42
43    private void start(boolean client) throws Exception JavaDoc {
44       cache=new TreeCache("test-cluster", null, 3000);
45       cache.setCacheMode(TreeCache.REPL_ASYNC);
46       cache.setFetchStateOnStartup(true);
47       cache.setCacheLoaderFetchTransientState(true);
48       cache.setCacheLoaderClass("org.jboss.cache.loader.RpcDelegatingCacheLoader");
49       cache.setCacheLoaderShared(false);
50       cache.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
51       cache.startService();
52
53       if(client == false) {
54          System.out.println("started as SERVER");
55          System.out.println("populating the cache");
56          cache.getTransactionManager().begin();
57          cache.put("/a/b/c", "bela", "ban");
58          cache.put("/1/2/3", "ben", "wang");
59          cache.getTransactionManager().commit();
60          System.out.println("the cache is " + cache.toString() + ", locks: " + cache.printLockInfo());
61
62          while(true){
63             sleep(2000);
64             cache.getTransactionManager().begin();
65             cache.put("/x/y/z", "d", "d");
66             cache.getTransactionManager().commit();
67          }
68       }
69
70       System.out.println("started as CLIENT");
71       System.out.println("initial cache (fetched from SERVER) is " + cache.printLockInfo());
72 // int i=1;
73
// while(true) {
74
// cache.put("/elements/" + i, "key", "value");
75
// sleep(1000);
76
// cache.evict(Fqn.fromString("/elements/" + i));
77
// i++;
78
// }
79

80       // sleep a bit, because we use async repl, changes from the server may not yet have arrived
81
sleep(1000);
82
83       System.out.println("evicting /a and /1 locally");
84       cache.evict(Fqn.fromString("/a/b/c"));
85       cache.evict(Fqn.fromString("/a/b"));
86       cache.evict(Fqn.fromString("/a"));
87       cache.evict(Fqn.fromString("/1/2/3"));
88       cache.evict(Fqn.fromString("/1/2"));
89       cache.evict(Fqn.fromString("/1"));
90       System.out.println("cache contents after eviction: " + cache.printLockInfo());
91
92       System.out.println("accessing /a and /1. this will trigger a fetch from the remote SERVER");
93       Object JavaDoc val=cache.get("/a/b/c", "bela");
94       System.out.println("value is " + val);
95
96       val=cache.get("/1/2/3", "ben");
97       System.out.println("value is " + val);
98
99       System.out.println("the cache is " + cache.toString() + ", locks: " + cache.printLockInfo());
100    }
101
102
103    private void sleep(long time) {
104       try {
105          Thread.sleep(time);
106       }
107       catch(InterruptedException JavaDoc e) {
108       }
109    }
110 }
111
Popular Tags