KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 * JBoss, the OpenSource J2EE webOS
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */

7 package org.jboss.cache.tests;
8
9 import org.jboss.cache.TreeCache;
10 import org.jboss.cache.Fqn;
11
12 /**
13  * @author Bela Ban
14  * @version $Id: RpcDelegatingCacheLoaderTests.java,v 1.3.2.2 2005/04/06 21:07:04 starksm Exp $
15  */

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

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