博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
聊聊rocketmq的RollingFileAppender
阅读量:6161 次
发布时间:2019-06-21

本文共 5253 字,大约阅读时间需要 17 分钟。

  hot3.png

本文主要研究一下rocketmq的RollingFileAppender

RollingFileAppender

org/apache/rocketmq/logging/inner/LoggingBuilder.java

public static class RollingFileAppender extends FileAppender {        protected long maxFileSize = 10 * 1024 * 1024;        protected int maxBackupIndex = 1;        private long nextRollover = 0;        public RollingFileAppender() {            super();        }        public int getMaxBackupIndex() {            return maxBackupIndex;        }        public long getMaximumFileSize() {            return maxFileSize;        }        //......        public synchronized void setFile(String fileName, boolean append, boolean bufferedIO, int bufferSize)            throws IOException {            super.setFile(fileName, append, this.bufferedIO, this.bufferSize);            if (append) {                File f = new File(fileName);                ((CountingQuietWriter) qw).setCount(f.length());            }        }        public void setMaxBackupIndex(int maxBackups) {            this.maxBackupIndex = maxBackups;        }        public void setMaximumFileSize(long maxFileSize) {            this.maxFileSize = maxFileSize;        }        protected void setQWForFiles(Writer writer) {            this.qw = new CountingQuietWriter(writer, this);        }        protected void subAppend(LoggingEvent event) {            super.subAppend(event);            if (fileName != null && qw != null) {                long size = ((CountingQuietWriter) qw).getCount();                if (size >= maxFileSize && size >= nextRollover) {                    rollOver();                }            }        }        protected class CountingQuietWriter extends QuietWriter {            protected long count;            public CountingQuietWriter(Writer writer, Appender appender) {                super(writer, appender);            }            public void write(String string) {                try {                    out.write(string);                    count += string.length();                } catch (IOException e) {                    appender.handleError("Write failure.", e, Appender.CODE_WRITE_FAILURE);                }            }            public long getCount() {                return count;            }            public void setCount(long count) {                this.count = count;            }        }    }
  • 这里重写了subAppend方法,调用父类subAppend方法之后,判断是否需要rollOver
  • 这里定义了maxFileSize,即单个文件的大小,然后还定义了nextRollover索引
  • 这里使用的是CountingQuietWriter,里头有个count来累积计算字符串的长度

RollingFileAppender.rollOver

org/apache/rocketmq/logging/inner/LoggingBuilder.java

public void rollOver() {            File target;            File file;            if (qw != null) {                long size = ((CountingQuietWriter) qw).getCount();                SysLogger.debug("rolling over count=" + size);                nextRollover = size + maxFileSize;            }            SysLogger.debug("maxBackupIndex=" + maxBackupIndex);            boolean renameSucceeded = true;            if (maxBackupIndex > 0) {                file = new File(fileName + '.' + maxBackupIndex);                if (file.exists()) {                    renameSucceeded = file.delete();                }                for (int i = maxBackupIndex - 1; i >= 1 && renameSucceeded; i--) {                    file = new File(fileName + "." + i);                    if (file.exists()) {                        target = new File(fileName + '.' + (i + 1));                        SysLogger.debug("Renaming file " + file + " to " + target);                        renameSucceeded = file.renameTo(target);                    }                }                if (renameSucceeded) {                    target = new File(fileName + "." + 1);                    this.closeFile(); // keep windows happy.                    file = new File(fileName);                    SysLogger.debug("Renaming file " + file + " to " + target);                    renameSucceeded = file.renameTo(target);                    if (!renameSucceeded) {                        try {                            this.setFile(fileName, true, bufferedIO, bufferSize);                        } catch (IOException e) {                            if (e instanceof InterruptedIOException) {                                Thread.currentThread().interrupt();                            }                            SysLogger.error("setFile(" + fileName + ", true) call failed.", e);                        }                    }                }            }            if (renameSucceeded) {                try {                    this.setFile(fileName, false, bufferedIO, bufferSize);                    nextRollover = 0;                } catch (IOException e) {                    if (e instanceof InterruptedIOException) {                        Thread.currentThread().interrupt();                    }                    SysLogger.error("setFile(" + fileName + ", false) call failed.", e);                }            }        }
  • 这个方法首先更新nextRollover的值,然后根据maxBackupIndex来递增重命名文件,然后再把现有的文件重名为为.1后缀
  • 重命名成功之后,再对新的文件进行setFile相关设置,关联writer,写入header

小结

RollingFileAppender在每次append的时候,都会先append数据,然后再判断是否超出文件大小限制,超出了再执行rollOver操作,对既有文件进行重命名,然后重新生成新的文件。注意这里没有进行同步操作,因此需要最外层调用的方法有同步并发控制。

doc

转载于:https://my.oschina.net/go4it/blog/1925527

你可能感兴趣的文章
logstash向elasticsearch写入数据,如何指定多个数据template
查看>>
Node.js:Web模块、文件系统
查看>>
【转】灵活运用 SQL SERVER FOR XML PATH
查看>>
WCF角色服务
查看>>
常用sql001_partition by 以及 row_number()和 dense_rank()和rank()区别
查看>>
已开源|码上用它开始Flutter混合开发——FlutterBoost
查看>>
原地踏步 – 没有突破高效理念的结果
查看>>
“间谍”软件克星Ad-Aware
查看>>
终端服务器超出了最大允许连接数
查看>>
ansible软件模块参数
查看>>
vue-amap - 基于Vue2.0和高德地图的地图组件
查看>>
Windows Tips
查看>>
C语言常用函数说明
查看>>
Java经典设计模式-创建型模式-抽象工厂模式(Abstract Factory)
查看>>
js 字符串操作
查看>>
phpstorm 10 注册码
查看>>
十年 IT 老兵告诉你如何快速构建可直接落地、基于开源的低成本架构!
查看>>
让Sublime Text 2支持GBK
查看>>
Android 四种启动模式
查看>>
pg_lightool-postgres数据分布查看工具
查看>>