I am using Spring 3 to create a scheduled-task.
I have to use XML based wiring to configure it, and would like scheduled task to run with an interval that is set in a properties file.
Spring Context file:
<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>
The problem is that the ${connector.connectionAttemptDelayMillis} is not allowed in the fixed-rate value. This will work fine if i was to put a number in its place, but i need this value to come from a loaded property file.
Any help is much appreciated.
You could do it like that (downside, you can only do your task every 1s or more):
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 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 * * * * *
This works because cron
need a string.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.