Enterprise Java

Dynamic Property Management in Spring

Static and Dynamic Properties are very important for both operational management and changing the behavior of the system at the production level. Specially, dynamic parameters reduces interruption of the service. This article shows how to manage dynamic properties in Spring Applications by using Quartz.
Multi-Job Scheduling Service by using Spring and Quartz article is offered for Spring and Quartz Integration.

Let us look at Dynamic Property Management in Spring.

Used Technologies :

JDK 1.6.0_31
Spring 3.1.1
Quartz 1.8.5
Maven 3.0.2

STEP 1 : CREATE MAVEN PROJECT

A maven project is created as follows. (It can be created by using Maven or IDE Plug-in).

STEP 2 : LIBRARIES

Spring dependencies are added to Maven’ s pom.xml.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<properties>
 <spring.version>3.1.1.RELEASE</spring.version>
</properties>
 
<dependencies>
    <!-- Spring 3 dependencies -->
       <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
  <version>${spring.version}</version>
 </dependency>
      <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>${spring.version}</version>
 </dependency>
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context-support</artifactId>
  <version>${spring.version}</version>
 </dependency>
  <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-tx</artifactId>
  <version>${spring.version}</version>
 </dependency>
 
 <!-- Quartz dependency -->
  <dependency>
     <groupId>org.quartz-scheduler</groupId>
     <artifactId>quartz</artifactId>
     <version>1.8.5</version>
 </dependency>
 
 <!-- Log4j dependency -->
 <dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
  <version>1.2.16</version>
 </dependency>
 
</dependencies>

STEP 3 : CREATE DynamicPropertiesFile.properties

DynamicPropertiesFile covers dynamic properties of the application.

01
02
03
04
05
06
07
08
09
10
11
# This property defines message content
# Possible values = Text. Default value : Welcome
Message_Content = Welcome Visitor
 
# This property defines minimum visitor count
# Possible values = positive integer. Default value : 1
Minimum_Visitor_Count = 1
 
# This property defines maximum visitor count
# Possible values = positive integer. Default value : 10
Maximum_Visitor_Count = 10

STEP 4 : CREATE applicationContext.xml

Application Context is created as follows :

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
 
 
 <!-- Beans Declaration -->
 <!-- Core Dynamic Properties Bean Declaration -->
 <bean id="CoreDynamicPropertiesBean" class="org.springframework.beans.factory.config.PropertiesFactoryBean" scope="prototype">
  <property name="location" value="classpath:DynamicPropertiesFile.properties" />
   </bean>
 <!-- Dynamic Properties Map Declaration -->
 <bean id="DynamicPropertiesMap" class="java.util.HashMap"/>
 
 <!-- Dynamic Properties File Reader Task Declaration -->
 <bean id="DynamicPropertiesFileReaderTask" class="com.otv.dynamic.properties.task.DynamicPropertiesFileReaderTask">
  <property name="dynamicPropertiesMap"   ref="DynamicPropertiesMap"/>
 </bean>
 <!-- End of Beans Declaration -->
 
 <!-- Scheduler Configuration -->
 <!-- Job Detail-->
 <bean id="DynamicPropertiesFileReaderTaskJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
  <property name="targetObject" ref="DynamicPropertiesFileReaderTask" />
  <property name="targetMethod" value="start" />
 </bean>
 
 <!-- Simple Trigger -->
 <bean id="DynamicPropertiesFileReaderTaskTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
  <property name="jobDetail" ref="DynamicPropertiesFileReaderTaskJobDetail" />
  <property name="repeatInterval" value="60000" />
  <property name="startDelay" value="0" />
 </bean>
 
 <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
  <property name="jobDetails">
     <list>
        <ref bean="DynamicPropertiesFileReaderTaskJobDetail" />
     </list>
  </property>
  <property name="triggers">
     <list>
     <ref bean="DynamicPropertiesFileReaderTaskTrigger" />
     </list>
  </property>
 </bean>
 <!-- End of Scheduler Configuration -->
</beans>

STEP 5 : CREATE SystemConstants CLASS

A new SystemConstants Class is created. This class covers all system constants.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.otv.common;
 
/**
 * System Constants
 *
 * @author  onlinetechvision.com
 * @since   26 May 2012
 * @version 1.0.0
 *
 */
public class SystemConstants {
 
 //Names of Dynamic Properties...
 public static final String DYNAMIC_PROPERTY_MESSAGE_CONTENT = "Message_Content";
 public static final String DYNAMIC_PROPERTY_MINIMUM_VISITOR_COUNT = "Minimum_Visitor_Count";
 public static final String DYNAMIC_PROPERTY_MAXIMUM_VISITOR_COUNT = "Maximum_Visitor_Count";
 
 //Default Values of Dynamic Properties...
 public static final String DYNAMIC_PROPERTY_MESSAGE_CONTENT_DEFAULT_VALUE       = "Welcome";
 public static final String DYNAMIC_PROPERTY_MINIMUM_VISITOR_COUNT_DEFAULT_VALUE = "1";
 public static final String DYNAMIC_PROPERTY_MAXIMUM_VISITOR_COUNT_DEFAULT_VALUE = "10";
 
 public static final String BEAN_NAME_CORE_DYNAMIC_PROPERTIES_BEAN = "CoreDynamicPropertiesBean";
 
 public static final String APPLICATION_CONTEXT_FILE_NAME = "applicationContext.xml";
}

STEP 6 : CREATE DynamicPropertiesFileReaderTask CLASS

DynamicPropertiesFileReaderTask Class is created. This class is managed by Quartz. It reads all dynamic properties via DynamicPropertiesFile by invoking “start” method in every minute. Reading period can be changed via applicationContext.xml.

Please note that coreDynamicPropertiesBean‘ s scope is Singleton by default. It must return new values of dynamic properties at the runtime so its scope should be set Prototype. Otherwise, new values can not be received.

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package com.otv.dynamic.properties.task;
 
import java.util.HashMap;
import java.util.Properties;
 
import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
 
import com.otv.common.SystemConstants;
 
/**
 * Dynamic Properties File Reader Task
 *
 * @author  onlinetechvision.com
 * @since   26 May 2012
 * @version 1.0.0
 *
 */
public class DynamicPropertiesFileReaderTask implements BeanFactoryAware {
 
 private static Logger logger = Logger.getLogger(DynamicPropertiesFileReaderTask.class);
 private Properties coreDynamicPropertiesBean;
 private HashMap<String, String> dynamicPropertiesMap;
 private BeanFactory beanFactory;
 
 /**
  * Starts reading the dynamic properties
  *
  */
 public void start() {
 
  setCoreDynamicPropertiesBean(createCoreDynamicPropertiesBeanInstance());
 
  logger.info("**** Dynamic Properties File Reader Task is being started... ****");
  readConfiguration();
  logger.info("**** Dynamic Properties File Reader Task is stopped... ****");
 }
 
 /**
  * Reads all the dynamic properties
  *
  */
 private void readConfiguration() {
  readMessageContent();
  readMinimumVisitorCount();
  readMaximumVisitorCount();
 }
 
 /**
  * Reads Message_Content dynamic property
  *
  */
 private void readMessageContent() {
  String messageContent = getCoreDynamicPropertiesBean()
                       .getProperty(SystemConstants.DYNAMIC_PROPERTY_MESSAGE_CONTENT,
           SystemConstants.DYNAMIC_PROPERTY_MESSAGE_CONTENT_DEFAULT_VALUE);
 
  if (messageContent.equals("")){
         getDynamicPropertiesMap().put(SystemConstants.DYNAMIC_PROPERTY_MESSAGE_CONTENT,
                  SystemConstants.DYNAMIC_PROPERTY_MESSAGE_CONTENT_DEFAULT_VALUE);
 
         logger.error(SystemConstants.DYNAMIC_PROPERTY_MESSAGE_CONTENT +
                 " value is not found so its default value is set. Default value : " +
                  SystemConstants.DYNAMIC_PROPERTY_MESSAGE_CONTENT_DEFAULT_VALUE);
 
        } else {
         messageContent = messageContent.trim();
         getDynamicPropertiesMap().put(SystemConstants.DYNAMIC_PROPERTY_MESSAGE_CONTENT, messageContent);
         logger.info(SystemConstants.DYNAMIC_PROPERTY_MESSAGE_CONTENT + " : " +
                getDynamicPropertiesMap().get(SystemConstants.DYNAMIC_PROPERTY_MESSAGE_CONTENT));
        }
 }
 
 /**
  * Reads Minimum_Visitor_Count dynamic property
  *
  */
 private void readMinimumVisitorCount() {
  String minimumVisitorCount = getCoreDynamicPropertiesBean()
                                 .getProperty(SystemConstants.DYNAMIC_PROPERTY_MINIMUM_VISITOR_COUNT,
                         SystemConstants.DYNAMIC_PROPERTY_MINIMUM_VISITOR_COUNT_DEFAULT_VALUE).trim();
 
  try {
   if (Integer.parseInt(minimumVisitorCount) > 0){
    getDynamicPropertiesMap().put(SystemConstants.DYNAMIC_PROPERTY_MINIMUM_VISITOR_COUNT,
                                             minimumVisitorCount);
 
    logger.info(SystemConstants.DYNAMIC_PROPERTY_MINIMUM_VISITOR_COUNT + " : " +
           getDynamicPropertiesMap().get(SystemConstants.DYNAMIC_PROPERTY_MINIMUM_VISITOR_COUNT));
   } else {
    getDynamicPropertiesMap().put(SystemConstants.DYNAMIC_PROPERTY_MINIMUM_VISITOR_COUNT,
                                        SystemConstants.DYNAMIC_PROPERTY_MINIMUM_VISITOR_COUNT_DEFAULT_VALUE);
 
    logger.error("Invalid "+SystemConstants.DYNAMIC_PROPERTY_MINIMUM_VISITOR_COUNT +
           " value encountered. Must be greater than 0. Its default value is set.
                                 Default value : "+ SystemConstants.DYNAMIC_PROPERTY_MINIMUM_VISITOR_COUNT_DEFAULT_VALUE);
   }
  } catch (NumberFormatException nfe) {
   logger.error("Invalid "+SystemConstants.DYNAMIC_PROPERTY_MINIMUM_VISITOR_COUNT +
                  " value encountered. Must be numeric!", nfe);
 
   logger.warn(SystemConstants.DYNAMIC_PROPERTY_MINIMUM_VISITOR_COUNT +
                  " default value is set. Default value : " +
                  SystemConstants.DYNAMIC_PROPERTY_MINIMUM_VISITOR_COUNT_DEFAULT_VALUE);
  }
 }
 
 /**
  * Reads Maximum_Visitor_Count dynamic property
  *
  */
 private void readMaximumVisitorCount() {
  String maximumVisitorCount = getCoreDynamicPropertiesBean()
                                  .getProperty(SystemConstants.DYNAMIC_PROPERTY_MAXIMUM_VISITOR_COUNT,
                                            SystemConstants.DYNAMIC_PROPERTY_MAXIMUM_VISITOR_COUNT_DEFAULT_VALUE).trim();
  try {
   if (Integer.parseInt(maximumVisitorCount) > 0){
    getDynamicPropertiesMap().put(SystemConstants.DYNAMIC_PROPERTY_MAXIMUM_VISITOR_COUNT, maximumVisitorCount);
 
    logger.info(SystemConstants.DYNAMIC_PROPERTY_MAXIMUM_VISITOR_COUNT + " : " +
                 getDynamicPropertiesMap()
                        .get(SystemConstants.DYNAMIC_PROPERTY_MAXIMUM_VISITOR_COUNT));
   } else {
    getDynamicPropertiesMap().put(SystemConstants.DYNAMIC_PROPERTY_MAXIMUM_VISITOR_COUNT,
                                  SystemConstants.DYNAMIC_PROPERTY_MAXIMUM_VISITOR_COUNT_DEFAULT_VALUE);
 
    logger.error("Invalid "+SystemConstants.DYNAMIC_PROPERTY_MAXIMUM_VISITOR_COUNT +
                " value encountered. Must be greater than 0. Its default value is set. Default value : " +
                SystemConstants.DYNAMIC_PROPERTY_MAXIMUM_VISITOR_COUNT_DEFAULT_VALUE);
 
   }
  } catch (NumberFormatException nfe) {
   logger.error("Invalid "+SystemConstants.DYNAMIC_PROPERTY_MAXIMUM_VISITOR_COUNT +
                 " value encountered. Must be numeric!", nfe);
 
   logger.warn(SystemConstants.DYNAMIC_PROPERTY_MAXIMUM_VISITOR_COUNT +
                  " default value is set. Default value : " +
                  SystemConstants.DYNAMIC_PROPERTY_MAXIMUM_VISITOR_COUNT_DEFAULT_VALUE);
  }
 }
 
 /**
  * Gets CoreDynamicPropertiesBean
  *
  * @return Properties coreDynamicPropertiesBean
  */
 public Properties getCoreDynamicPropertiesBean() {
  return coreDynamicPropertiesBean;
 }
 
 /**
  * Sets CoreDynamicPropertiesBean
  *
  * @param Properties coreDynamicPropertiesBean
  */
 public void setCoreDynamicPropertiesBean(Properties coreDynamicPropertiesBean) {
  this.coreDynamicPropertiesBean = coreDynamicPropertiesBean;
 }
 
 /**
  * Gets DynamicPropertiesMap
  *
  * @return HashMap dynamicPropertiesMap
  */
 public HashMap<String, String> getDynamicPropertiesMap() {
  return dynamicPropertiesMap;
 }
 
 /**
  * Sets DynamicPropertiesMap
  *
  * @param HashMap dynamicPropertiesMap
  */
 public void setDynamicPropertiesMap(HashMap<String, String> dynamicPropertiesMap) {
  this.dynamicPropertiesMap = dynamicPropertiesMap;
 }
 
 /**
  * Gets a new instance of CoreDynamicPropertiesBean
  *
  * @return Properties CoreDynamicPropertiesBean
  */
 public Properties createCoreDynamicPropertiesBeanInstance() {
  return  (Properties) this.beanFactory.getBean(SystemConstants.BEAN_NAME_CORE_DYNAMIC_PROPERTIES_BEAN);
 }
 
 /**
  * Sets BeanFactory
  *
  * @param BeanFactory beanFactory
  */
 public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
  this.beanFactory = beanFactory;
 }
 
}

STEP 7 : CREATE Application CLASS

Application Class starts the project.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.otv.starter;
 
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import com.otv.common.SystemConstants;
 
/**
 * Application Starter Class
 *
 * @author  onlinetechvision.com
 * @since   26 May 2012
 * @version 1.0.0
 *
 */
public class Application {
 
 /**
  * Main method of the Application
  *
  */
 public static void main(String[] args) {
  new ClassPathXmlApplicationContext(SystemConstants.APPLICATION_CONTEXT_FILE_NAME);
 }
}

STEP 8 : RUN PROJECT

If Application Class is run, following console logs are shown :

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
26.05.2012 17:25:09  INFO (DefaultLifecycleProcessor.java:334) - Starting beans in phase 2147483647
26.05.2012 17:25:09  INFO (SchedulerFactoryBean.java:648) - Starting Quartz Scheduler now
26.05.2012 17:25:09  INFO (PropertiesLoaderSupport.java:177) - Loading properties file from class path resource [DynamicPropertiesFile.properties]
26.05.2012 17:25:09  INFO (DynamicPropertiesFileReaderTask.java:36) - **** Dynamic Properties File Reader Task is being started... ****
26.05.2012 17:25:09  INFO (DynamicPropertiesFileReaderTask.java:63) - Message_Content : Welcome Visitor
26.05.2012 17:25:09  INFO (DynamicPropertiesFileReaderTask.java:76) - Minimum_Visitor_Count : 1
26.05.2012 17:25:09  INFO (DynamicPropertiesFileReaderTask.java:96) - Maximum_Visitor_Count : 10
26.05.2012 17:25:09  INFO (DynamicPropertiesFileReaderTask.java:38) - **** Dynamic Properties File Reader Task is stopped... ****
 
26.05.2012 17:26:09  INFO (PropertiesLoaderSupport.java:177) - Loading properties file from class path resource [DynamicPropertiesFile.properties]
26.05.2012 17:26:09  INFO (DynamicPropertiesFileReaderTask.java:36) - **** Dynamic Properties File Reader Task is being started... ****
26.05.2012 17:26:09  INFO (DynamicPropertiesFileReaderTask.java:63) - Message_Content : Welcome Visitor, Bruce!
26.05.2012 17:26:09  INFO (DynamicPropertiesFileReaderTask.java:76) - Minimum_Visitor_Count : 2
26.05.2012 17:26:09  INFO (DynamicPropertiesFileReaderTask.java:96) - Maximum_Visitor_Count : 20
26.05.2012 17:26:09  INFO (DynamicPropertiesFileReaderTask.java:38) - **** Dynamic Properties File Reader Task is stopped... ****

STEP 9 : DOWNLOAD

OTV_SpringDynamicPropertyManagement

REFERENCES :

Spring Framework Reference 3.x

Reference: Dynamic Property Management in Spring from our JCG partner Eren Avsarogullari at the Online Technology Vision blog.

Subscribe
Notify of
guest


This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button