Spring 3如何使用属性文件的间隔运行计划任务

DMa*_*Mac 3 java spring

我使用Spring 3创建一个计划任务.

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

Spring Context文件:

<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>
Run Code Online (Sandbox Code Playgroud)

问题是固定速率值中不允许$ {connector.connectionAttemptDelayMillis}.如果我要将数字放在其位置,这将工作正常,但我需要此值来自加载的属性文件.

任何帮助深表感谢.

小智 6

你可以这样做(下行,你只能每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
    }
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

属性

connector.user = someuser
connector.password = somepassword
connector.connectionAttemptDelayMillis = 5000
connector.connectionAttemptCron = */5 * * * * *
Run Code Online (Sandbox Code Playgroud)

这有效,因为cron需要一个字符串.

参考