/******************************************************************************
* WebJavin - Java Web Framework. *
* *
* Copyright (c) 2011 - Sergey "Frosman" Lukjanov, me@frostman.ru *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
******************************************************************************/
package ru.frostman.web.util;
import ru.frostman.web.config.JavinConfig;
import ru.frostman.web.config.PoolConfig;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.*;
/**
* @author slukjanov aka Frostman
*/
public class Invoker {
private final ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(
JavinConfig.get().getApp().getPool().getCorePoolSize(),
new JavinThreadFactory("javin-executor")
);
//todo we should shutdown executor in servlet context listener
public Invoker() {
PoolConfig poolConfig = JavinConfig.get().getApp().getPool();
executor.setKeepAliveTime(poolConfig.getKeepAliveTime(), poolConfig.getTimeUnit());
executor.setMaximumPoolSize(poolConfig.getMaximumPoolSize());
executor.setRejectedExecutionHandler(new RejectedExecutionHandler() {
@Override
public void rejectedExecution(Runnable runnable, ThreadPoolExecutor executor) {
}
});
}
public ScheduledFuture<?> submit(Runnable task) {
return schedule(task, 0, TimeUnit.NANOSECONDS);
}
public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) {
return executor.schedule(command, delay, unit);
}
public <T> ScheduledFuture<T> submit(Callable<T> task) {
return schedule(task, 0, TimeUnit.NANOSECONDS);
}
public <T> Future<T> submit(Runnable task, T result) {
return schedule(Executors.callable(task, result), 0, TimeUnit.NANOSECONDS);
}
public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) {
return executor.schedule(callable, delay, unit);
}
public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException {
return executor.invokeAny(tasks);
}
public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException {
return executor.invokeAny(tasks, timeout, unit);
}
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException {
return executor.invokeAll(tasks);
}
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
throws InterruptedException {
return executor.invokeAll(tasks, timeout, unit);
}
public List<Runnable> shutdownNow() {
return executor.shutdownNow();
}
public void shutdown() {
executor.shutdown();
}
public boolean remove(Runnable task) {
return executor.remove(task);
}
public int getQueueSize() {
return executor.getQueue().size();
}
}