1
16
17 package net.sf.ehcache.constructs.nonstop;
18
19 import java.util.HashMap;
20 import java.util.Map;
21 import java.util.concurrent.ThreadFactory;
22 import java.util.concurrent.atomic.AtomicInteger;
23
24 import net.sf.ehcache.CacheManager;
25
26
32 public final class CacheManagerExecutorServiceFactory implements NonstopExecutorServiceFactory {
33
34
37 private static final String EXECUTOR_THREAD_NAME_PREFIX = "Executor Thread";
38
39 private static final CacheManagerExecutorServiceFactory SINGLETON = new CacheManagerExecutorServiceFactory();
40
41 private final Map<String, NonstopExecutorService> executorServiceMap = new HashMap<String, NonstopExecutorService>();
42
43
46 private CacheManagerExecutorServiceFactory() {
47
48 }
49
50
55 public static CacheManagerExecutorServiceFactory getInstance() {
56 return SINGLETON;
57 }
58
59
62 public NonstopExecutorService getOrCreateNonstopExecutorService(final CacheManager cacheManager) {
63 final String cacheManagerName = cacheManager.getName();
64 synchronized (executorServiceMap) {
65 NonstopExecutorService rv = executorServiceMap.get(cacheManagerName);
66 if (rv == null) {
67 rv = new NonstopExecutorServiceImpl(new ThreadFactory() {
68 private final AtomicInteger count = new AtomicInteger();
69
70 public Thread newThread(Runnable runnable) {
71 Thread thread = new NonstopThread(runnable, "NonStopCache [" + cacheManagerName + "] "
72 + EXECUTOR_THREAD_NAME_PREFIX + "-" + count.incrementAndGet() + " for '" + Thread.currentThread().getName()
73 + "'");
74 thread.setDaemon(true);
75 return thread;
76 }
77 });
78 executorServiceMap.put(cacheManagerName, rv);
79 }
80 return rv;
81 }
82 }
83
84
87 public void shutdown(final CacheManager cacheManager) {
88 synchronized (executorServiceMap) {
89 NonstopExecutorService executorService = executorServiceMap.remove(cacheManager.getName());
90 if (executorService != null) {
91 executorService.shutdown();
92 }
93 }
94
95 }
96
97 }
98