简体   繁体   English

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

[英]Spring 3 how to run a scheduled task with an interval from property file

I am using Spring 3 to create a scheduled-task. 我使用Spring 3创建一个计划任务。

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

Spring Context file: 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>

The problem is that the ${connector.connectionAttemptDelayMillis} is not allowed in the fixed-rate value. 问题是固定速率值中不允许$ {connector.connectionAttemptDelayMillis}。 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): 你可以这样做(下行,你只能每1秒或更多时间完成你的任务):

Connector class 连接器类

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 context 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>

Properties 属性

connector.user = someuser
connector.password = somepassword
connector.connectionAttemptDelayMillis = 5000
connector.connectionAttemptCron = */5 * * * * *

This works because cron need a string. 这是有效的,因为cron需要一个字符串。

References 参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM