public class DynamicPropertyFactory
extends java.lang.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 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
Deprecated.
Moved to ConfigurationManager in 0.5.12
|
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
Deprecated.
Moved to ConfigurationManager in 0.5.12
|
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
Deprecated.
Moved to ConfigurationManager in 0.5.12
|
Modifier and Type | Method and Description |
---|---|
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.
|
<T> DynamicContextualProperty<T> |
getContextualProperty(java.lang.String propName,
T defaultValue)
Create a new contextual property of type T
|
<T> DynamicContextualProperty<T> |
getContextualProperty(java.lang.String propName,
T defaultValue,
java.lang.Runnable propertyChangeCallback)
Create a new contextual property of type T
|
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(AbstractConfiguration config)
Initialize the factory with an AbstractConfiguration.
|
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. |
@Deprecated public static final java.lang.String URL_CONFIG_NAME
@Deprecated public static final java.lang.String SYS_CONFIG_NAME
public static final java.lang.String ENABLE_JMX
public static final java.lang.String THROW_MISSING_CONFIGURATION_SOURCE_EXCEPTION
getInstance()
should throw
MissingConfigurationSourceException
if there is no proper configuration source
at the time of call.@Deprecated public static final java.lang.String DISABLE_DEFAULT_SYS_CONFIG
public static final java.lang.String DISABLE_DEFAULT_CONFIG
getInstance()
. Default is false (not set).public static DynamicPropertyFactory initWithConfigurationSource(AbstractConfiguration config)
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 DynamicPropertyjava.lang.IllegalArgumentException
- if the factory has already been initialized with a non-default configuration sourcepublic static boolean isInitializedWithDefaultConfig()
public static java.lang.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 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
).
public DynamicStringProperty getStringProperty(java.lang.String propName, java.lang.String defaultValue)
propName
- property namedefaultValue
- default value if the property is not defined in underlying configurationpublic DynamicStringProperty getStringProperty(java.lang.String propName, java.lang.String defaultValue, java.lang.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(java.lang.String propName, int defaultValue)
propName
- property namedefaultValue
- default value if the property is not defined in underlying configurationpublic DynamicIntProperty getIntProperty(java.lang.String propName, int defaultValue, java.lang.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(java.lang.String propName, long defaultValue)
propName
- property namedefaultValue
- default value if the property is not defined in underlying configurationpublic DynamicLongProperty getLongProperty(java.lang.String propName, long defaultValue, java.lang.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(java.lang.String propName, boolean defaultValue)
propName
- property namedefaultValue
- default value if the property is not defined in underlying configurationpublic DynamicBooleanProperty getBooleanProperty(java.lang.String propName, boolean defaultValue, java.lang.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(java.lang.String propName, float defaultValue)
propName
- property namedefaultValue
- default value if the property is not defined in underlying configurationpublic DynamicFloatProperty getFloatProperty(java.lang.String propName, float defaultValue, java.lang.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(java.lang.String propName, double defaultValue)
propName
- property namedefaultValue
- default value if the property is not defined in underlying configurationpublic DynamicDoubleProperty getDoubleProperty(java.lang.String propName, double defaultValue, java.lang.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 <T> DynamicContextualProperty<T> getContextualProperty(java.lang.String propName, T defaultValue)
T
- the type of the property valuepropName
- property namedefaultValue
- default value if the property is not defined in underlying configurationpublic <T> DynamicContextualProperty<T> getContextualProperty(java.lang.String propName, T defaultValue, java.lang.Runnable propertyChangeCallback)
T
- the type of the property valuepropName
- property namedefaultValue
- default value if the property is not defined in underlying configurationpropertyChangeCallback
- a Runnable to be called when the property is changed