All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.smallmind.scribe.pen.AsynchronousAppender Maven / Gradle / Ivy

/*
 * Copyright (c) 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 David Berkman
 * 
 * This file is part of the SmallMind Code Project.
 * 
 * The SmallMind Code Project is free software, you can redistribute
 * it and/or modify it under either, at your discretion...
 * 
 * 1) The terms of GNU Affero General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 * 
 * ...or...
 * 
 * 2) The terms of the Apache License, Version 2.0.
 * 
 * The SmallMind Code Project is distributed in the hope that it will
 * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License or Apache License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * and the Apache License along with the SmallMind Code Project. If not, see
 *  or .
 * 
 * Additional permission under the GNU Affero GPL version 3 section 7
 * ------------------------------------------------------------------
 * If you modify this Program, or any covered work, by linking or
 * combining it with other code, such other code is not for that reason
 * alone subject to any of the requirements of the GNU Affero GPL
 * version 3.
 */
package org.smallmind.scribe.pen;

import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

public class AsynchronousAppender implements Appender {

  private final AtomicBoolean finished = new AtomicBoolean(false);
  private final Appender internalAppender;
  private final LinkedBlockingQueue publishQueue;
  private final PublishWorker publishWorker;

  public AsynchronousAppender (Appender internalAppender) {

    this(internalAppender, Integer.MAX_VALUE);
  }

  public AsynchronousAppender (Appender internalAppender, int bufferSize) {

    Thread publishThread;

    this.internalAppender = internalAppender;

    publishQueue = new LinkedBlockingQueue<>(bufferSize);

    publishThread = new Thread(publishWorker = new PublishWorker());
    publishThread.setDaemon(true);
    publishThread.start();
  }

  public String getName () {

    return internalAppender.getName();
  }

  public void setName (String name) {

    internalAppender.setName(name);
  }

  public void clearFilters () {

    internalAppender.clearFilters();
  }

  public synchronized void setFilter (Filter filter) {

    internalAppender.setFilter(filter);
  }

  public void addFilter (Filter filter) {

    internalAppender.addFilter(filter);
  }

  public Filter[] getFilters () {

    return internalAppender.getFilters();
  }

  public void setFilters (List filterList) {

    internalAppender.setFilters(filterList);
  }

  public ErrorHandler getErrorHandler () {

    return internalAppender.getErrorHandler();
  }

  public void setErrorHandler (ErrorHandler errorHandler) {

    internalAppender.setErrorHandler(errorHandler);
  }

  public Formatter getFormatter () {

    return internalAppender.getFormatter();
  }

  public void setFormatter (Formatter formatter) {

    internalAppender.setFormatter(formatter);
  }

  public boolean isActive () {

    return internalAppender.isActive();
  }

  public void setActive (boolean active) {

    internalAppender.setActive(active);
  }

  public void publish (Record record) {

    try {
      if (finished.get()) {
        throw new LoggerException("%s has been previously closed", this.getClass().getSimpleName());
      }

      publishQueue.put(record);
    } catch (Exception exception) {
      if (internalAppender.getErrorHandler() == null) {
        exception.printStackTrace();
      } else {
        internalAppender.getErrorHandler().process(record, exception, "Unable to publish message from appender(%s)", this.getClass().getCanonicalName());
      }
    }
  }

  public void close ()
    throws InterruptedException, LoggerException {

    publishWorker.finish();
    internalAppender.close();
  }

  protected void finalize ()
    throws InterruptedException, LoggerException {

    close();
  }

  private class PublishWorker implements Runnable {

    private final CountDownLatch exitLatch = new CountDownLatch(1);

    private void finish ()
      throws InterruptedException {

      finished.compareAndSet(false, true);
      exitLatch.await();
    }

    public void run () {

      Record record;

      try {
        while (!finished.get()) {
          if ((record = publishQueue.poll(1, TimeUnit.SECONDS)) != null) {
            internalAppender.publish(record);
          }
        }
      } catch (InterruptedException interruptedException) {
        finished.set(true);
        LoggerManager.getLogger(AsynchronousAppender.class).error(interruptedException);
      } finally {
        exitLatch.countDown();
      }
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy