Fixing shutdown of the server and using only one background cleaning thread.
This commit is contained in:
@@ -1,42 +1,23 @@
|
|||||||
package com.gempukku.lotro;
|
package com.gempukku.lotro;
|
||||||
|
|
||||||
public abstract class AbstractServer {
|
public abstract class AbstractServer {
|
||||||
|
private static ServerCleaner _cleaningTask = new ServerCleaner();
|
||||||
|
|
||||||
private boolean _started;
|
private boolean _started;
|
||||||
private CleaningTask _cleaningTask;
|
|
||||||
|
|
||||||
public final synchronized void startServer() {
|
public final synchronized void startServer() {
|
||||||
if (!_started) {
|
if (!_started) {
|
||||||
_cleaningTask = new CleaningTask();
|
_cleaningTask.addServer(this);
|
||||||
new Thread(_cleaningTask).start();
|
|
||||||
_started = true;
|
_started = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public final synchronized void stopServer() {
|
public final synchronized void stopServer() {
|
||||||
if (_started) {
|
if (_started) {
|
||||||
_cleaningTask.stop();
|
_cleaningTask.removeServer(this);
|
||||||
_started = false;
|
_started = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract void cleanup();
|
protected abstract void cleanup();
|
||||||
|
|
||||||
private class CleaningTask implements Runnable {
|
|
||||||
private boolean _running = true;
|
|
||||||
|
|
||||||
public void run() {
|
|
||||||
while (_running) {
|
|
||||||
cleanup();
|
|
||||||
try {
|
|
||||||
Thread.sleep(1000);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
// Ignore
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void stop() {
|
|
||||||
_running = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,53 @@
|
|||||||
|
package com.gempukku.lotro;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
public class ServerCleaner {
|
||||||
|
private final Set<AbstractServer> _servers = Collections.synchronizedSet(new HashSet<AbstractServer>());
|
||||||
|
private CleaningThread _thr;
|
||||||
|
|
||||||
|
public synchronized void addServer(AbstractServer server) {
|
||||||
|
_servers.add(server);
|
||||||
|
if (_thr == null) {
|
||||||
|
_thr = new CleaningThread();
|
||||||
|
_thr.start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized void removeServer(AbstractServer server) {
|
||||||
|
_servers.remove(server);
|
||||||
|
if (_servers.size() == 0 && _thr != null) {
|
||||||
|
_thr.pleaseStop();
|
||||||
|
_thr = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class CleaningThread extends Thread {
|
||||||
|
private boolean _stopped;
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
while (!_stopped) {
|
||||||
|
synchronized (ServerCleaner.this) {
|
||||||
|
for (AbstractServer server : _servers) {
|
||||||
|
try {
|
||||||
|
server.cleanup();
|
||||||
|
} catch (Exception exp) {
|
||||||
|
// We can't do much about it
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Thread.sleep(1000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (InterruptedException exp) {
|
||||||
|
// Thread interrupted - get lost
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void pleaseStop() {
|
||||||
|
_stopped = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -16,6 +16,7 @@ import com.sun.jersey.core.spi.component.ComponentScope;
|
|||||||
import com.sun.jersey.spi.inject.Injectable;
|
import com.sun.jersey.spi.inject.Injectable;
|
||||||
import com.sun.jersey.spi.inject.InjectableProvider;
|
import com.sun.jersey.spi.inject.InjectableProvider;
|
||||||
|
|
||||||
|
import javax.annotation.PreDestroy;
|
||||||
import javax.ws.rs.core.Context;
|
import javax.ws.rs.core.Context;
|
||||||
import javax.ws.rs.ext.Provider;
|
import javax.ws.rs.ext.Provider;
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
@@ -76,6 +77,14 @@ public class ServerProvider implements InjectableProvider<Context, Type> {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PreDestroy
|
||||||
|
public void destroyResources() {
|
||||||
|
getHallServerInjectable().getValue().stopServer();
|
||||||
|
getTradeServerInjectable().getValue().stopServer();
|
||||||
|
getLotroServerInjectable().getValue().stopServer();
|
||||||
|
getChatServerInjectable().getValue().stopServer();
|
||||||
|
}
|
||||||
|
|
||||||
private synchronized Injectable<LotroFormatLibrary> getLotroFormatLibraryInjectable() {
|
private synchronized Injectable<LotroFormatLibrary> getLotroFormatLibraryInjectable() {
|
||||||
if (_lotroFormatLibraryInjectable == null) {
|
if (_lotroFormatLibraryInjectable == null) {
|
||||||
final LotroFormatLibrary lotroFormatLibrary = new LotroFormatLibrary(_library);
|
final LotroFormatLibrary lotroFormatLibrary = new LotroFormatLibrary(_library);
|
||||||
|
|||||||
Reference in New Issue
Block a user