Skip to content

Commit

Permalink
Ensure that no matter how many times the embedded GlassFish server is…
Browse files Browse the repository at this point in the history
… started that it is correctly initialized.
  • Loading branch information
realityforge committed Aug 9, 2019
1 parent 06120f6 commit 2860f71
Showing 1 changed file with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,6 @@ public void postConstruct() {
if (transactions != null) {
transactions.addListenerForType(PayaraExecutorServiceConfiguration.class, this);
}

initialiseThreadPools();
}

/**
Expand All @@ -131,14 +129,29 @@ public void postConstruct() {
@Override
public void event(Event event) {
if (event.is(Deployment.ALL_APPLICATIONS_LOADED)) {
// there is awkward point in embedded EJBContainer initialization, where same server
// instance is started twice. In such case, we need to reinitialize.
if (threadPoolExecutor.isShutdown()) {
initialiseThreadPools();
}
// Embedded containers can be started and stopped multiple times.
// Thus we need to initialize anytime the server instance is started.
initialiseThreadPools();
} else if (event.is(EventTypes.SERVER_SHUTDOWN)) {
threadPoolExecutor.shutdown();
scheduledThreadPoolExecutor.shutdown();

// Wait until the schedulers actually terminate
try {
threadPoolExecutor.awaitTermination(5, TimeUnit.SECONDS);
scheduledThreadPoolExecutor.awaitTermination(5, TimeUnit.SECONDS);
} catch (InterruptedException ignored) {
}

// If they do not terminate in the alloted time then just forcefully terminate
if (!threadPoolExecutor.isShutdown()) {
threadPoolExecutor.shutdownNow();
}
if (!scheduledThreadPoolExecutor.isShutdown()) {
scheduledThreadPoolExecutor.shutdownNow();
}
threadPoolExecutor = null;
scheduledThreadPoolExecutor = null;
}
}

Expand Down

0 comments on commit 2860f71

Please sign in to comment.