Skip to content

Commit

Permalink
** JIRA Required: Use a single thread executor instead of the default…
Browse files Browse the repository at this point in the history
… executor for a more graceful shutdown.
  • Loading branch information
jamezp committed Apr 22, 2020
1 parent ef55491 commit 8c77f5b
Showing 1 changed file with 29 additions and 0 deletions.
Expand Up @@ -9,6 +9,10 @@

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

/**
* com.sun.net.httpserver.HttpServer adapter for Resteasy. You may instead want to create and manage your own HttpServer.
Expand All @@ -25,6 +29,23 @@ public class SunHttpJaxrsServer implements EmbeddedJaxrsServer<SunHttpJaxrsServe
protected int runtimePort = -1;
protected ResteasyDeployment deployment;
private EmbeddedServerHelper serverHelper = new EmbeddedServerHelper();
private final ExecutorService executor = Executors.newSingleThreadExecutor(new ThreadFactory() {
@Override
public Thread newThread(final Runnable r) {
final Thread result = new Thread(r);
final ClassLoader cl;
if (deployment == null) {
cl = SunHttpJaxrsServer.class.getClassLoader();
} else {
cl = deployment.getClass().getClassLoader();
}
// TODO (jrp) think about this and if it should be privileged
result.setContextClassLoader(cl);
result.setName("EmbeddedJaxrsServer");
result.setDaemon(true);
return result;
}
});

@Override
public SunHttpJaxrsServer deploy() {
Expand All @@ -49,6 +70,7 @@ public SunHttpJaxrsServer start()
try
{
httpServer = HttpServer.create(new InetSocketAddress(configuredPort), 10);
httpServer.setExecutor(executor);
runtimePort = httpServer.getAddress().getPort();
}
catch (IOException e)
Expand All @@ -66,6 +88,13 @@ public void stop()
{
runtimePort = -1;
httpServer.stop(0);
executor.shutdown();
try {
// TODO (jrp) is 10 correct? That is what the VertxJaxrsServer uses
executor.awaitTermination(10, TimeUnit.SECONDS);
} catch (InterruptedException ignore) {
Thread.currentThread().interrupt();
}
context.cleanup();

if (deployment != null) {
Expand Down

0 comments on commit 8c77f5b

Please sign in to comment.