Flume ng Source Code Analysis (ii) configuration modules that support dynamic configuration changes at execution time

Source: Internet
Author: User
Tags eventbus

In the previous article, the Flume NG configuration module is the main interface class, Propertiesconfigurationprovider provides the ability to base on the static configuration of the properties configuration file, This article is about the ability of Pollingpropertiesfileconfigurationprovider to dynamically change configuration and take effect when executing.


To implement the dynamic configuration file and take effect, there are two main features to be implemented

1. See if the configuration file changes

2. If changes are made, the content of the changes is notified to the viewer


For the 1th, if the monitoring configuration file is changed, Flume ng defines a filewatcherrunnable object to monitor the configuration file and initiates a separate thread to monitor with a timed poll frequency of 30 milliseconds. Compare File.lastmodified properties with Lastchange timestamps, when file.lastmodified > lastchange indicate file changes

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 "+" found in classpath.        Error follows. ", e); } catch (Throwable t) {//caught because the caller does not handle or log throwables logger.error ("Unh        andled error ", T); }}}}//Pollingpropertiesfileconfigurationprovider.start () initiates a separate thread to monitor the properties configuration file  public void Start () {     Logger.info ("Configuration provider starting");    preconditions.checkstate (file ! = null,        "The parameter file must not being 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"); }

For the 2nd, the advertisement subscription mode mechanism provided by guava Eventbus is used to encapsulate the configuration changes into an event to be passed to application. To load the configuration again

The Filewatcherrunnable.run method publishes the event Eventbus.post (GetConfiguration ()) of the configuration change, and//Application.main method to register the event subscription application A pplication;      if (reload) {        EventBus EventBus = new Eventbus (agentname + "-event-bus");        Pollingpropertiesfileconfigurationprovider Configurationprovider =             New Pollingpropertiesfileconfigurationprovider (agentname,                 configurationfile, Eventbus,;         Components.add (Configurationprovider);        application = new Application (components);        Eventbus.register (Application ;     }//Application class uses @subscribe annotations to define the subscription method, that is, the configuration changes will run the Handleconfigurationevent method, This approach is thread-safe @subscribe  Public synchronized void Handleconfigurationevent (materializedconfiguration conf) {    Stopallcomponents ();    startallcomponents (conf); }


Flume ng Source Code Analysis (ii) configuration modules that support dynamic configuration changes at execution time

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.