Subversion Repositories XServices

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
94 brianR 1
/*
2
 *   Copyright 2012 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
 
23
import javax.servlet.ServletContext;
24
import javax.servlet.ServletContextEvent;
25
import javax.servlet.ServletContextListener;
26
 
27
/**
28
 * @author Brian Rosenberger, bru(at)brutex.de
29
 * @since 0.5.0-20120825
30
 */
31
public class CacheExecutorService implements ServletContextListener {
32
	static final String EXECUTOR_NAME = "CACHE_EXECUTOR";
33
    private  ExecutorService executor;
34
 
35
    public void contextInitialized(ServletContextEvent arg0) {
36
        ServletContext context = arg0.getServletContext();
37
        int nr_executors = 1;
38
        ThreadFactory daemonFactory = new DaemonThreadFactory();
39
        try {
40
            nr_executors = Integer.parseInt(context.getInitParameter("cache:thread-count"));
41
        } catch (NumberFormatException ignore ) {}
42
 
43
        if(nr_executors <= 1) {
44
        executor = Executors.newSingleThreadExecutor(daemonFactory);
45
        } else {
46
        executor = Executors.newFixedThreadPool(nr_executors,daemonFactory);
47
       }
48
          context.setAttribute(EXECUTOR_NAME, executor);
49
      }
50
 
51
    public void contextDestroyed(ServletContextEvent arg0) {
52
        ServletContext context = arg0.getServletContext();
53
        executor.shutdownNow(); // or process/wait until all pending jobs are done
54
    }
55
 
56
}