| | |
| | | |
| | | import java.util.concurrent.LinkedBlockingQueue; |
| | | import java.util.concurrent.TimeUnit; |
| | | import java.util.concurrent.atomic.AtomicBoolean; |
| | | |
| | | /** |
| | | * A Text Writer which writes log records asynchronously to |
| | |
| | | private final LinkedBlockingQueue<String> queue; |
| | | |
| | | private String name; |
| | | private boolean stopRequested; |
| | | private AtomicBoolean stopRequested; |
| | | private WriterThread writerThread; |
| | | |
| | | private boolean autoFlush; |
| | |
| | | |
| | | this.queue = new LinkedBlockingQueue<String>(capacity); |
| | | this.writerThread = null; |
| | | this.stopRequested = false; |
| | | this.stopRequested = new AtomicBoolean(false); |
| | | |
| | | writerThread = new WriterThread(); |
| | | writerThread.start(); |
| | |
| | | public void run() |
| | | { |
| | | String message = null; |
| | | while (!isShuttingDown() || !queue.isEmpty()) { |
| | | while (!stopRequested.get() || !queue.isEmpty()) { |
| | | try |
| | | { |
| | | message = queue.poll(10, TimeUnit.SECONDS); |
| | |
| | | } |
| | | } |
| | | |
| | | // Method needs to be synchronized with _shutdown mutator, as we don't |
| | | // want shutdown to start after we check for it, but before we queue |
| | | // request. |
| | | private synchronized void writeAsynchronously(String record) |
| | | { |
| | | // If shutting down reject, otherwise publish (if we have a publisher!) |
| | | while (!isShuttingDown()) |
| | | { |
| | | // Put request on queue for writer |
| | | try |
| | | { |
| | | queue.put(record); |
| | | break; |
| | | } |
| | | catch(InterruptedException e) |
| | | { |
| | | // We expect this to happen. Just ignore it and hopefully |
| | | // drop out in the next try. |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Write the log record asyncronously. |
| | | * |
| | |
| | | { |
| | | // No writer? Off to the bit bucket. |
| | | if (writer != null) { |
| | | writeAsynchronously(record); |
| | | while (!stopRequested.get()) |
| | | { |
| | | // Put request on queue for writer |
| | | try |
| | | { |
| | | queue.put(record); |
| | | break; |
| | | } |
| | | catch(InterruptedException e) |
| | | { |
| | | // We expect this to happen. Just ignore it and hopefully |
| | | // drop out in the next try. |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | /** |
| | | * Queries whether the publisher is in shutdown mode. |
| | | */ |
| | | private boolean isShuttingDown() |
| | | { |
| | | return stopRequested; |
| | | } |
| | | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | public void shutdown() |
| | |
| | | */ |
| | | public void shutdown(boolean shutdownWrapped) |
| | | { |
| | | stopRequested = true; |
| | | stopRequested.set(true); |
| | | |
| | | // Wait for publisher thread to terminate |
| | | while (writerThread != null && writerThread.isAlive()) { |