我正在使用 Spring 3 创建计划任务。
我必须使用基于 XML 的连接来配置它,并且希望计划任务以属性文件中设置的时间间隔运行。
Spring 上下文文件:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.1.xsd"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task">
<context:property-placeholder location="classpath:connector.properties"/>
<task:scheduler id="connectorScheduler" pool-size="10"/>
<task:scheduled-tasks scheduler="connectorScheduler">
<task:scheduled ref="connector" method="checkConnection" fixed-rate="${connector.connectionAttemptDelayMillis}"/>
</task:scheduled-tasks>
<bean id="connector" class="com.test.Connector" scope="singleton">
<constructor-arg index="0" value="${connector.user}"/>
<constructor-arg index="1" value="${connector.password}"/>
<constructor-arg index="2" value="${connector.connectionAttemptDelayMillis}"/>
</bean>
</beans>
问题是 ${connector.connectionAttemptDelayMillis} 在固定速率值中是不允许的。如果我要在其位置放置一个数字,这会很好地工作,但我需要这个值来自加载的属性文件。
非常感谢任何帮助。
最佳答案
你可以那样做(缺点是,你只能每隔 1 秒或更长时间执行一次任务):
连接器类
package com.test.Connector;
@Component
public class Connector {
@Value("${connector.user}")
private String user;
@Value("${connector.password}")
private String password;
@Value("${connector.connectionAttemptDelayMillis:0}")
private long attemptDelayMillis;
@Scheduled(cron = "${connector.connectionAttemptCron}")
public checkConnection() {
// do the check
}
}
XML 上下文
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
<context:annotation-config />
<context:component-scan base-package="com.test" />
<context:property-placeholder location="classpath:connector.properties" />
<task:scheduler id="connectorScheduler" pool-size="10"/>
<task:annotation-driven scheduler="connectorScheduler" />
</beans>
属性
connector.user = someuser
connector.password = somepassword
connector.connectionAttemptDelayMillis = 5000
connector.connectionAttemptCron = */5 * * * * *
这是可行的,因为 cron
需要一个字符串。
引用资料
关于java - Spring 3如何以属性文件的间隔运行计划任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13917137/