1 |
| package org.jboss.cache.interceptors; |
2 |
| |
3 |
| import org.jboss.cache.CacheSPI; |
4 |
| import org.jboss.cache.Fqn; |
5 |
| import org.jboss.cache.InvocationContext; |
6 |
| import org.jboss.cache.NodeSPI; |
7 |
| import org.jboss.cache.loader.CacheLoader; |
8 |
| import org.jboss.cache.marshall.MethodCall; |
9 |
| import org.jboss.cache.marshall.MethodDeclarations; |
10 |
| |
11 |
| import java.util.Collections; |
12 |
| import java.util.HashMap; |
13 |
| import java.util.Map; |
14 |
| import java.util.concurrent.atomic.AtomicLong; |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| public class PassivationInterceptor extends Interceptor implements PassivationInterceptorMBean |
24 |
| { |
25 |
| |
26 |
| protected CacheLoader loader = null; |
27 |
| private AtomicLong m_passivations = new AtomicLong(0); |
28 |
| |
29 |
488
| public synchronized void setCache(CacheSPI cache)
|
30 |
| { |
31 |
488
| super.setCache(cache);
|
32 |
488
| this.loader = cache.getCacheLoaderManager().getCacheLoader();
|
33 |
| } |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
319844
| public Object invoke(InvocationContext ctx) throws Throwable
|
44 |
| { |
45 |
319844
| MethodCall m = ctx.getMethodCall();
|
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
319844
| if (m.getMethodId() == MethodDeclarations.evictNodeMethodLocal_id)
|
51 |
| { |
52 |
1566
| Object[] args = m.getArgs();
|
53 |
1566
| Fqn fqn = (Fqn) args[0];
|
54 |
1566
| try
|
55 |
| { |
56 |
1566
| synchronized (this)
|
57 |
| { |
58 |
| |
59 |
| |
60 |
1566
| Map attributes = getNodeAttributes(fqn);
|
61 |
| |
62 |
1561
| cache.getNotifier().notifyNodePassivated(fqn, true, attributes, ctx);
|
63 |
| |
64 |
1561
| loader.put(fqn, attributes);
|
65 |
| |
66 |
1561
| cache.getNotifier().notifyNodePassivated(fqn, false, Collections.emptyMap(), ctx);
|
67 |
| } |
68 |
| |
69 |
1561
| if (getStatisticsEnabled() && configuration.getExposeManagementStatistics())
|
70 |
| { |
71 |
1561
| m_passivations.getAndIncrement();
|
72 |
| } |
73 |
| } |
74 |
| catch (NodeNotLoadedException e) |
75 |
| { |
76 |
5
| if (log.isTraceEnabled())
|
77 |
| { |
78 |
0
| log.trace("Node " + fqn + " not loaded in memory; passivation skipped");
|
79 |
| } |
80 |
| } |
81 |
| } |
82 |
| |
83 |
319844
| return super.invoke(ctx);
|
84 |
| } |
85 |
| |
86 |
13
| public long getPassivations()
|
87 |
| { |
88 |
13
| return m_passivations.get();
|
89 |
| } |
90 |
| |
91 |
1
| public void resetStatistics()
|
92 |
| { |
93 |
1
| m_passivations.set(0);
|
94 |
| } |
95 |
| |
96 |
0
| public Map<String, Object> dumpStatistics()
|
97 |
| { |
98 |
0
| Map<String, Object> retval = new HashMap<String, Object>();
|
99 |
0
| retval.put("Passivations", m_passivations.get());
|
100 |
0
| return retval;
|
101 |
| } |
102 |
| |
103 |
| |
104 |
| |
105 |
| |
106 |
1566
| private Map getNodeAttributes(Fqn fqn) throws NodeNotLoadedException
|
107 |
| { |
108 |
1566
| if (fqn == null)
|
109 |
| { |
110 |
0
| throw new NodeNotLoadedException();
|
111 |
| } |
112 |
1566
| NodeSPI n = cache.peek(fqn, true);
|
113 |
| |
114 |
1566
| if (n != null)
|
115 |
| { |
116 |
1561
| return n.getDataDirect();
|
117 |
| } |
118 |
| else |
119 |
| { |
120 |
5
| throw new NodeNotLoadedException();
|
121 |
| } |
122 |
| } |
123 |
| |
124 |
| private static class NodeNotLoadedException extends Exception |
125 |
| { |
126 |
| |
127 |
| |
128 |
| |
129 |
| private static final long serialVersionUID = -4078972305344328905L; |
130 |
| } |
131 |
| } |