动态加载属性文件与SpringQuartz的应用

前面写了一篇关于动态加载属性文件的例子,参考下面的这个帖子:

http://zhangzhenting.iteye.com/blog/600269

 

针对动态加载属性文件,再次给出一个更简单的例子,去掉了config.xml文件,当然配置也就没有那么灵活了。对于一些简单的配置可以采用这种方式;

在该例子中引入了Spring Quartz的使用,通过quartz来进行测试。

 

属性动态加载应用:

 

public class PropertiesHander {

	private static PropertiesConfiguration configuration = null;

	private static final String CONFIG_FILE = "main.properties";

	private PropertiesHander() {
	}

	public static PropertiesConfiguration getIntance() {
		if (configuration == null) {
			try {
				configuration = new PropertiesConfiguration(CONFIG_FILE);
			} catch (ConfigurationException e) {
				e.printStackTrace();
			}
			FileChangedReloadingStrategy strategy = new FileChangedReloadingStrategy();
			// 文件自动重加载延时设置为30秒,只有当文件修改之后才会重新加载(根据文件最后修改时间判断)
			strategy.setRefreshDelay(30000);
			configuration.setReloadingStrategy(strategy);
		}
		return configuration;
	}

	public static String getProperty(String propertyName, String defaultValue) {
		return getIntance().getString(propertyName, defaultValue);
	}

	public static String getProperty(String propertyName) {
		return getIntance().getString(propertyName);
	}
}

 

Spring Quartz 配置:

	<!-- 定时任务 -->
	<bean id="taskService" class="com.zzt.application.service.TaskService" />

	<!--定义目标bean和bean中的方法 -->
	<bean id="jobtask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject">
			<ref local="taskService" />
		</property>
		<property name="targetMethod">
			<value>execute</value>
		</property>
		<!--定义task为单线程-->
		<property name="concurrent" value="false" />
	</bean>
	<!--定义job触发的时间-->
	<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail">
			<ref bean="jobtask" />
		</property>
		<property name="cronExpression">
			<value>5/5 * * * * ?</value>
		</property>
	</bean>

	<!--总管理-->
	<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>
				<ref local="cronTrigger" />
			</list>
		</property>
	</bean>

 需要引用的包:

commons-collections.jar

commons-configuration-1.6.jar

commons-lang.jar

commons-logging.jar

quartz-1.5.2.jar

spring.jar

 

附件给出该测试的源码。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值