1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| package org.jboss.cache.eviction; |
9 |
| |
10 |
| import org.apache.commons.logging.Log; |
11 |
| import org.apache.commons.logging.LogFactory; |
12 |
| import org.jboss.cache.Region; |
13 |
| |
14 |
| import java.util.Collections; |
15 |
| import java.util.HashSet; |
16 |
| import java.util.Set; |
17 |
| import java.util.Timer; |
18 |
| import java.util.TimerTask; |
19 |
| import java.util.concurrent.atomic.AtomicInteger; |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| public class EvictionTimerTask |
29 |
| { |
30 |
| private Log log = LogFactory.getLog(EvictionTimerTask.class); |
31 |
| |
32 |
| private final Set<Region> processedRegions; |
33 |
| private static AtomicInteger tcount = new AtomicInteger(); |
34 |
| private int wakeupIntervalSeconds; |
35 |
| private Timer evictionThread; |
36 |
| |
37 |
2969
| public EvictionTimerTask()
|
38 |
| { |
39 |
| |
40 |
| |
41 |
2969
| processedRegions = Collections.synchronizedSet(new HashSet<Region>());
|
42 |
| } |
43 |
| |
44 |
377
| public void init(int wakeupIntervalSeconds)
|
45 |
| { |
46 |
377
| if (log.isTraceEnabled())
|
47 |
0
| log.trace("Creating a new eviction listener with wakeupIntervalSeconds set at " + wakeupIntervalSeconds);
|
48 |
377
| this.wakeupIntervalSeconds = wakeupIntervalSeconds;
|
49 |
377
| start();
|
50 |
| } |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
| |
57 |
1230
| public void addRegionToProcess(Region region)
|
58 |
| { |
59 |
1230
| processedRegions.add(region);
|
60 |
| } |
61 |
| |
62 |
| |
63 |
| |
64 |
| |
65 |
| |
66 |
| |
67 |
0
| public void removeRegionToProcess(Region region)
|
68 |
| { |
69 |
0
| processedRegions.remove(region);
|
70 |
| } |
71 |
| |
72 |
6
| public boolean isRegionRegisteredForProcessing(Region region)
|
73 |
| { |
74 |
6
| return processedRegions.contains(region);
|
75 |
| } |
76 |
| |
77 |
358
| public void stop()
|
78 |
| { |
79 |
360
| log.debug("Stopping eviction timer");
|
80 |
| |
81 |
368
| if (evictionThread != null)
|
82 |
| { |
83 |
360
| evictionThread.cancel();
|
84 |
| } |
85 |
360
| evictionThread = null;
|
86 |
| } |
87 |
| |
88 |
377
| private void start()
|
89 |
| { |
90 |
377
| evictionThread = new Timer("EvictionTimer-" + tcount.getAndIncrement(), true);
|
91 |
377
| TimerTask tt = new TimerTask()
|
92 |
| { |
93 |
| |
94 |
| |
95 |
| |
96 |
| |
97 |
| |
98 |
| |
99 |
336
| public void run()
|
100 |
| { |
101 |
336
| synchronized (processedRegions)
|
102 |
| { |
103 |
336
| for (Region region : processedRegions)
|
104 |
| { |
105 |
925
| final EvictionPolicy policy = region.getEvictionPolicy();
|
106 |
| |
107 |
925
| synchronized (region)
|
108 |
| { |
109 |
925
| final EvictionAlgorithm algo = policy.getEvictionAlgorithm();
|
110 |
925
| if (algo == null)
|
111 |
0
| throw new NullPointerException("algorithm null");
|
112 |
925
| try
|
113 |
| { |
114 |
925
| algo.process(region);
|
115 |
| } |
116 |
| catch (EvictionException e) |
117 |
| { |
118 |
0
| log.error("run(): error processing eviction with exception: " + e.toString()
|
119 |
| + " will reset the eviction queue list."); |
120 |
0
| region.resetEvictionQueues();
|
121 |
0
| log.debug("trace", e);
|
122 |
| } |
123 |
| } |
124 |
| } |
125 |
| } |
126 |
| } |
127 |
| }; |
128 |
377
| evictionThread.schedule(tt, wakeupIntervalSeconds * 1000, wakeupIntervalSeconds * 1000);
|
129 |
| } |
130 |
| } |
131 |
| |
132 |
| |