|
43 | 43 | import java.util.concurrent.ExecutionException;
|
44 | 44 | import java.util.concurrent.ExecutorService;
|
45 | 45 | import java.util.concurrent.Executors;
|
46 |
| -import java.util.concurrent.Future; |
| 46 | +import java.util.concurrent.RejectedExecutionException; |
47 | 47 | import java.util.concurrent.atomic.AtomicBoolean;
|
48 | 48 | import java.util.concurrent.atomic.AtomicInteger;
|
49 | 49 | import java.util.logging.Level;
|
50 | 50 |
|
51 | 51 | import static com.earth2me.essentials.I18n.tlLiteral;
|
52 | 52 |
|
53 | 53 | public class EssentialsConfiguration {
|
54 |
| - private static final ExecutorService EXECUTOR_SERVICE = Executors.newSingleThreadExecutor(); |
| 54 | + private static volatile ExecutorService EXECUTOR_SERVICE = Executors.newSingleThreadExecutor(); |
55 | 55 | private static final ObjectMapper.Factory MAPPER_FACTORY = ObjectMapper.factoryBuilder()
|
56 | 56 | .addProcessor(DeleteOnEmpty.class, (data, value) -> new DeleteOnEmptyProcessor())
|
57 | 57 | .addProcessor(DeleteIfIncomplete.class, (data, value) -> new DeleteIfIncompleteProcessor())
|
@@ -462,21 +462,50 @@ public synchronized void save() {
|
462 | 462 |
|
463 | 463 | public synchronized void blockingSave() {
|
464 | 464 | try {
|
465 |
| - delaySave().get(); |
| 465 | + delaySave(); |
| 466 | + getExecutor().submit(() -> {}).get(); |
466 | 467 | } catch (final InterruptedException | ExecutionException e) {
|
467 | 468 | Essentials.getWrappedLogger().log(Level.SEVERE, e.getMessage(), e);
|
468 | 469 | }
|
469 | 470 | }
|
470 | 471 |
|
471 |
| - private Future<?> delaySave() { |
| 472 | + private void delaySave() { |
472 | 473 | if (saveHook != null) {
|
473 | 474 | saveHook.run();
|
474 | 475 | }
|
475 | 476 |
|
476 |
| - final CommentedConfigurationNode node = configurationNode.copy(); |
477 |
| - |
478 | 477 | pendingWrites.incrementAndGet();
|
| 478 | + try { |
| 479 | + getExecutor().submit(new ConfigurationSaveTask(loader, () -> configurationNode.copy(), pendingWrites)); |
| 480 | + } catch (RejectedExecutionException rex) { |
| 481 | + try { |
| 482 | + synchronized (loader) { |
| 483 | + loader.save(configurationNode.copy()); |
| 484 | + } |
| 485 | + } catch (ConfigurateException e) { |
| 486 | + Essentials.getWrappedLogger().log(Level.SEVERE, e.getMessage(), e); |
| 487 | + } finally { |
| 488 | + pendingWrites.decrementAndGet(); |
| 489 | + } |
| 490 | + } |
| 491 | + } |
479 | 492 |
|
480 |
| - return EXECUTOR_SERVICE.submit(new ConfigurationSaveTask(loader, node, pendingWrites)); |
| 493 | + public static void shutdownExecutor() { |
| 494 | + final ExecutorService exec = EXECUTOR_SERVICE; |
| 495 | + exec.shutdown(); |
| 496 | + try { |
| 497 | + if (!exec.awaitTermination(5, java.util.concurrent.TimeUnit.SECONDS)) { |
| 498 | + exec.shutdownNow(); |
| 499 | + } |
| 500 | + } catch (InterruptedException ignored) { |
| 501 | + exec.shutdownNow(); |
| 502 | + } |
| 503 | + } |
| 504 | + |
| 505 | + private static synchronized ExecutorService getExecutor() { |
| 506 | + if (EXECUTOR_SERVICE == null || EXECUTOR_SERVICE.isShutdown() || EXECUTOR_SERVICE.isTerminated()) { |
| 507 | + EXECUTOR_SERVICE = Executors.newSingleThreadExecutor(); |
| 508 | + } |
| 509 | + return EXECUTOR_SERVICE; |
481 | 510 | }
|
482 | 511 | }
|
0 commit comments