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