com.kolibrifx.plovercrest.server.PlovercrestServer Maven / Gradle / Ivy
Show all versions of plovercrest-server Show documentation
/*
* Copyright (c) 2010-2017, KolibriFX AS. Licensed under the Apache License, version 2.0.
*/
package com.kolibrifx.plovercrest.server;
import java.io.File;
import java.util.Collection;
import java.util.concurrent.TimeUnit;
import org.apache.log4j.Logger;
import com.kolibrifx.plovercrest.client.TableSerializerFactory;
import com.kolibrifx.plovercrest.server.folds.FoldManager;
import com.kolibrifx.plovercrest.server.internal.EngineAdapter;
import com.kolibrifx.plovercrest.server.internal.folds.EngineFoldManager;
import com.kolibrifx.plovercrest.server.internal.protocol.StreamServer;
import com.kolibrifx.plovercrest.server.security.AccessControlFilter;
import com.kolibrifx.plovercrest.server.streams.PlovercrestStreamEngineBuilder;
import com.kolibrifx.plovercrest.server.streams.StreamEngine;
import com.kolibrifx.plovercrest.server.streams.StreamProvider;
public class PlovercrestServer {
private static final Logger log = Logger.getLogger(PlovercrestServer.class);
private final PlovercrestEngine engine;
private final StreamServer streamer;
private final EngineAdapter adapter;
private final FoldManager foldManager;
private final StreamEngine streamEngine;
public PlovercrestServer(final String dataPath,
final int streamerPort,
final TableSerializerFactory serializerFactory,
final AccessControlFilter accessControlFilter,
final Collection extraStreamProviders) {
engine = new PlovercrestEngine(dataPath);
streamEngine =
new PlovercrestStreamEngineBuilder(engine).serializerFactory(serializerFactory)
.extraProviders(extraStreamProviders).build();
adapter = new EngineAdapter(streamEngine);
streamer = new StreamServer(adapter, streamerPort, accessControlFilter);
foldManager = new EngineFoldManager(streamEngine, serializerFactory);
}
public void start() {
streamer.start();
log.info("Started plovercrest server with port " + getStreamerPort() + " and data dir: "
+ new File(engine.getDataPath()).getAbsolutePath());
}
/**
* Stop the server unconditionally. All clients will be disconnected without notice.
*
* Data that has already been received will be flushed to disk, but clients will not get a
* chance to flush active table writers.
*/
public void shutdownNow() {
streamer.shutdownNow();
streamEngine.close();
engine.close();
}
/**
* Shutdown the server, giving clients a chance to clean up. New connections will not be
* accepted. Once all clients have disconnected, the server will shut down. All data is flushed
* to disk.
*
* If the timeout is reached while clients are still active, the server is stopped
* unconditionally after logging a warning.
*/
public void shutdown(final long timeout, final TimeUnit unit) {
try {
if (!streamer.shutdown(timeout, unit)) {
log.warn("Server failed to shutdown cleanly after timeout (" + timeout + " " + unit + ")");
}
} catch (final InterruptedException e) {
log.error("Interrupted during shutdown", e);
Thread.currentThread().interrupt();
}
shutdownNow();
}
public int getStreamerPort() {
return streamer.getOpenPort();
}
public FoldManager getFoldManager() {
return foldManager;
}
/**
* Deprecated alias for {@link #shutdownNow()}.
*/
@Deprecated
public void stop() {
shutdownNow();
}
}