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