1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.mule.util.store; |
11 | |
|
12 | |
import org.mule.api.MuleContext; |
13 | |
import org.mule.api.store.ObjectStore; |
14 | |
import org.mule.api.context.MuleContextAware; |
15 | |
import org.mule.api.lifecycle.Initialisable; |
16 | |
import org.mule.api.lifecycle.InitialisationException; |
17 | |
import org.mule.api.lifecycle.Disposable; |
18 | |
import org.mule.config.i18n.CoreMessages; |
19 | |
import org.mule.util.concurrent.DaemonThreadFactory; |
20 | |
import org.mule.util.UUID; |
21 | |
|
22 | |
import org.apache.commons.logging.Log; |
23 | |
import org.apache.commons.logging.LogFactory; |
24 | |
import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit; |
25 | |
import edu.emory.mathcs.backport.java.util.concurrent.ScheduledThreadPoolExecutor; |
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | 18 | public abstract class AbstractMonitoredObjectStore implements ObjectStore, Runnable, MuleContextAware, Initialisable, Disposable |
31 | |
{ |
32 | 18 | protected final Log logger = LogFactory.getLog(this.getClass()); |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
protected ScheduledThreadPoolExecutor scheduler; |
51 | 18 | protected int maxEntries = 4000; |
52 | 18 | protected int entryTTL = -1; |
53 | 18 | protected int expirationInterval = 1000; |
54 | |
protected String name; |
55 | |
protected MuleContext context; |
56 | |
|
57 | |
|
58 | |
public void initialise() throws InitialisationException |
59 | |
{ |
60 | 18 | if (name == null) |
61 | |
{ |
62 | 0 | name = UUID.getUUID(); |
63 | |
|
64 | |
} |
65 | |
|
66 | 18 | if (expirationInterval <= 0) |
67 | |
{ |
68 | 0 | throw new IllegalArgumentException(CoreMessages.propertyHasInvalidValue("expirationInterval", |
69 | |
new Integer(expirationInterval)).toString()); |
70 | |
} |
71 | |
|
72 | 18 | if (scheduler == null) |
73 | |
{ |
74 | 18 | this.scheduler = new ScheduledThreadPoolExecutor(1); |
75 | 18 | scheduler.setThreadFactory(new DaemonThreadFactory(name + "-Monitor")); |
76 | 18 | scheduler.scheduleWithFixedDelay(this, 0, expirationInterval, TimeUnit.MILLISECONDS); |
77 | |
} |
78 | 18 | } |
79 | |
|
80 | |
public final void run() |
81 | |
{ |
82 | 89 | expire(); |
83 | 89 | } |
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
public void dispose() |
91 | |
{ |
92 | 12 | if(scheduler!=null) |
93 | |
{ |
94 | 12 | scheduler.shutdown(); |
95 | |
} |
96 | 12 | } |
97 | |
|
98 | |
public void setEntryTTL(int entryTTL) |
99 | |
{ |
100 | 18 | this.entryTTL = entryTTL; |
101 | 18 | } |
102 | |
|
103 | |
public void setExpirationInterval(int expirationInterval) |
104 | |
{ |
105 | 18 | this.expirationInterval = expirationInterval; |
106 | 18 | } |
107 | |
|
108 | |
public void setMaxEntries(int maxEntries) |
109 | |
{ |
110 | 18 | this.maxEntries = maxEntries; |
111 | 18 | } |
112 | |
|
113 | |
public void setScheduler(ScheduledThreadPoolExecutor scheduler) |
114 | |
{ |
115 | 0 | this.scheduler = scheduler; |
116 | 0 | } |
117 | |
|
118 | |
public void setName(String id) |
119 | |
{ |
120 | 18 | this.name = id; |
121 | 18 | } |
122 | |
|
123 | |
public void setMuleContext(MuleContext context) |
124 | |
{ |
125 | 12 | this.context = context; |
126 | 12 | } |
127 | |
|
128 | |
public int getEntryTTL() |
129 | |
{ |
130 | 0 | return entryTTL; |
131 | |
} |
132 | |
|
133 | |
public int getExpirationInterval() |
134 | |
{ |
135 | 0 | return expirationInterval; |
136 | |
} |
137 | |
|
138 | |
public int getMaxEntries() |
139 | |
{ |
140 | 0 | return maxEntries; |
141 | |
} |
142 | |
|
143 | |
public String getName() |
144 | |
{ |
145 | 0 | return name; |
146 | |
} |
147 | |
|
148 | |
public ScheduledThreadPoolExecutor getScheduler() |
149 | |
{ |
150 | 0 | return scheduler; |
151 | |
} |
152 | |
|
153 | |
protected abstract void expire(); |
154 | |
|
155 | |
} |