|
17 | 17 |
|
18 | 18 | package com.astamuse.asta4d.web.util.timeout; |
19 | 19 |
|
| 20 | +import java.io.Serializable; |
20 | 21 | import java.util.ArrayList; |
21 | 22 | import java.util.List; |
| 23 | +import java.util.Map; |
22 | 24 | import java.util.Map.Entry; |
23 | 25 | import java.util.concurrent.ConcurrentHashMap; |
24 | 26 | import java.util.concurrent.Executors; |
@@ -36,7 +38,7 @@ public class DefaultSessionAwareExpirableDataManager implements ExpirableDataMan |
36 | 38 |
|
37 | 39 | private static final String CheckSessionIdKey = DefaultSessionAwareExpirableDataManager.class + "#CheckSessionIdKey"; |
38 | 40 |
|
39 | | - private ConcurrentHashMap<String, DataHolder> dataMap = null; |
| 41 | + private Map<String, DataHolder> dataMap = null; |
40 | 42 |
|
41 | 43 | private AtomicInteger dataCounter = null; |
42 | 44 |
|
@@ -57,7 +59,8 @@ public class DefaultSessionAwareExpirableDataManager implements ExpirableDataMan |
57 | 59 | private String checkThreadName = this.getClass().getSimpleName() + "-check-thread"; |
58 | 60 |
|
59 | 61 | public DefaultSessionAwareExpirableDataManager() { |
60 | | - |
| 62 | + dataMap = createThreadSafeDataMap(); |
| 63 | + dataCounter = new AtomicInteger(); |
61 | 64 | } |
62 | 65 |
|
63 | 66 | public void setExpirationCheckPeriodInMilliseconds(long expirationCheckPeriodInMilliseconds) { |
@@ -80,11 +83,28 @@ public void setMaxSpinTimeInMilliseconds(long maxSpinTimeInMilliseconds) { |
80 | 83 | this.maxSpinTimeInMilliseconds = maxSpinTimeInMilliseconds; |
81 | 84 | } |
82 | 85 |
|
| 86 | + protected Map<String, DataHolder> createThreadSafeDataMap() { |
| 87 | + return new ConcurrentHashMap<>(); |
| 88 | + } |
| 89 | + |
| 90 | + protected void decreaseCount() { |
| 91 | + dataCounter.decrementAndGet(); |
| 92 | + } |
| 93 | + |
| 94 | + protected void addCount(int delta) { |
| 95 | + dataCounter.addAndGet(delta); |
| 96 | + } |
| 97 | + |
| 98 | + protected void increaseCount() { |
| 99 | + dataCounter.incrementAndGet(); |
| 100 | + } |
| 101 | + |
| 102 | + protected int getCurrentCount() { |
| 103 | + return dataCounter.get(); |
| 104 | + } |
| 105 | + |
83 | 106 | @Override |
84 | 107 | public void start() { |
85 | | - // init fields |
86 | | - dataMap = new ConcurrentHashMap<>(); |
87 | | - dataCounter = new AtomicInteger(); |
88 | 108 | service = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() { |
89 | 109 | @Override |
90 | 110 | public Thread newThread(Runnable r) { |
@@ -109,7 +129,7 @@ public void run() { |
109 | 129 | } |
110 | 130 | } |
111 | 131 | if (removedCounter > 0) { |
112 | | - dataCounter.addAndGet(-removedCounter); |
| 132 | + addCount(-removedCounter); |
113 | 133 | } |
114 | 134 | } |
115 | 135 | }, expirationCheckPeriodInMilliseconds, expirationCheckPeriodInMilliseconds, TimeUnit.MILLISECONDS); |
@@ -129,7 +149,7 @@ public <T> T get(String dataId, boolean remove) { |
129 | 149 | if (remove) { |
130 | 150 | holder = dataMap.remove(dataId); |
131 | 151 | if (holder != null) { |
132 | | - dataCounter.decrementAndGet(); |
| 152 | + decreaseCount(); |
133 | 153 | if (holder.isExpired(System.currentTimeMillis())) { |
134 | 154 | holder = null; |
135 | 155 | } |
@@ -140,7 +160,7 @@ public <T> T get(String dataId, boolean remove) { |
140 | 160 | if (holder.isExpired(System.currentTimeMillis())) { |
141 | 161 | holder = dataMap.remove(dataId); |
142 | 162 | if (holder != null) { |
143 | | - dataCounter.decrementAndGet(); |
| 163 | + decreaseCount(); |
144 | 164 | holder = null; |
145 | 165 | } |
146 | 166 | } |
@@ -158,13 +178,20 @@ public <T> T get(String dataId, boolean remove) { |
158 | 178 | } |
159 | 179 |
|
160 | 180 | public void put(String dataId, Object data, long expireMilliSeconds) { |
161 | | - if (dataCounter.get() >= maxDataSize) { |
| 181 | + Object existing = dataMap.put(dataId, new DataHolder(data, expireMilliSeconds, retrieveSessionId(true))); |
| 182 | + if (existing == null) { |
| 183 | + increaseCount(); |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + protected void checkSize() { |
| 188 | + if (getCurrentCount() >= maxDataSize) { |
162 | 189 | try { |
163 | 190 | long spinTimeTotal = 0; |
164 | | - while (dataCounter.get() >= maxDataSize) { |
| 191 | + while (getCurrentCount() >= maxDataSize) { |
165 | 192 | if (spinTimeTotal >= maxSpinTimeInMilliseconds) { |
166 | | - String msg = "There are too many data in %s and we could not get empty space after waiting for %d milliseconds." |
167 | | - + " The configured max size is %d and perhaps you should increase the value."; |
| 193 | + String msg = "There are too many data in %s and we could not get empty space after waiting for %d milliseconds." + |
| 194 | + " The configured max size is %d and perhaps you should increase the value."; |
168 | 195 | msg = String.format(msg, this.getClass().getName(), spinTimeTotal, maxDataSize); |
169 | 196 | throw new TooManyDataException(msg); |
170 | 197 | } |
@@ -175,10 +202,6 @@ public void put(String dataId, Object data, long expireMilliSeconds) { |
175 | 202 | throw new RuntimeException(e); |
176 | 203 | } |
177 | 204 | } |
178 | | - Object existing = dataMap.put(dataId, new DataHolder(data, expireMilliSeconds, retrieveSessionId(true))); |
179 | | - if (existing == null) { |
180 | | - dataCounter.incrementAndGet(); |
181 | | - } |
182 | 205 | } |
183 | 206 |
|
184 | 207 | protected String retrieveSessionId(boolean create) { |
@@ -200,7 +223,11 @@ protected void finalize() throws Throwable { |
200 | 223 | super.finalize(); |
201 | 224 | } |
202 | 225 |
|
203 | | - private static class DataHolder { |
| 226 | + protected static class DataHolder implements Serializable { |
| 227 | + /** |
| 228 | + * |
| 229 | + */ |
| 230 | + private static final long serialVersionUID = 1L; |
204 | 231 | private final Object data; |
205 | 232 | private final long creationTime; |
206 | 233 | private final long expireMilliSeconds; |
|