com.netflix.config
Class DynamicPropertyFactory

java.lang.Object
  extended by com.netflix.config.DynamicPropertyFactory

public class DynamicPropertyFactory
extends java.lang.Object

A factory that creates instances of dynamic properties and associates them with an underlying configuration or DynamicPropertySupport where the properties could be changed dynamically at runtime.

It is recommended to initialize this class with a configuration or DynamicPropertySupport before the first call to getInstance(). Otherwise, it will be lazily initialized with a ConcurrentCompositeConfiguration, where a SystemConfiguration and DynamicURLConfiguration will be added. You can also disable installing the default configuration by setting system property "archaius.dynamicProperty.disableDefaultConfig" to be true.

If system property "archaius.dynamicPropertyFactory.registerConfigWithJMX" is set to true, when this class is initialized with a configuration, the configuration will also be exposed to JMX via an instance of BaseConfigMBean, where you can update the properties via jconsole. Example:

    import com.netflix.config.DynamicProperty;

    class MyClass {
        private static DynamicIntProperty maxWaitMillis
            = DynamicPropertyFactory.getInstance().getIntProperty("myclass.sleepMillis", 250);
           // ...

           // add a callback when this property is changed
           maxWaitMillis.addCallback(new Runnable() {
               public void run() {
                   int currentValue = maxWaitMillis.get();
                   // ...
               }
           });
           // ...
           // Wait for a configurable amount of time for condition to become true.
           // Note that the time can be changed on-the-fly.
           someCondition.wait(maxWaitMillis.get());

        // ...
    }
 

Please note that you should not cache the property value if you expect the value to change on-the-fly. For example, in the following code the change of the value is ineffective:

    int maxWaitMillis = DynamicPropertyFactory.getInstance().getIntProperty("myclass.sleepMillis", 250).get();
    // ...
    someCondition.wait(maxWaitMillis);


Field Summary
static java.lang.String DISABLE_DEFAULT_CONFIG
          System property to determine whether DynamicPropertyFactory should be lazily initialized with default configuration for getInstance().
static java.lang.String DISABLE_DEFAULT_SYS_CONFIG
          System property to disable adding SystemConfiguration to the default ConcurrentCompositeConfiguration
static java.lang.String ENABLE_JMX
          Boolean system property to define whether a configuration MBean should be registered with JMX so that properties can be accessed via JMX console.
static java.lang.String SYS_CONFIG_NAME
           
static java.lang.String THROW_MISSING_CONFIGURATION_SOURCE_EXCEPTION
          System property name that defines whether getInstance() should throw MissingConfigurationSourceException if there is no proper configuration source at the time of call.
static java.lang.String URL_CONFIG_NAME
           
 
Method Summary
static java.lang.Object getBackingConfigurationSource()
          Get the backing configuration from the factory.
 DynamicBooleanProperty getBooleanProperty(java.lang.String propName, boolean defaultValue)
          Create a new property whose value is a boolean and subject to change on-the-fly..
 DynamicBooleanProperty getBooleanProperty(java.lang.String propName, boolean defaultValue, java.lang.Runnable propertyChangeCallback)
          Create a new property whose value is a boolean and subject to change on-the-fly.
 DynamicDoubleProperty getDoubleProperty(java.lang.String propName, double defaultValue)
          Create a new property whose value is a double and subject to change on-the-fly..
 DynamicDoubleProperty getDoubleProperty(java.lang.String propName, double defaultValue, java.lang.Runnable propertyChangeCallback)
          Create a new property whose value is a double and subject to change on-the-fly.
 DynamicFloatProperty getFloatProperty(java.lang.String propName, float defaultValue)
          Create a new property whose value is a float and subject to change on-the-fly..
 DynamicFloatProperty getFloatProperty(java.lang.String propName, float defaultValue, java.lang.Runnable propertyChangeCallback)
          Create a new property whose value is a float and subject to change on-the-fly.
static DynamicPropertyFactory getInstance()
          Get the instance to create dynamic properties.
 DynamicIntProperty getIntProperty(java.lang.String propName, int defaultValue)
          Create a new property whose value is an integer and subject to change on-the-fly..
 DynamicIntProperty getIntProperty(java.lang.String propName, int defaultValue, java.lang.Runnable propertyChangeCallback)
          Create a new property whose value is an integer and subject to change on-the-fly.
 DynamicLongProperty getLongProperty(java.lang.String propName, long defaultValue)
          Create a new property whose value is a long and subject to change on-the-fly..
 DynamicLongProperty getLongProperty(java.lang.String propName, long defaultValue, java.lang.Runnable propertyChangeCallback)
          Create a new property whose value is a long and subject to change on-the-fly.
 DynamicStringProperty getStringProperty(java.lang.String propName, java.lang.String defaultValue)
          Create a new property whose value is a string and subject to change on-the-fly.
 DynamicStringProperty getStringProperty(java.lang.String propName, java.lang.String defaultValue, java.lang.Runnable propertyChangeCallback)
          Create a new property whose value is a string and subject to change on-the-fly.
static DynamicPropertyFactory initWithConfigurationSource(org.apache.commons.configuration.AbstractConfiguration config)
          Initialize the factory with an AbstractConfiguration.
static DynamicPropertyFactory initWithConfigurationSource(DynamicPropertySupport dynamicPropertySupport)
          Initialize the factory with a DynamicPropertySupport.
static boolean isInitializedWithDefaultConfig()
          Return whether the factory is initialized with the default ConcurrentCompositeConfiguration.
static boolean isThrowMissingConfigurationSourceException()
           
static void setThrowMissingConfigurationSourceException(boolean value)
          Set the boolean value to indicate whether getInstance() should throw MissingConfigurationSourceException if there is no proper configuration source at the time of call.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

URL_CONFIG_NAME

public static final java.lang.String URL_CONFIG_NAME
See Also:
Constant Field Values

SYS_CONFIG_NAME

public static final java.lang.String SYS_CONFIG_NAME
See Also:
Constant Field Values

ENABLE_JMX

public static final java.lang.String ENABLE_JMX
Boolean system property to define whether a configuration MBean should be registered with JMX so that properties can be accessed via JMX console. Default is "unset".

See Also:
Constant Field Values

THROW_MISSING_CONFIGURATION_SOURCE_EXCEPTION

public static final java.lang.String THROW_MISSING_CONFIGURATION_SOURCE_EXCEPTION
System property name that defines whether getInstance() should throw MissingConfigurationSourceException if there is no proper configuration source at the time of call.

See Also:
Constant Field Values

DISABLE_DEFAULT_SYS_CONFIG

public static final java.lang.String DISABLE_DEFAULT_SYS_CONFIG
System property to disable adding SystemConfiguration to the default ConcurrentCompositeConfiguration

See Also:
Constant Field Values

DISABLE_DEFAULT_CONFIG

public static final java.lang.String DISABLE_DEFAULT_CONFIG
System property to determine whether DynamicPropertyFactory should be lazily initialized with default configuration for getInstance(). Default is false (not set).

See Also:
Constant Field Values
Method Detail

initWithConfigurationSource

public static DynamicPropertyFactory initWithConfigurationSource(org.apache.commons.configuration.AbstractConfiguration config)
Initialize the factory with an AbstractConfiguration.

The initialization will register a ConfigurationListener with the configuration so that DynamicProperty will receives a callback and refresh its value when a property is changed in the configuration.

If the factory is already initialized with a default configuration source (see getInstance()), it will re-register itself with the new configuration source passed to this method. Otherwise, this method will throw IllegalArgumentException if it has been initialized with a different and non-default configuration source. This method should be only called once.

Parameters:
config - Configuration to be attached with DynamicProperty
Returns:
the instance of DynamicPropertyFactory
Throws:
java.lang.IllegalArgumentException - if the factory has already been initialized with a non-default configuration source

isInitializedWithDefaultConfig

public static boolean isInitializedWithDefaultConfig()
Return whether the factory is initialized with the default ConcurrentCompositeConfiguration.


getBackingConfigurationSource

public static java.lang.Object getBackingConfigurationSource()
Get the backing configuration from the factory. This can be cased to a ConcurrentCompositeConfiguration if the default configuration is installed.

For example:

     Configuration config = DynamicPropertyFactory.getInstance().getBackingConfigurationSource();
     if (DynamicPropertyFactory.isInitializedWithDefaultConfig()) {
         ConcurrentCompositeConfiguration composite = (ConcurrentCompositeConfiguration) config;
         // ...
     }
 

Returns:
the configuration source that DynamicPropertyFactory is initialized with. This will be an instance of Configuration, or DynamicPropertySupport, or null if DynamicPropertyFactory has not been initialized.

setThrowMissingConfigurationSourceException

public static void setThrowMissingConfigurationSourceException(boolean value)
Set the boolean value to indicate whether getInstance() should throw MissingConfigurationSourceException if there is no proper configuration source at the time of call.

Parameters:
value - to set

isThrowMissingConfigurationSourceException

public static boolean isThrowMissingConfigurationSourceException()
Returns:
the boolean value to indicate whether getInstance() should throw MissingConfigurationSourceException if there is no proper configuration source at the time of call.

initWithConfigurationSource

public static DynamicPropertyFactory initWithConfigurationSource(DynamicPropertySupport dynamicPropertySupport)
Initialize the factory with a DynamicPropertySupport.

The initialization will register a PropertyListener with the DynamicPropertySupport so that DynamicProperty will receives a callback and refresh its value when a property is changed.

If the factory is already initialized with a default configuration source (see getInstance()), it will re-register itself with the new configuration source passed to this method. Otherwise, this method will throw IllegalArgumentException if it has been initialized with a non-default configuration source. This method should be only called once.

Parameters:
dynamicPropertySupport - DynamicPropertySupport to be associated with the DynamicProperty
Returns:
the instance of DynamicPropertyFactory
Throws:
java.lang.IllegalArgumentException - if the factory has already been initialized with a different and non-default configuration source

getInstance

public static DynamicPropertyFactory getInstance()
Get the instance to create dynamic properties. If the factory is not initialized with a configuration source (see initWithConfigurationSource(AbstractConfiguration) and initWithConfigurationSource(DynamicPropertySupport)), it will fist try to initialize itself with a default ConcurrentCompositeConfiguration, with the following two sub configurations: Between the two sub-configurations, the SystemConfiguration will take the precedence when determining property values.

You can disable the initialization with the default configuration by setting system property "archaius.dynamicProperty.disableDefaultConfig" to "true".


getStringProperty

public DynamicStringProperty getStringProperty(java.lang.String propName,
                                               java.lang.String defaultValue)
Create a new property whose value is a string and subject to change on-the-fly.

Parameters:
propName - property name
defaultValue - default value if the property is not defined in underlying configuration

getStringProperty

public DynamicStringProperty getStringProperty(java.lang.String propName,
                                               java.lang.String defaultValue,
                                               java.lang.Runnable propertyChangeCallback)
Create a new property whose value is a string and subject to change on-the-fly.

Parameters:
propName - property name
defaultValue - default value if the property is not defined in underlying configuration
propertyChangeCallback - a Runnable to be called when the property is changed

getIntProperty

public DynamicIntProperty getIntProperty(java.lang.String propName,
                                         int defaultValue)
Create a new property whose value is an integer and subject to change on-the-fly..

Parameters:
propName - property name
defaultValue - default value if the property is not defined in underlying configuration

getIntProperty

public DynamicIntProperty getIntProperty(java.lang.String propName,
                                         int defaultValue,
                                         java.lang.Runnable propertyChangeCallback)
Create a new property whose value is an integer and subject to change on-the-fly.

Parameters:
propName - property name
defaultValue - default value if the property is not defined in underlying configuration
propertyChangeCallback - a Runnable to be called when the property is changed

getLongProperty

public DynamicLongProperty getLongProperty(java.lang.String propName,
                                           long defaultValue)
Create a new property whose value is a long and subject to change on-the-fly..

Parameters:
propName - property name
defaultValue - default value if the property is not defined in underlying configuration

getLongProperty

public DynamicLongProperty getLongProperty(java.lang.String propName,
                                           long defaultValue,
                                           java.lang.Runnable propertyChangeCallback)
Create a new property whose value is a long and subject to change on-the-fly.

Parameters:
propName - property name
defaultValue - default value if the property is not defined in underlying configuration
propertyChangeCallback - a Runnable to be called when the property is changed

getBooleanProperty

public DynamicBooleanProperty getBooleanProperty(java.lang.String propName,
                                                 boolean defaultValue)
Create a new property whose value is a boolean and subject to change on-the-fly..

Parameters:
propName - property name
defaultValue - default value if the property is not defined in underlying configuration

getBooleanProperty

public DynamicBooleanProperty getBooleanProperty(java.lang.String propName,
                                                 boolean defaultValue,
                                                 java.lang.Runnable propertyChangeCallback)
Create a new property whose value is a boolean and subject to change on-the-fly.

Parameters:
propName - property name
defaultValue - default value if the property is not defined in underlying configuration
propertyChangeCallback - a Runnable to be called when the property is changed

getFloatProperty

public DynamicFloatProperty getFloatProperty(java.lang.String propName,
                                             float defaultValue)
Create a new property whose value is a float and subject to change on-the-fly..

Parameters:
propName - property name
defaultValue - default value if the property is not defined in underlying configuration

getFloatProperty

public DynamicFloatProperty getFloatProperty(java.lang.String propName,
                                             float defaultValue,
                                             java.lang.Runnable propertyChangeCallback)
Create a new property whose value is a float and subject to change on-the-fly.

Parameters:
propName - property name
defaultValue - default value if the property is not defined in underlying configuration
propertyChangeCallback - a Runnable to be called when the property is changed

getDoubleProperty

public DynamicDoubleProperty getDoubleProperty(java.lang.String propName,
                                               double defaultValue)
Create a new property whose value is a double and subject to change on-the-fly..

Parameters:
propName - property name
defaultValue - default value if the property is not defined in underlying configuration

getDoubleProperty

public DynamicDoubleProperty getDoubleProperty(java.lang.String propName,
                                               double defaultValue,
                                               java.lang.Runnable propertyChangeCallback)
Create a new property whose value is a double and subject to change on-the-fly.

Parameters:
propName - property name
defaultValue - default value if the property is not defined in underlying configuration
propertyChangeCallback - a Runnable to be called when the property is changed