美文网首页
Logback中异步压缩任务实现

Logback中异步压缩任务实现

作者: a乐乐_1234 | 来源:发表于2017-10-30 11:47 被阅读0次

有些时候我们需要在业务主流程外做一些不影响主流程的操作,比如发生通知,开启一个异步任务,我们不关心这些额外操作的执行成功与否,但不能影响主流程。一般这种场景下,都会使用异步线程处理,下面带着源码,来看下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);
       }
    }
}

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

相关文章

  • Logback中异步压缩任务实现

    有些时候我们需要在业务主流程外做一些不影响主流程的操作,比如发生通知,开启一个异步任务,我们不关心这些额外操作的执...

  • 如何回收旧版本数据

    Compat接口首先会更新当前server已压缩的版本号,并将耗时昂贵的压缩任务保存到FIFO队列中异步执行,压缩...

  • Logback实现异步日志

    官方介绍:http://logback.qos.ch/manual/appenders.html#AsyncApp...

  • flutter之---Future的正确用法

    在flutter中经常会用到异步任务,dart中异步任务异步处理是用Future来处理,那么如何实现用Future...

  • flutter Future

    在flutter中经常会用到异步任务,dart中异步任务异步处理是用Future来处理,那么如何实现用Future...

  • Celery+Redis实现异步任务(2)

    相关: Celery-详解Celery+Redis实现异步任务(1)Celery+Redis实现异步任务(2)Ce...

  • Celery+Redis实现异步任务(3)

    相关: Celery-详解Celery+Redis实现异步任务(1)Celery+Redis实现异步任务(2)Ce...

  • Spring Boot与任务

    异步任务、定时任务、邮件任务 一、异步任务在Java应用中,绝大多数情况下都是通过同步的方式来实现交互处理的;但是...

  • GCD实现等待多个异步任务完成

    开发过程中可能需要实现多个异步任务完成后再执行下一步任务,GCD实现如下:

  • 自定义FutureTask实现

    FutureTask FutureTask是Future的实现,用来异步任务的获取结果,可以启动和取消异步任务,查...

网友评论

      本文标题:Logback中异步压缩任务实现

      本文链接:https://www.haomeiwen.com/subject/dkkppxtx.html