use of jgnash.engine.attachment.AttachmentTransferServer in project jgnash by ccavanaugh.
the class JpaNetworkServer method run.
/**
* Starts the server and blocks until it is stopped
*
* @param dataStoreType datastore type
* @param fileName database file name
* @param port port
* @param password password*
* @throws EngineException thrown if engine or buss cannot be created
*/
private synchronized void run(final DataStoreType dataStoreType, final String fileName, final int port, final char[] password) throws EngineException {
final DistributedLockServer distributedLockServer = new DistributedLockServer(port + LOCK_SERVER_INCREMENT);
final boolean lockServerStarted = distributedLockServer.startServer(password);
final AttachmentTransferServer attachmentTransferServer = new AttachmentTransferServer(port + TRANSFER_SERVER_INCREMENT, AttachmentUtils.getAttachmentDirectory(Paths.get(fileName)));
final boolean attachmentServerStarted = attachmentTransferServer.startServer(password);
if (attachmentServerStarted && lockServerStarted) {
final Engine engine = createEngine(dataStoreType, fileName, port, password);
if (engine != null) {
// Start the message bus and pass the file name so it can be reported to the client
final MessageBusServer messageBusServer = new MessageBusServer(port + MESSAGE_SERVER_INCREMENT);
// don't continue if the server is not started successfully
if (messageBusServer.startServer(dataStoreType, fileName, password)) {
// Start the backup thread that ensures an XML backup is created at set intervals
final ScheduledExecutorService backupExecutor = Executors.newSingleThreadScheduledExecutor(new DefaultDaemonThreadFactory("JPA Network Server Executor"));
// run commit every backup period after startup
backupExecutor.scheduleWithFixedDelay(() -> {
if (dirty) {
exportXML(engine, fileName);
EngineFactory.removeOldCompressedXML(fileName, engine.getRetainedBackupLimit());
dirty = false;
}
}, BACKUP_PERIOD, BACKUP_PERIOD, TimeUnit.HOURS);
final LocalServerListener listener = event -> {
// look for a remote request to stop the server
if (event.startsWith(STOP_SERVER_MESSAGE)) {
logger.info("Remote shutdown request was received");
stopServer();
}
dirty = true;
};
messageBusServer.addLocalListener(listener);
// if a callback has been registered, call it
if (runningCallback != null) {
runningCallback.run();
}
// wait here forever
try {
while (stopLatch.getCount() != 0) {
// check for condition, handle a spurious wake up
// wait forever from stopServer()
stopLatch.await();
}
} catch (final InterruptedException ex) {
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
Thread.currentThread().interrupt();
}
messageBusServer.removeLocalListener(listener);
backupExecutor.shutdown();
exportXML(engine, fileName);
messageBusServer.stopServer();
EngineFactory.closeEngine(SERVER_ENGINE);
EngineFactory.removeOldCompressedXML(fileName, engine.getRetainedBackupLimit());
distributedLockManager.disconnectFromServer();
distributedAttachmentManager.disconnectFromServer();
distributedLockServer.stopServer();
attachmentTransferServer.stopServer();
em.close();
factory.close();
} else {
throw new EngineException("Failed to start the Message Bus");
}
} else {
throw new EngineException("Failed to create the engine");
}
} else {
if (lockServerStarted) {
distributedLockServer.stopServer();
}
if (attachmentServerStarted) {
attachmentTransferServer.stopServer();
}
}
}
Aggregations