java - Spring 3如何以属性文件的间隔运行计划任务

标签 java spring

我正在使用 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/

相关文章:

java - Spring、JdbcTemplate不能使用Select List中未指定的Column吗?

Spring MVC : how can I re-load the logged-in user information from the database on each Thymeleaf page?

java - 如何在 Java8 流/过滤器中使用非最终变量?

java - 找不到 URL 内容时更快失败,howto

java - 使用 0xFF 而不是 0xFFFFFFFF 进行字节初始化

spring - 扩展 Spring Boot 加载程序

java - Spring bean 在集合上设置销毁方法

spring - 如果远程存储库不可用,如何为 Spring Cloud Config 服务设置本地后备配置?

java - 合并排序返回一个由第一个条目的多个重复项组成的 ArrayList,而不是已排序的 ArrayList

java - Android - 当我在设备上运行它时无法访问网络服务,但使用 AVD 它可以正常工作。为什么?