Subversion Repositories XServices

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
102 brianR 1
/*
2
 *   Copyright 2013 Brian Rosenberger (Brutex Network)
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.brutex.xservices.util.cache;
18
 
19
import java.util.concurrent.ExecutorService;
20
import java.util.concurrent.Executors;
21
import java.util.concurrent.ThreadFactory;
22
import javax.servlet.ServletContext;
23
import javax.servlet.ServletContextEvent;
24
import javax.servlet.ServletContextListener;
25
 
26
/**
27
 * @author Brian Rosenberger, bru(at)brutex.de
28
 *
29
 */
30
 
31
 
32
 
33
public class CacheExecutorService
34
  implements ServletContextListener
35
{
36
  static final String EXECUTOR_NAME = "CACHE_EXECUTOR";
37
  private ExecutorService executor;
38
 
39
  public void contextInitialized(ServletContextEvent arg0)
40
  {
41
    ServletContext context = arg0.getServletContext();
42
    int nr_executors = 5;
43
    ThreadFactory daemonFactory = new DaemonThreadFactory();
44
    try {
45
      nr_executors = Integer.parseInt(context.getInitParameter("cache:thread-count"));
46
    } catch (NumberFormatException localNumberFormatException) {
47
    }
48
    if (nr_executors <= 1)
49
      this.executor = Executors.newSingleThreadExecutor(daemonFactory);
50
    else {
51
      this.executor = Executors.newFixedThreadPool(nr_executors, daemonFactory);
52
    }
53
    context.setAttribute("CACHE_EXECUTOR", this.executor);
54
  }
55
 
56
  public void contextDestroyed(ServletContextEvent arg0) {
57
    ServletContext context = arg0.getServletContext();
58
    this.executor.shutdownNow();
59
  }
60
}