Java 使用cron的Spring调度
我不熟悉春季日程安排。经过一些研究,我发现可以使用cron表达式来实现。我有两个问题要问论坛。我需要安排一个每天晚上8点运行的任务。。下面的cron表达式是否每天晚上8点安排任务Java 使用cron的Spring调度,java,spring,spring-mvc,cron,Java,Spring,Spring Mvc,Cron,我不熟悉春季日程安排。经过一些研究,我发现可以使用cron表达式来实现。我有两个问题要问论坛。我需要安排一个每天晚上8点运行的任务。。下面的cron表达式是否每天晚上8点安排任务 @Scheduled(cron="0 0 20 * * ?") 出于测试目的,我计划每5分钟运行一次任务。使用的表达是 @Scheduled(cron="0 0/5 * * * *") 但我发现上面的表达式每5分钟就会触发一次我的方法。我的代码每5分钟运行两次。我不知道为什么它每5分钟运行两次 下面是我的代码
@Scheduled(cron="0 0 20 * * ?")
出于测试目的,我计划每5分钟运行一次任务。使用的表达是
@Scheduled(cron="0 0/5 * * * *")
但我发现上面的表达式每5分钟就会触发一次我的方法。我的代码每5分钟运行两次。我不知道为什么它每5分钟运行两次
下面是我的代码
@Scheduled(cron="0 0/5 * * * *")
public void demo(){
counter++;
System.out.println("Method "+new Date()+" counter "+counter); }
上述代码的输出为:
Method Thu Nov 05 22:26:00 IST 2015 counter 1
Method Thu Nov 05 22:26:00 IST 2015 counter 2
我的静态变量计数器说这个方法每5分钟调用两次
我的spring.xml如下所示
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.controller" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean id="Daily" class="com.scheduler.Daily">
</bean>
<mvc:resources mapping="/resources/**" location="/resources/"
cache-period="31556926"/>
<mvc:annotation-driven />
<task:annotation-driven />
</beans>
感谢您的帮助。是的,我能够解决调度程序运行两次的问题。我的另一个问题是安排每8点有正确的cron表达式吗?是的,它似乎是正确的。顺便说一句,你可以使用,粘贴你的cron表达式,它会显示日期,当它将运行i检查和它的权利。欣赏