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