public class DynamicPropertyFactory extends Object
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);
Modifier and Type | Field and Description |
---|---|
static String |
DISABLE_DEFAULT_CONFIG
System property to determine whether DynamicPropertyFactory should be lazily initialized with
default configuration for
getInstance() . |
static String |
DISABLE_DEFAULT_SYS_CONFIG
System property to disable adding SystemConfiguration to the default ConcurrentCompositeConfiguration
|
static 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 String |
SYS_CONFIG_NAME |
static 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 String |
URL_CONFIG_NAME |
Modifier and Type | Method and Description |
---|---|
static Object |
getBackingConfigurationSource()
Get the backing configuration from the factory.
|
DynamicBooleanProperty |
getBooleanProperty(String propName,
boolean defaultValue)
Create a new property whose value is a boolean and subject to change on-the-fly..
|
DynamicBooleanProperty |
getBooleanProperty(String propName,
boolean defaultValue,
Runnable propertyChangeCallback)
Create a new property whose value is a boolean and subject to change on-the-fly.
|
DynamicDoubleProperty |
getDoubleProperty(String propName,
double defaultValue)
Create a new property whose value is a double and subject to change on-the-fly..
|
DynamicDoubleProperty |
getDoubleProperty(String propName,
double defaultValue,
Runnable propertyChangeCallback)
Create a new property whose value is a double and subject to change on-the-fly.
|
DynamicFloatProperty |
getFloatProperty(String propName,
float defaultValue)
Create a new property whose value is a float and subject to change on-the-fly..
|
DynamicFloatProperty |
getFloatProperty(String propName,
float defaultValue,
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(String propName,
int defaultValue)
Create a new property whose value is an integer and subject to change on-the-fly..
|
DynamicIntProperty |
getIntProperty(String propName,
int defaultValue,
Runnable propertyChangeCallback)
Create a new property whose value is an integer and subject to change on-the-fly.
|
DynamicLongProperty |
getLongProperty(String propName,
long defaultValue)
Create a new property whose value is a long and subject to change on-the-fly..
|
DynamicLongProperty |
getLongProperty(String propName,
long defaultValue,
Runnable propertyChangeCallback)
Create a new property whose value is a long and subject to change on-the-fly.
|
DynamicStringProperty |
getStringProperty(String propName,
String defaultValue)
Create a new property whose value is a string and subject to change on-the-fly.
|
DynamicStringProperty |
getStringProperty(String propName,
String defaultValue,
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. |
public static final String URL_CONFIG_NAME
public static final String SYS_CONFIG_NAME
public static final String ENABLE_JMX
public static final String THROW_MISSING_CONFIGURATION_SOURCE_EXCEPTION
getInstance()
should throw
MissingConfigurationSourceException
if there is no proper configuration source
at the time of call.public static final String DISABLE_DEFAULT_SYS_CONFIG
public static final String DISABLE_DEFAULT_CONFIG
getInstance()
. Default is false (not set).public static DynamicPropertyFactory initWithConfigurationSource(org.apache.commons.configuration.AbstractConfiguration config)
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.
config
- Configuration to be attached with DynamicPropertyIllegalArgumentException
- if the factory has already been initialized with a non-default configuration sourcepublic static boolean isInitializedWithDefaultConfig()
public static Object getBackingConfigurationSource()
ConcurrentCompositeConfiguration
if the default configuration is installed.
For example:
Configuration config = DynamicPropertyFactory.getInstance().getBackingConfigurationSource(); if (DynamicPropertyFactory.isInitializedWithDefaultConfig()) { ConcurrentCompositeConfiguration composite = (ConcurrentCompositeConfiguration) config; // ... }
DynamicPropertySupport
, or null if DynamicPropertyFactory has not been initialized.public static void setThrowMissingConfigurationSourceException(boolean value)
getInstance()
should throw
MissingConfigurationSourceException
if there is no proper configuration source
at the time of call.value
- to setpublic static boolean isThrowMissingConfigurationSourceException()
getInstance()
should throw
MissingConfigurationSourceException
if there is no proper configuration source
at the time of call.public static DynamicPropertyFactory initWithConfigurationSource(DynamicPropertySupport dynamicPropertySupport)
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.
dynamicPropertySupport
- DynamicPropertySupport to be associated with the DynamicPropertyIllegalArgumentException
- if the factory has already been initialized with a different and non-default configuration sourcepublic static DynamicPropertyFactory getInstance()
initWithConfigurationSource(AbstractConfiguration)
and initWithConfigurationSource(DynamicPropertySupport)
),
it will fist try to initialize itself with a default ConcurrentCompositeConfiguration
, with the following two
sub configurations:
true
DynamicURLConfiguration
, which at a fixed interval polls
a configuration file (see URLConfigurationSource.DEFAULT_CONFIG_FILE_NAME
) on classpath and a set of URLs specified via a system property
(see URLConfigurationSource.CONFIG_URL
).
You can disable the initialization with the default configuration by setting system property "archaius.dynamicProperty.disableDefaultConfig" to "true".
public DynamicStringProperty getStringProperty(String propName, String defaultValue)
propName
- property namedefaultValue
- default value if the property is not defined in underlying configurationpublic DynamicStringProperty getStringProperty(String propName, String defaultValue, Runnable propertyChangeCallback)
propName
- property namedefaultValue
- default value if the property is not defined in underlying configurationpropertyChangeCallback
- a Runnable to be called when the property is changedpublic DynamicIntProperty getIntProperty(String propName, int defaultValue)
propName
- property namedefaultValue
- default value if the property is not defined in underlying configurationpublic DynamicIntProperty getIntProperty(String propName, int defaultValue, Runnable propertyChangeCallback)
propName
- property namedefaultValue
- default value if the property is not defined in underlying configurationpropertyChangeCallback
- a Runnable to be called when the property is changedpublic DynamicLongProperty getLongProperty(String propName, long defaultValue)
propName
- property namedefaultValue
- default value if the property is not defined in underlying configurationpublic DynamicLongProperty getLongProperty(String propName, long defaultValue, Runnable propertyChangeCallback)
propName
- property namedefaultValue
- default value if the property is not defined in underlying configurationpropertyChangeCallback
- a Runnable to be called when the property is changedpublic DynamicBooleanProperty getBooleanProperty(String propName, boolean defaultValue)
propName
- property namedefaultValue
- default value if the property is not defined in underlying configurationpublic DynamicBooleanProperty getBooleanProperty(String propName, boolean defaultValue, Runnable propertyChangeCallback)
propName
- property namedefaultValue
- default value if the property is not defined in underlying configurationpropertyChangeCallback
- a Runnable to be called when the property is changedpublic DynamicFloatProperty getFloatProperty(String propName, float defaultValue)
propName
- property namedefaultValue
- default value if the property is not defined in underlying configurationpublic DynamicFloatProperty getFloatProperty(String propName, float defaultValue, Runnable propertyChangeCallback)
propName
- property namedefaultValue
- default value if the property is not defined in underlying configurationpropertyChangeCallback
- a Runnable to be called when the property is changedpublic DynamicDoubleProperty getDoubleProperty(String propName, double defaultValue)
propName
- property namedefaultValue
- default value if the property is not defined in underlying configurationpublic DynamicDoubleProperty getDoubleProperty(String propName, double defaultValue, Runnable propertyChangeCallback)
propName
- property namedefaultValue
- default value if the property is not defined in underlying configurationpropertyChangeCallback
- a Runnable to be called when the property is changedCopyright © 2017. All Rights Reserved.