標籤:read .post pos ide 代碼 super 基於 data void
在上一篇中講了Flume NG配置模組主要的介面的類,PropertiesConfigurationProvider提供了基於properties設定檔的靜態配置的能力,這篇細說一下PollingPropertiesFileConfigurationProvider提供的執行時動態改動配置並生效的能力。
要實現動態改動設定檔並生效,主要有兩個待實現的功能
1. 觀察設定檔是否改動
2. 假設改動,將改動的內容通知給觀察者
對於第一點,監控設定檔是否改動,Flume NG定義了一個FileWatcherRunnable對象來監控設定檔,啟動了一個單獨的線程採用定時輪詢的方式來監控,輪詢頻率是30毫秒一次。比較file.lastModified屬性與lastChange時間戳記,當file.lastModified > lastChange時表示檔案被改動
public class FileWatcherRunnable implements Runnable { private final File file; private final CounterGroup counterGroup; private long lastChange; public FileWatcherRunnable(File file, CounterGroup counterGroup) { super(); this.file = file; this.counterGroup = counterGroup; this.lastChange = 0L; } @Override public void run() { LOGGER.debug("Checking file:{} for changes", file); counterGroup.incrementAndGet("file.checks"); long lastModified = file.lastModified(); if (lastModified > lastChange) { LOGGER.info("Reloading configuration file:{}", file); counterGroup.incrementAndGet("file.loads"); lastChange = lastModified; try { eventBus.post(getConfiguration()); } catch (Exception e) { LOGGER.error("Failed to load configuration data. Exception follows.", e); } catch (NoClassDefFoundError e) { LOGGER.error("Failed to start agent because dependencies were not " + "found in classpath. Error follows.", e); } catch (Throwable t) { // caught because the caller does not handle or log Throwables LOGGER.error("Unhandled error", t); } } } }// PollingPropertiesFileConfigurationProvider.start()啟動一個單獨的線程來監控properties設定檔 public void start() { LOGGER.info("Configuration provider starting"); Preconditions.checkState(file != null, "The parameter file must not be null"); executorService = Executors.newSingleThreadScheduledExecutor( new ThreadFactoryBuilder().setNameFormat("conf-file-poller-%d") .build()); FileWatcherRunnable fileWatcherRunnable = new FileWatcherRunnable(file, counterGroup); executorService.scheduleWithFixedDelay(fileWatcherRunnable, 0, interval, TimeUnit.SECONDS); lifecycleState = LifecycleState.START; LOGGER.debug("Configuration provider started"); }
對於第二點,利用Guava EventBus提供的公布訂閱模式機制,將配置改動封裝成事件傳遞給Application。來又一次載入配置
// FileWatcherRunnable.run方法 公布配置改動的事件 eventBus.post(getConfiguration());// Application.main方法來注冊事件訂閱 Application application; if(reload) { EventBus eventBus = new EventBus(agentName + "-event-bus"); PollingPropertiesFileConfigurationProvider configurationProvider = new PollingPropertiesFileConfigurationProvider(agentName, configurationFile, eventBus, 30); components.add(configurationProvider); application = new Application(components); eventBus.register(application); }// Application類採用@Subscribe標註來定義訂閱者法,即配置改動後會運行handleConfigurationEvent方法,這種方法是安全執行緒的@Subscribe public synchronized void handleConfigurationEvent(MaterializedConfiguration conf) { stopAllComponents(); startAllComponents(conf); }
Flume NG原始碼分析(二)支援執行時動態改動配置的配置模組