KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > TstTimedCache


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.test;
8
9 import java.io.FilePermission JavaDoc;
10 import java.net.URL JavaDoc;
11 import java.security.CodeSource JavaDoc;
12 import java.security.Permission JavaDoc;
13 import java.security.PermissionCollection JavaDoc;
14 import java.security.Policy JavaDoc;
15
16 import org.jboss.util.TimedCachePolicy;
17
18 /** Tests of the TimedCachePolicy class.
19
20 @see org.jboss.util.TimedCachePolicy
21
22 @author Scott.Stark@jboss.org
23 @version $Revision: 1.6.6.1 $
24 */

25 public class TstTimedCache
26 {
27     static class Refreshable implements TimedCachePolicy.TimedEntry
28     {
29         int refreshes;
30         long expirationTime;
31         Object JavaDoc value;
32         Refreshable(long lifetime, Object JavaDoc value, int refreshes)
33         {
34             this.expirationTime = 1000 * lifetime;
35             this.value = value;
36             this.refreshes = refreshes;
37         }
38         public void init(long now)
39         {
40             expirationTime += now;
41             System.out.println(value+".init("+now+"), expirationTime="+expirationTime);
42         }
43         public boolean isCurrent(long now)
44         {
45             System.out.println(value+".isCurrent("+now+") = "+(expirationTime > now));
46             return expirationTime > now;
47         }
48         public boolean refresh()
49         {
50             refreshes --;
51             System.out.println(value+".refresh() = "+(refreshes > 0));
52             return refreshes > 0;
53         }
54         public void destroy()
55         {
56             System.out.println(value+".destroy()");
57         }
58         public Object JavaDoc getValue()
59         {
60             return value;
61         }
62     }
63
64     /**
65     * @param args the command line arguments
66     */

67     public static void main(String JavaDoc args[])
68     {
69         TimedCachePolicy cache = new TimedCachePolicy(20, false, 1);
70         cache.create();
71         cache.start();
72         cache.insert("1", new Refreshable(5, "value1", 4));
73         cache.insert("2", new Refreshable(3, "value2", 10));
74         cache.insert("3", "value3");
75         long start = System.currentTimeMillis();
76         // Loop until the longest lived value is gone
77
while( cache.peek("2") != null )
78         {
79             long now = System.currentTimeMillis();
80             System.out.println("Elapsed: "+(now - start) / 1000);
81             System.out.println("get(1) -> "+cache.get("1"));
82             System.out.println("get(2) -> "+cache.get("2"));
83             System.out.println("get(3) -> "+cache.get("3"));
84             try
85             {
86                 Thread.currentThread().sleep(3*1000);
87             }
88             catch(InterruptedException JavaDoc e)
89             {
90             }
91         }
92         long now = System.currentTimeMillis();
93         System.out.println("End, elapsed: "+(now - start) / 1000);
94         System.out.println("get(1) -> "+cache.get("1"));
95         System.out.println("get(2) -> "+cache.get("2"));
96         System.out.println("get(3) -> "+cache.get("3"));
97     }
98
99 }
100
Popular Tags