com.qwazr.scheduler.SchedulerServer Maven / Gradle / Ivy
Show all versions of qwazr-scheduler Show documentation
/*
* Copyright 2015-2018 Emmanuel Keller / QWAZR
*
* 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 com.qwazr.scheduler;
import com.qwazr.cluster.ClusterManager;
import com.qwazr.cluster.ClusterServiceInterface;
import com.qwazr.library.LibraryManager;
import com.qwazr.library.LibraryServiceInterface;
import com.qwazr.scripts.ScriptManager;
import com.qwazr.scripts.ScriptServiceBuilder;
import com.qwazr.scripts.ScriptServiceInterface;
import com.qwazr.server.ApplicationBuilder;
import com.qwazr.server.BaseServer;
import com.qwazr.server.GenericServer;
import com.qwazr.server.GenericServerBuilder;
import com.qwazr.server.RestApplication;
import com.qwazr.server.WelcomeShutdownService;
import com.qwazr.server.configuration.ServerConfiguration;
import org.quartz.SchedulerException;
import javax.management.JMException;
import javax.servlet.ServletException;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class SchedulerServer implements BaseServer {
private final GenericServer server;
private final SchedulerServiceBuilder serviceBuilder;
private SchedulerServer(final ServerConfiguration configuration) throws IOException, SchedulerException {
final ExecutorService executorService = Executors.newCachedThreadPool();
final GenericServerBuilder builder = GenericServer.of(configuration, executorService);
final Set services = new HashSet<>();
services.add(ClusterServiceInterface.SERVICE_NAME);
services.add(SchedulerServiceInterface.SERVICE_NAME);
services.add(ScriptServiceInterface.SERVICE_NAME);
services.add(LibraryServiceInterface.SERVICE_NAME);
final ApplicationBuilder webServices = ApplicationBuilder.of("/*").classes(RestApplication.JSON_CLASSES).
singletons(new WelcomeShutdownService());
final ClusterManager clusterManager =
new ClusterManager(executorService, configuration).registerProtocolListener(builder, services);
webServices.singletons(clusterManager.getService());
final LibraryManager libraryManager =
new LibraryManager(configuration.dataDirectory, configuration.getEtcFiles());
builder.shutdownListener(server -> libraryManager.close());
webServices.singletons(libraryManager.getService());
final ScriptManager scriptManager =
new ScriptManager(executorService, clusterManager, libraryManager.getService(),
configuration.dataDirectory);
webServices.singletons(scriptManager.getService());
final ScriptServiceBuilder scriptServiceBuilder =
new ScriptServiceBuilder(executorService, clusterManager, scriptManager);
final SchedulerManager schedulerManager = new SchedulerManager(clusterManager, scriptServiceBuilder,
SchedulerManager.getMaxThreadConfiguration(configuration), configuration.getEtcFiles());
builder.shutdownListener((server) -> schedulerManager.close());
webServices.singletons(schedulerManager.getService());
serviceBuilder = new SchedulerServiceBuilder(executorService, clusterManager, schedulerManager);
builder.getWebServiceContext().jaxrs(webServices);
server = builder.build();
}
public GenericServer getServer() {
return server;
}
public SchedulerServiceBuilder getServiceBuilder() {
return serviceBuilder;
}
private static volatile SchedulerServer INSTANCE;
public static synchronized void shutdown() {
if (INSTANCE == null)
return;
INSTANCE.stop();
INSTANCE = null;
}
public static SchedulerServer getInstance() {
return INSTANCE;
}
public static synchronized void main(final String... args)
throws IOException, ServletException, ReflectiveOperationException, JMException, SchedulerException {
shutdown();
INSTANCE = new SchedulerServer(new ServerConfiguration(args));
INSTANCE.start();
}
}