iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >flume源码学习3-自动reload配
  • 535
分享到

flume源码学习3-自动reload配

源码flumereload 2023-01-31 03:01:54 535人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

  在1.5.0的flume版本中开始提供这个功能,判断配置文件的更新时间戳来reload服务原理:1)在启动中使用EventBus.reGISter注册Application对象,同时Application有一个Subscribe的方法h

  在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时

        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方法:

  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方法

    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的方法处理信息:

@Subscribe
public synchronized void handleConfigurationEvent(MaterializedConfiguration conf) { //reload服务
  stopAllComponents();
  startAllComponents(conf);
}

--结束END--

本文标题: flume源码学习3-自动reload配

本文链接: https://www.lsjlt.com/news/187654.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • flume源码学习3-自动reload配
      在1.5.0的flume版本中开始提供这个功能,判断配置文件的更新时间戳来reload服务原理:1)在启动中使用EventBus.register注册Application对象,同时Application有一个Subscribe的方法h...
    99+
    2023-01-31
    源码 flume reload
  • Spring框架学习之Spring @Autowired实现自动装配的代码
    学习自动装配之前,讲一个概念:Component,即组件。组件你也可以理解为bean对象,只不过通常Component的组成会稍微复杂一些,比如,一个组件里面会引用一个或多个别的be...
    99+
    2022-11-12
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作