3

我正在使用 Spring 3 创建计划任务。

我必须使用基于 XML 的接线来配置它,并且希望计划任务以属性文件中设置的间隔运行。

弹簧上下文文件:

<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}。如果我要在其位置放置一个数字,这将正常工作,但我需要这个值来自加载的属性文件。

任何帮助深表感谢。

4

1 回答 1

5

您可以这样做(缺点,您只能每 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需要一个字符串。

参考

于 2012-12-17T15:59:37.693 回答