Subversion Repositories XServices

Rev

Rev 185 | Details | Compare with Previous | 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
 
185 brianR 31
public class CacheExecutorService implements ServletContextListener {
32
	static final String EXECUTOR_NAME = "CACHE_EXECUTOR";
33
	private ExecutorService executor;
102 brianR 34
 
185 brianR 35
	public void contextInitialized(ServletContextEvent arg0) {
36
		ServletContext context = arg0.getServletContext();
37
		int nr_executors = 5;
38
		ThreadFactory daemonFactory = new DaemonThreadFactory();
39
		try {
40
			nr_executors = Integer.parseInt(context.getInitParameter("cache:thread-count"));
41
		} catch (NumberFormatException localNumberFormatException) {
42
		}
43
		if (nr_executors <= 1)
44
			this.executor = Executors.newSingleThreadExecutor(daemonFactory);
45
		else {
46
			this.executor = Executors.newFixedThreadPool(nr_executors, daemonFactory);
47
		}
48
		context.setAttribute("CACHE_EXECUTOR", this.executor);
49
	}
102 brianR 50
 
185 brianR 51
	public void contextDestroyed(ServletContextEvent arg0) {
52
		ServletContext context = arg0.getServletContext();
53
		this.executor.shutdownNow();
54
	}
102 brianR 55
}