Fixed possible ExecutorService leak in BukkitImporter

This commit is contained in:
Rsl1122 2019-02-15 10:42:05 +02:00
parent 4f68a59dcb
commit 9616d5b6f0

View File

@ -89,17 +89,12 @@ public abstract class BukkitImporter implements Importer {
public final void processImport() { public final void processImport() {
ExecutorService service = Executors.newCachedThreadPool(); ExecutorService service = Executors.newCachedThreadPool();
submitTo(service, this::processServerData);
submitTo(service, this::processUserData);
service.shutdown();
try { try {
service.awaitTermination(20, TimeUnit.MINUTES); submitTo(service, this::processServerData);
} catch (InterruptedException e) { submitTo(service, this::processUserData);
service.shutdownNow(); } finally {
Thread.currentThread().interrupt(); shutdownService(service);
} }
} }
private void processServerData() { private void processServerData() {
@ -111,16 +106,12 @@ public abstract class BukkitImporter implements Importer {
ExecutorService service = Executors.newCachedThreadPool(); ExecutorService service = Executors.newCachedThreadPool();
SaveOperations save = dbSystem.getDatabase().save();
submitTo(service, () -> save.insertTPS(ImmutableMap.of(serverUUID.get(), serverImportData.getTpsData())));
submitTo(service, () -> save.insertCommandUsage(ImmutableMap.of(serverUUID.get(), serverImportData.getCommandUsages())));
service.shutdown();
try { try {
service.awaitTermination(20, TimeUnit.MINUTES); SaveOperations save = dbSystem.getDatabase().save();
} catch (InterruptedException e) { submitTo(service, () -> save.insertTPS(ImmutableMap.of(serverUUID.get(), serverImportData.getTpsData())));
service.shutdownNow(); submitTo(service, () -> save.insertCommandUsage(ImmutableMap.of(serverUUID.get(), serverImportData.getCommandUsages())));
Thread.currentThread().interrupt(); } finally {
shutdownService(service);
} }
} }
@ -165,19 +156,27 @@ public abstract class BukkitImporter implements Importer {
ExecutorService service = Executors.newCachedThreadPool(); ExecutorService service = Executors.newCachedThreadPool();
SaveOperations save = dbSystem.getDatabase().save(); try {
SaveOperations save = dbSystem.getDatabase().save();
// TODO Replace with a transaction // TODO Replace with a transaction
save.insertUsers(users); save.insertUsers(users);
submitTo(service, () -> save.insertSessions(ImmutableMap.of(serverUUID.get(), sessions), true)); submitTo(service, () -> save.insertSessions(ImmutableMap.of(serverUUID.get(), sessions), true));
submitTo(service, () -> save.kickAmount(timesKicked)); submitTo(service, () -> save.kickAmount(timesKicked));
submitTo(service, () -> save.insertUserInfo(ImmutableMap.of(serverUUID.get(), userInfo))); submitTo(service, () -> save.insertUserInfo(ImmutableMap.of(serverUUID.get(), userInfo)));
submitTo(service, () -> save.insertNicknames(ImmutableMap.of(serverUUID.get(), nickNames))); submitTo(service, () -> save.insertNicknames(ImmutableMap.of(serverUUID.get(), nickNames)));
submitTo(service, () -> save.insertAllGeoInfo(geoInfo)); submitTo(service, () -> save.insertAllGeoInfo(geoInfo));
} finally {
shutdownService(service);
}
}
private void shutdownService(ExecutorService service) {
service.shutdown(); service.shutdown();
try { try {
service.awaitTermination(20, TimeUnit.MINUTES); if (!service.awaitTermination(20, TimeUnit.MINUTES)) {
service.shutdownNow();
}
} catch (InterruptedException e) { } catch (InterruptedException e) {
service.shutdownNow(); service.shutdownNow();
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();