/*
* Copyright 2012 Jason Miller
*
* 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 jj.execution;
import java.time.Clock;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;
import javax.inject.Inject;
import javax.inject.Singleton;
import jj.ServerStopping;
import jj.event.Listener;
import jj.event.Subscriber;
/**
* Special internal executor for the various tasks the server needs done.
*
* @author jason
*
*/
@Singleton
@Subscriber
class ServerExecutor extends DelayedExecutor {
@Inject
ServerExecutor(
Clock clock,
JJThreadFactory threadFactory,
JJRejectedExecutionHandler handler
) {
super(
clock,
2, // core threads, which we will always keep alive. effectively one since the first thing we do is start a scheduler
Integer.MAX_VALUE, // never run out of threads for server tasks until we exhaust the machine
20, TimeUnit.SECONDS, // don't keep threads around too long if they aren't kept busy.
new SynchronousQueue<Runnable>(), // hand off tasks immediately for execution
threadFactory.namePattern("JibbrJabbr Server Thread %d"),
handler
);
}
@Override
protected String schedulerThreadName() {
return Thread.currentThread().getName() + " - " + toString() + " Scheduler";
}
@Listener
public void on(ServerStopping event) {
shutdownNow();
}
@Override
public String toString() {
return getClass().getSimpleName();
}
/**
* @return
*/
boolean isServerThread() {
return threadFactory.in();
}
}