[PR #1] Refactoring Thread > Executor

This commit is contained in:
Guillaume Polaert 2017-05-02 17:13:00 +02:00
parent c9ea5c0c5d
commit 9e9e525535
1 changed files with 108 additions and 111 deletions

View File

@ -7,13 +7,11 @@ import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Semaphore;
import java.util.concurrent.*;
/**
* This writer write provided traces to the a DD agent which is most of time located on the same host.
*
* <p>
* It handles writes asynchronuously so the calling threads are automatically released. However, if too much spans are collected
* the writers can reach a state where it is forced to drop incoming spans.
*/
@ -50,7 +48,7 @@ public class DDAgentWriter implements Writer {
/**
* Async worker that posts the spans to the DD agent
*/
private final Thread asyncWriterThread;
private final ExecutorService executor = Executors.newSingleThreadExecutor();
/**
* The DD agent api
@ -68,9 +66,8 @@ public class DDAgentWriter implements Writer {
tokens = new Semaphore(DEFAULT_MAX_SPANS);
traces = new ArrayBlockingQueue<List<Span>>(DEFAULT_MAX_SPANS);
asyncWriterThread = new Thread(new SpansSendingTask(), "dd.DDAgentWriter-SpansSendingTask");
asyncWriterThread.setDaemon(true);
asyncWriterThread.start();
executor.submit(new SpansSendingTask());
}
/* (non-Javadoc)
@ -91,9 +88,9 @@ public class DDAgentWriter implements Writer {
* @see com.datadoghq.trace.Writer#close()
*/
public void close() {
asyncWriterThread.interrupt();
executor.shutdownNow();
try {
asyncWriterThread.join();
executor.awaitTermination(500, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
logger.info("Writer properly closed and async writer interrupted.");
}