View Javadoc

1   /***
2    *  Copyright 2003-2010 Terracotta, Inc.
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
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  /***
27   * {@link NonstopExecutorServiceFactory} that creates and maintains one per CacheManager
28   *
29   * @author Abhishek Sanoujam
30   *
31   */
32  public final class CacheManagerExecutorServiceFactory implements NonstopExecutorServiceFactory {
33  
34      /***
35       * A string that is a part of the thread name created by the default thread factory.
36       */
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      /***
44       * private constructor
45       */
46      private CacheManagerExecutorServiceFactory() {
47          //
48      }
49  
50      /***
51       * Returns the singleton instance
52       *
53       * @return the singleton instance
54       */
55      public static CacheManagerExecutorServiceFactory getInstance() {
56          return SINGLETON;
57      }
58  
59      /***
60       * {@inheritDoc}
61       */
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 Thread(runnable, "NonStopCache [" + cacheManagerName + "] " + EXECUTOR_THREAD_NAME_PREFIX + "-"
72                                  + count.incrementAndGet() + " for '" + Thread.currentThread().getName() + "'");
73                          thread.setDaemon(true);
74                          return thread;
75                      }
76                  });
77                  executorServiceMap.put(cacheManagerName, rv);
78              }
79              return rv;
80          }
81      }
82  
83      /***
84       * {@inheritDoc}
85       */
86      public void shutdown(final CacheManager cacheManager) {
87          synchronized (executorServiceMap) {
88              NonstopExecutorService executorService = executorServiceMap.remove(cacheManager.getName());
89              if (executorService != null) {
90                  executorService.shutdown();
91              }
92          }
93  
94      }
95  
96  }