|
2 | 2 |
|
3 | 3 | import java.util.List;
|
4 | 4 | import java.util.Optional;
|
| 5 | +import java.util.concurrent.ExecutorService; |
| 6 | +import java.util.concurrent.Executors; |
5 | 7 |
|
6 | 8 | import javax.swing.undo.UndoManager;
|
7 | 9 |
|
@@ -418,53 +420,90 @@ public void startBackgroundTasks() {
|
418 | 420 |
|
419 | 421 | @Override
|
420 | 422 | public void stop() {
|
421 |
| - LOGGER.trace("Closing AI service"); |
422 |
| - try { |
423 |
| - aiService.close(); |
424 |
| - } catch (Exception e) { |
425 |
| - LOGGER.error("Unable to close AI service", e); |
| 423 | + LOGGER.trace("Stopping JabRef GUI"); |
| 424 | + try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { |
| 425 | + LOGGER.trace("Stopping JabRef GUI using a virtual thread executor"); |
| 426 | + |
| 427 | + // Shutdown everything in parallel to prevent causing non-shutdown of something in case of issues |
| 428 | + executor.submit(() -> { |
| 429 | + LOGGER.trace("Closing citations and relations search service"); |
| 430 | + citationsAndRelationsSearchService.close(); |
| 431 | + LOGGER.trace("Citations and relations search service closed"); |
| 432 | + }); |
| 433 | + |
| 434 | + executor.submit(() -> { |
| 435 | + LOGGER.trace("Closing AI service"); |
| 436 | + try { |
| 437 | + aiService.close(); |
| 438 | + } catch (Exception e) { |
| 439 | + LOGGER.error("Unable to close AI service", e); |
| 440 | + } |
| 441 | + LOGGER.trace("AI service closed"); |
| 442 | + }); |
| 443 | + |
| 444 | + executor.submit(() -> { |
| 445 | + LOGGER.trace("Closing OpenOffice connection"); |
| 446 | + OOBibBaseConnect.closeOfficeConnection(); |
| 447 | + LOGGER.trace("OpenOffice connection closed"); |
| 448 | + }); |
| 449 | + |
| 450 | + executor.submit(() -> { |
| 451 | + LOGGER.trace("Shutting down remote server manager"); |
| 452 | + remoteListenerServerManager.stop(); |
| 453 | + LOGGER.trace("RemoteListenerServerManager shut down"); |
| 454 | + }); |
| 455 | + |
| 456 | + executor.submit(() -> { |
| 457 | + LOGGER.trace("Shutting down http server manager"); |
| 458 | + httpServerManager.stop(); |
| 459 | + LOGGER.trace("HttpServerManager shut down"); |
| 460 | + }); |
| 461 | + |
| 462 | + executor.submit(() -> { |
| 463 | + LOGGER.trace("Stopping background tasks"); |
| 464 | + Unirest.shutDown(); |
| 465 | + LOGGER.trace("Unirest shut down"); |
| 466 | + }); |
| 467 | + |
| 468 | + // region All threading related shutdowns |
| 469 | + executor.submit(() -> { |
| 470 | + LOGGER.trace("Shutting down taskExecutor"); |
| 471 | + if (taskExecutor != null) { |
| 472 | + taskExecutor.shutdown(); |
| 473 | + } |
| 474 | + LOGGER.trace("TaskExecutor shut down"); |
| 475 | + }); |
| 476 | + |
| 477 | + executor.submit(() -> { |
| 478 | + LOGGER.trace("Shutting down fileUpdateMonitor"); |
| 479 | + fileUpdateMonitor.shutdown(); |
| 480 | + LOGGER.trace("FileUpdateMonitor shut down"); |
| 481 | + }); |
| 482 | + |
| 483 | + executor.submit(() -> { |
| 484 | + LOGGER.trace("Shutting down directoryMonitor"); |
| 485 | + DirectoryMonitor directoryMonitor = Injector.instantiateModelOrService(DirectoryMonitor.class); |
| 486 | + directoryMonitor.shutdown(); |
| 487 | + LOGGER.trace("DirectoryMonitor shut down"); |
| 488 | + }); |
| 489 | + |
| 490 | + executor.submit(() -> { |
| 491 | + LOGGER.trace("Shutting down postgreServer"); |
| 492 | + PostgreServer postgreServer = Injector.instantiateModelOrService(PostgreServer.class); |
| 493 | + postgreServer.shutdown(); |
| 494 | + LOGGER.trace("PostgreServer shut down"); |
| 495 | + }); |
| 496 | + |
| 497 | + executor.submit(() -> { |
| 498 | + LOGGER.trace("Shutting down HeadlessExecutorService"); |
| 499 | + HeadlessExecutorService.INSTANCE.shutdownEverything(); |
| 500 | + LOGGER.trace("HeadlessExecutorService shut down"); |
| 501 | + }); |
| 502 | + // endregion |
| 503 | + |
| 504 | + HeadlessExecutorService.gracefullyShutdown("HeadlessExecutorService", executor, 30); |
426 | 505 | }
|
427 | 506 |
|
428 |
| - LOGGER.trace("Closing OpenOffice connection"); |
429 |
| - OOBibBaseConnect.closeOfficeConnection(); |
430 |
| - |
431 |
| - LOGGER.trace("Shutting down remote server manager"); |
432 |
| - remoteListenerServerManager.stop(); |
433 |
| - |
434 |
| - LOGGER.trace("Shutting down http server manager"); |
435 |
| - httpServerManager.stop(); |
436 |
| - |
437 |
| - LOGGER.trace("Stopping background tasks"); |
438 |
| - stopBackgroundTasks(); |
439 |
| - |
440 |
| - LOGGER.trace("Shutting down thread pools"); |
441 |
| - shutdownThreadPools(); |
442 |
| - |
443 |
| - LOGGER.trace("Closing citations and relations search service"); |
444 |
| - citationsAndRelationsSearchService.close(); |
445 |
| - |
446 | 507 | LOGGER.trace("Finished stop");
|
447 | 508 | }
|
448 |
| - |
449 |
| - public void stopBackgroundTasks() { |
450 |
| - Unirest.shutDown(); |
451 |
| - } |
452 |
| - |
453 |
| - public static void shutdownThreadPools() { |
454 |
| - LOGGER.trace("Shutting down taskExecutor"); |
455 |
| - if (taskExecutor != null) { |
456 |
| - taskExecutor.shutdown(); |
457 |
| - } |
458 |
| - LOGGER.trace("Shutting down fileUpdateMonitor"); |
459 |
| - fileUpdateMonitor.shutdown(); |
460 |
| - LOGGER.trace("Shutting down directoryMonitor"); |
461 |
| - DirectoryMonitor directoryMonitor = Injector.instantiateModelOrService(DirectoryMonitor.class); |
462 |
| - directoryMonitor.shutdown(); |
463 |
| - LOGGER.trace("Shutting down postgreServer"); |
464 |
| - PostgreServer postgreServer = Injector.instantiateModelOrService(PostgreServer.class); |
465 |
| - postgreServer.shutdown(); |
466 |
| - LOGGER.trace("Shutting down HeadlessExecutorService"); |
467 |
| - HeadlessExecutorService.INSTANCE.shutdownEverything(); |
468 |
| - LOGGER.trace("Finished shutdownThreadPools"); |
469 |
| - } |
470 | 509 | }
|
0 commit comments