Logback中异步压缩任务实现

2017-10-30  本文已影响0人  a乐乐_1234

有些时候我们需要在业务主流程外做一些不影响主流程的操作,比如发生通知,开启一个异步任务,我们不关心这些额外操作的执行成功与否,但不能影响主流程。一般这种场景下,都会使用异步线程处理,下面带着源码,来看下Logback在进行日志归档的过程中如何通过异步任务实现日志压缩。希望大家举一反三,领会其设计精髓,方便以后运行在自己的代码中。

场景描述

Logback在进行日志归档过程中主要处理一下几件事:
1.文件名转换,将当前活动文件名转换成归档文件名。执行该步骤的条件是<appender>配置了<file> 属性,当前活动文件名是<file> 属性值,归档文件名是<fileNamePatternStr>的格式。
2.归档文件压缩,执行该步骤的条件是<fileNamePatternStr>属性值中以.gz/.zip等后缀结尾。
3.历史归档文件删除,执行该步骤的条件是配置了<maxHistory>。

// ch.qos.logback.core.rolling.TimeBasedRollingPolicy#rollover
//文件归档操作
public void rollover() throws RolloverFailure {
    //归档文件名全路径,如logs/test.log
    String elapsedPeriodsFileName = timeBasedFileNamingAndTriggeringPolicy.getElapsedPeriodsFileName();
    //归档文件名称,如test.log
    String elapsedPeriodStem = FileFilterUtil.afterLastSlash(elapsedPeriodsFileName);
    //判断是否配置压缩
    if (compressionMode == CompressionMode.NONE) {
        //判断<appender>中是否配置了<file>属性
        if (getParentsRawFileProperty() != null) {
            //将当前活动文件名转换为归档文件名
            renameUtil.rename(getParentsRawFileProperty(), elapsedPeriodsFileName);
         } 
     } else {
        if (getParentsRawFileProperty() == null) {
            //直接将归档文件压缩
            compressionFuture = compressor.asyncCompress(elapsedPeriodsFileName, elapsedPeriodsFileName, elapsedPeriodStem);
        } else {
            //先根据<file>创建一个临时文件和一个新的<file>文件,再将临时文件名转换为归档文件名,然后将归档文件压缩
            compressionFuture = renameRawAndAsyncCompress(elapsedPeriodsFileName, elapsedPeriodStem);
        }
    }
    //判断是否配置<maxHistory>
    if (archiveRemover != null) {
        Date now = new Date(timeBasedFileNamingAndTriggeringPolicy.getCurrentTime());
        this.cleanUpFuture = archiveRemover.cleanAsynchronously(now);
     }
}

这里面归档文件的压缩和历史文件的删除都使用的是异步任务。压缩操作使用Compressor类处理,删除操作使用ArchiveRemover类处理

//ch.qos.logback.core.rolling.helper.Compressor#asyncCompress
public Future<?> asyncCompress(String nameOfFile2Compress, String nameOfCompressedFile, String innerEntryName) throws RolloverFailure {
    //创建异步任务
    CompressionRunnable runnable = new CompressionRunnable(nameOfFile2Compress, nameOfCompressedFile, innerEntryName);
    //获取任务线程池
    ExecutorService executorService = context.getScheduledExecutorService();
    //执行异步任务
    Future<?> future = executorService.submit(runnable);
    return future;
 }

//ch.qos.logback.core.rolling.helper.Compressor.CompressionRunnable
class CompressionRunnable implements Runnable {
    final String nameOfFile2Compress;
    final String nameOfCompressedFile;
    final String innerEntryName;

    public CompressionRunnable(String nameOfFile2Compress, String nameOfCompressedFile, String innerEntryName) {
      this.nameOfFile2Compress = nameOfFile2Compress;
      this.nameOfCompressedFile = nameOfCompressedFile;
      this.innerEntryName = innerEntryName;
     }

    public void run() {
        //调用Compressor实例中的compress方法压缩文件
        Compressor.this.compress(nameOfFile2Compress, nameOfCompressedFile, innerEntryName);
    }
 }

CompressionRunnable 实际上是没有返回值的,但是asyncCompress方法还是返回了任务执行结果,那这个空的结果有什么用呢?

  //ch.qos.logback.core.rolling.TimeBasedRollingPolicy#stop
@Override
public void stop() {
   if (!isStarted())
      return;
    waitForAsynchronousJobToStop(compressionFuture,"compression");
    waitForAsynchronousJobToStop(cleanUpFuture, "clean-up");
    super.stop();
   }

private void waitForAsynchronousJobToStop(Future<?> aFuture, String jobDescription) {
    if (aFuture != null) {
       try {
       // 等待指定时间,如果任务还未执行完,就抛出异常
         aFuture.get(CoreConstants.SECONDS_TO_WAIT_FOR_COMPRESSION_JOBS, TimeUnit.SECONDS);
        } catch (TimeoutException e) {
            addError("Timeout while waiting for " + jobDescription + " job to finish", e);
        } catch (Exception e) {
           addError("Unexpected exception while waiting for " + jobDescription + " job to finish", e);
       }
    }
}

在日志系统停止时,如果压缩操作还在进行,等待一定时间,超过该时间抛出异常。

上一篇 下一篇

猜你喜欢

热点阅读