Skip to content

Commit 93aba0e

Browse files
authored
Merge pull request #50 from bugsnag/allow-executor-service-to-die
Stop AsyncHttpDelivery from indefinitely blocking exit
2 parents 272cf8b + b02b793 commit 93aba0e

2 files changed

Lines changed: 11 additions & 6 deletions

File tree

examples/simple/src/main/java/com/bugsnag/example/simple/ExampleApp.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,5 @@ public void run() {
6161
throw new RuntimeException("Unhandled exception");
6262
}
6363
}).start();
64-
65-
// Exit to run the shutdown hooks
66-
System.exit(0);
6764
}
6865
}

src/main/java/com/bugsnag/delivery/AsyncHttpDelivery.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,23 @@
77

88
import java.net.Proxy;
99
import java.util.concurrent.ExecutorService;
10-
import java.util.concurrent.Executors;
10+
import java.util.concurrent.LinkedBlockingQueue;
11+
import java.util.concurrent.ThreadPoolExecutor;
1112
import java.util.concurrent.TimeUnit;
1213

1314
public class AsyncHttpDelivery implements HttpDelivery {
1415
private static final Logger logger = LoggerFactory.getLogger(AsyncHttpDelivery.class);
1516
private static final int SHUTDOWN_TIMEOUT = 5000;
1617

17-
protected HttpDelivery baseDelivery = new SyncHttpDelivery();
18-
protected ExecutorService executorService = Executors.newSingleThreadExecutor();
18+
private HttpDelivery baseDelivery = new SyncHttpDelivery();
19+
20+
// Create an exector service which keeps idle threads alive for a maximum of SHUTDOWN_TIMEOUT.
21+
// This should avoid blocking an application that doesn't call shutdown from exiting.
22+
private ExecutorService executorService =
23+
new ThreadPoolExecutor(0, 1,
24+
SHUTDOWN_TIMEOUT, TimeUnit.MILLISECONDS,
25+
new LinkedBlockingQueue<Runnable>());
26+
1927
private boolean shuttingDown = false;
2028

2129
/**

0 commit comments

Comments
 (0)