1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| package org.jboss.cache.passivation; |
8 |
| |
9 |
| import junit.framework.TestCase; |
10 |
| import org.apache.commons.logging.Log; |
11 |
| import org.apache.commons.logging.LogFactory; |
12 |
| import org.jboss.cache.CacheException; |
13 |
| import org.jboss.cache.CacheImpl; |
14 |
| import org.jboss.cache.CacheSPI; |
15 |
| import org.jboss.cache.DefaultCacheFactory; |
16 |
| import org.jboss.cache.Fqn; |
17 |
| import org.jboss.cache.RegionManager; |
18 |
| import org.jboss.cache.config.CacheLoaderConfig; |
19 |
| import org.jboss.cache.config.EvictionConfig; |
20 |
| import org.jboss.cache.config.EvictionRegionConfig; |
21 |
| import org.jboss.cache.eviction.LRUConfiguration; |
22 |
| import org.jboss.cache.loader.CacheLoader; |
23 |
| import org.jboss.cache.loader.DummyInMemoryCacheLoader; |
24 |
| import org.jboss.cache.lock.IsolationLevel; |
25 |
| import org.jboss.cache.misc.TestingUtil; |
26 |
| import org.jboss.cache.notifications.annotation.NodeActivated; |
27 |
| import org.jboss.cache.notifications.annotation.NodePassivated; |
28 |
| import org.jboss.cache.notifications.event.NodeEvent; |
29 |
| |
30 |
| import java.util.ArrayList; |
31 |
| import java.util.HashSet; |
32 |
| import java.util.List; |
33 |
| import java.util.Set; |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| public class PassivationActivationCallbacksTestCase extends TestCase |
41 |
| { |
42 |
| private static final Fqn BASE = Fqn.fromString("/base"); |
43 |
| private static final Log log = LogFactory.getLog(PassivationActivationCallbacksTestCase.class); |
44 |
| |
45 |
| |
46 |
| private CacheSPI cache; |
47 |
| private CacheLoader loader = null; |
48 |
| private CacheListener listener = null; |
49 |
| |
50 |
0
| protected void setUp() throws Exception
|
51 |
| { |
52 |
0
| super.setUp();
|
53 |
0
| log.debug("Testing " + getName());
|
54 |
0
| log.debug("");
|
55 |
0
| cache = (CacheImpl) DefaultCacheFactory.getInstance().createCache(false);
|
56 |
0
| cache.getConfiguration().setCacheMode("local");
|
57 |
0
| configureEviction();
|
58 |
0
| configureCacheLoader();
|
59 |
0
| cache.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.transaction.DummyTransactionManagerLookup");
|
60 |
0
| cache.getConfiguration().setIsolationLevel(IsolationLevel.SERIALIZABLE);
|
61 |
0
| listener = new CacheListener();
|
62 |
0
| cache.addCacheListener(listener);
|
63 |
0
| cache.create();
|
64 |
0
| cache.start();
|
65 |
0
| loader = cache.getCacheLoaderManager().getCacheLoader();
|
66 |
| } |
67 |
| |
68 |
0
| protected void configureCacheLoader() throws Exception
|
69 |
| { |
70 |
0
| CacheLoaderConfig clc = new CacheLoaderConfig();
|
71 |
0
| clc.setPassivation(true);
|
72 |
0
| clc.setShared(false);
|
73 |
0
| clc.setPreload("/");
|
74 |
| |
75 |
0
| CacheLoaderConfig.IndividualCacheLoaderConfig dummyConfig = new CacheLoaderConfig.IndividualCacheLoaderConfig();
|
76 |
0
| dummyConfig.setAsync(false);
|
77 |
0
| dummyConfig.setFetchPersistentState(true);
|
78 |
0
| dummyConfig.setIgnoreModifications(false);
|
79 |
0
| dummyConfig.setClassName(DummyInMemoryCacheLoader.class.getName());
|
80 |
0
| clc.addIndividualCacheLoaderConfig(dummyConfig);
|
81 |
0
| cache.getConfiguration().setCacheLoaderConfig(clc);
|
82 |
| } |
83 |
| |
84 |
0
| protected void configureEviction() throws Exception
|
85 |
| { |
86 |
0
| EvictionConfig ec = new EvictionConfig();
|
87 |
0
| ec.setWakeupIntervalSeconds(1);
|
88 |
| |
89 |
0
| List<EvictionRegionConfig> ercs = new ArrayList<EvictionRegionConfig>();
|
90 |
| |
91 |
0
| EvictionRegionConfig erc = new EvictionRegionConfig();
|
92 |
0
| erc.setRegionFqn(RegionManager.DEFAULT_REGION);
|
93 |
0
| LRUConfiguration epc = new LRUConfiguration();
|
94 |
0
| epc.setMaxNodes(0);
|
95 |
0
| epc.setTimeToLiveSeconds(5);
|
96 |
0
| erc.setEvictionPolicyConfig(epc);
|
97 |
0
| ercs.add(erc);
|
98 |
| |
99 |
0
| erc = new EvictionRegionConfig();
|
100 |
0
| erc.setRegionFqn(BASE);
|
101 |
0
| epc = new LRUConfiguration();
|
102 |
0
| epc.setMaxNodes(0);
|
103 |
0
| epc.setTimeToLiveSeconds(1);
|
104 |
0
| erc.setEvictionPolicyConfig(epc);
|
105 |
0
| ercs.add(erc);
|
106 |
| |
107 |
0
| ec.setEvictionRegionConfigs(ercs);
|
108 |
| |
109 |
0
| cache.getConfiguration().setEvictionConfig(ec);
|
110 |
| } |
111 |
| |
112 |
0
| protected void tearDown() throws Exception
|
113 |
| { |
114 |
0
| super.tearDown();
|
115 |
0
| cache.removeNode(Fqn.ROOT);
|
116 |
0
| loader.remove(Fqn.fromString("/"));
|
117 |
0
| cache.stop();
|
118 |
0
| cache.destroy();
|
119 |
| } |
120 |
| |
121 |
0
| public void testSimpleLifecycle() throws Exception
|
122 |
| { |
123 |
0
| Fqn fqn = new Fqn(BASE, "bean1");
|
124 |
0
| cache.put(fqn, "bean", "A bean");
|
125 |
| |
126 |
0
| TestingUtil.sleepThread(3000);
|
127 |
| |
128 |
0
| assertNull("No activation exception", listener.activationException);
|
129 |
0
| assertNull("No passivation exception", listener.passivationException);
|
130 |
0
| assertTrue(listener.passivated.contains(fqn));
|
131 |
0
| assertFalse(listener.activated.contains(fqn));
|
132 |
| |
133 |
0
| Object obj = cache.get(fqn, "bean");
|
134 |
0
| assertEquals("Got bean", "A bean", obj);
|
135 |
| |
136 |
0
| assertNull("No activation exception", listener.activationException);
|
137 |
0
| assertNull("No passivation exception", listener.passivationException);
|
138 |
0
| assertTrue(listener.activated.contains(fqn));
|
139 |
| } |
140 |
| |
141 |
| |
142 |
| |
143 |
| |
144 |
| @org.jboss.cache.notifications.annotation.CacheListener |
145 |
| public class CacheListener |
146 |
| { |
147 |
| protected Log log = LogFactory.getLog(CacheListener.class); |
148 |
| |
149 |
| protected Set passivated = new HashSet(); |
150 |
| protected Set activated = new HashSet(); |
151 |
| protected Exception passivationException; |
152 |
| protected Exception activationException; |
153 |
| |
154 |
0
| @NodeActivated
|
155 |
| public void nodeActivated(NodeEvent e) |
156 |
| { |
157 |
0
| if (e.isPre())
|
158 |
0
| return;
|
159 |
| |
160 |
0
| if (!e.getFqn().isChildOrEquals(BASE)) return;
|
161 |
| |
162 |
0
| Object bean = null;
|
163 |
0
| try
|
164 |
| { |
165 |
0
| bean = cache.get(e.getFqn(), "bean");
|
166 |
| } |
167 |
| catch (CacheException ex) |
168 |
| { |
169 |
0
| log.error("nodeActivate(): can't retrieve bean instance from: " + e.getFqn() + " with exception: " + ex);
|
170 |
0
| activationException = ex;
|
171 |
0
| return;
|
172 |
| } |
173 |
0
| if (bean == null)
|
174 |
| { |
175 |
0
| activationException = new IllegalStateException("nodeActivate(): null bean instance.");
|
176 |
0
| throw (IllegalStateException) activationException;
|
177 |
| } |
178 |
| |
179 |
0
| if (log.isTraceEnabled())
|
180 |
| { |
181 |
0
| log.trace("nodeActivate(): saw postActivate event on fqn: " + e.getFqn());
|
182 |
| } |
183 |
| |
184 |
0
| activated.add(e.getFqn());
|
185 |
| } |
186 |
| |
187 |
0
| @NodePassivated
|
188 |
| public void nodePassivated(NodeEvent e) |
189 |
| { |
190 |
0
| if (!e.isPre()) return;
|
191 |
0
| Fqn fqn = e.getFqn();
|
192 |
0
| if (!fqn.isChildOrEquals(BASE)) return;
|
193 |
| |
194 |
0
| try
|
195 |
| { |
196 |
| |
197 |
0
| Object bean = cache.get(fqn, "bean");
|
198 |
0
| if (bean != null)
|
199 |
| { |
200 |
0
| if (log.isTraceEnabled())
|
201 |
| { |
202 |
0
| log.trace("nodePassivate(): send prePassivate event on fqn: " + fqn);
|
203 |
| } |
204 |
0
| passivated.add(fqn);
|
205 |
| } |
206 |
| |
207 |
| } |
208 |
| catch (CacheException ex) |
209 |
| { |
210 |
0
| log.error("nodePassivate(): can't retrieve bean instance from: " + fqn + " with exception: " + ex);
|
211 |
0
| passivationException = ex;
|
212 |
0
| return;
|
213 |
| } |
214 |
| |
215 |
| } |
216 |
| } |
217 |
| |
218 |
| } |