前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >flume源码学习3-自动reload配

flume源码学习3-自动reload配

作者头像
py3study
发布2020-01-09 12:36:07
4800
发布2020-01-09 12:36:07
举报
文章被收录于专栏:python3

  在1.5.0的flume版本中开始提供这个功能,判断配置文件的更新时间戳来reload服务 原理: 1)在启动中使用EventBus.register注册Application对象,同时Application有一个Subscribe的方法handleConfigurationEvent(参数是MaterializedConfiguration对象) 2)定义了一个计划任务线程池,检测到文件更新情况(判断文件的更新时间) 3)如果检测到文件有更新会使用EventBus.post方法发送这个event(MaterializedConfiguration对象) 4)调用Application.handleConfigurationEvent重启各个组件 下面看具体实现: 在org.apache.flume.node.Application的main方法中: 支持reload时

代码语言:javascript
复制
        EventBus eventBus = new EventBus(agentName + "-event-bus" ); //实例化一个EventBus对象
        PollingPropertiesFileConfigurationProvider configurationProvider =
            new PollingPropertiesFileConfigurationProvider(agentName,
                configurationFile, eventBus, 30); //默认的检测文件是否更新的interval时间为30s
        components.add(configurationProvider); //添加到启动列表中,在start方法中会启动PollingPropertiesFileConfigurationProvider 的计划任务线程池
        application = new Application(components);
        eventBus.register(application); //向EventBus中注册Application对象,让Application对象作为时间的监听者

org.apache.flume.node.PollingPropertiesFileConfigurationProvider //扩展了PropertiesFileConfigurationProvider类并实现了LifecycleAware接口 PollingPropertiesFileConfigurationProvider是一个有生命周期概念的监控配置更改的服务: start方法:

代码语言:javascript
复制
  public void start() {
....
    executorService = Executors.newSingleThreadScheduledExecutor(
            new ThreadFactoryBuilder().setNameFormat("conf-file-poller-%d" )
                .build()); //定义一个单线程的任务调度池
    FileWatcherRunnable fileWatcherRunnable =
        new FileWatcherRunnable(file , counterGroup ); //构建一个FileWatcherRunnable服务对象
    executorService.scheduleWithFixedDelay(fileWatcherRunnable, 0, interval,
        TimeUnit. SECONDS); //以interval为时间间隔运行FileWatcherRunnable
    lifecycleState = LifecycleState. START; // 设置为START状态
...
  }

FileWatcherRunnable是一个实现了Runnable 接口的线程类: 其主要的run方法

代码语言:javascript
复制
    public void run() {
...
      long lastModified = file.lastModified(); //调用File.lastModified获取文件的上一次更新时的时间戳
      if (lastModified > lastChange) { //初始lastChange 为0
...
        lastChange = lastModified; // 设置本次的lastChange 为更新时间,如果下一次更新文件,lastModified 会大于这个lastChange 
        try {
          eventBus.post(getConfiguration());
        } 
.....
      }
    }
  }

在检测到更新后调用eventBus.post(getConfiguration())向监听者发送信息,这里为Application对象,监听者调用注释为Subscribe的方法处理信息:

代码语言:javascript
复制
@Subscribe
public synchronized void handleConfigurationEvent(MaterializedConfiguration conf) { //reload服务
  stopAllComponents();
  startAllComponents(conf);
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/09/02 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档