每30分钟后弹簧cron表达

d-m*_*man 54 java cron spring expression

我每隔30分钟跟随Spring工作.请检查我的cron表达,这是正确的吗?

"0 0 0**30"


以下是相关Spring 配置文件中的完整cron作业定义:

<bean id="autoWeblogPingTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
    <property name="jobDetail" ref="jobDetailForWeblogPing"/>
    <!-- run every 35 minutes -->
    <property name="cronExpression" value="0 0 0 * * 30" />
</bean>
Run Code Online (Sandbox Code Playgroud)

sta*_*ker 118

根据Quartz-Scheduler教程 应该如此value="0 0/30 * * * ?"

cronExpression的字段顺序是

1.Seconds

2分钟

3小时

4.Day的日

5.Month

6.Day的星期 -

7.Year(可选字段)

确保您至少有6个参数或者您将收到错误(年份是可选的)

  • Quartz根据他们的文档需要一个额外的字段.我相信它会是'0 0/30***?'.一般方法是正确的 (5认同)
  • 石英网址已死 (3认同)

fed*_*qui 55

从图形上看,Quarz的cron语法是(source):

+-------------------- second (0 - 59)
|  +----------------- minute (0 - 59)
|  |  +-------------- hour (0 - 23)
|  |  |  +----------- day of month (1 - 31)
|  |  |  |  +-------- month (1 - 12)
|  |  |  |  |  +----- day of week (0 - 6) (Sunday=0 or 7)
|  |  |  |  |  |  +-- year [optional]
|  |  |  |  |  |  |
*  *  *  *  *  *  * command to be executed 
Run Code Online (Sandbox Code Playgroud)

因此,如果您想每30分钟运行一次命令,您可以说以下任何一种:

0 0/30 * * * * ?
0 0,30 * * * * ?
Run Code Online (Sandbox Code Playgroud)

您可以使用以下任一方法检查crontab表达式:

  • 请注意crontab.guru使用没有秒的cron的Unix风格.Spring cron使用秒,因此第一个字段是秒. (3认同)
  • 我也喜欢crontab.guru,但在这种情况下,我认为它要好得多:https://www.freeformatter.com/cron-expression-generator-quartz.html (2认同)

d-m*_*man 46

<property name="cronExpression" value="0 0/30 * * * ?" />
Run Code Online (Sandbox Code Playgroud)


sha*_*eef 8

在网络应用程序java春天什么对我有用

cron="0 0/30 * * * ?"
Run Code Online (Sandbox Code Playgroud)

这将触发例如上午10:00然后上午10:30等...

<?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:beans="http://www.springframework.org/schema/beans"
       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/task 
    http://www.springframework.org/schema/task/spring-task.xsd">

    <beans profile="cron">
        <bean id="executorService" class="java.util.concurrent.Executors" factory-method="newFixedThreadPool">
            <beans:constructor-arg value="5" />
        </bean>

        <task:executor id="threadPoolTaskExecutor" pool-size="5" />
        <task:annotation-driven executor="executorService" />

        <beans:bean id="expireCronJob" class="com.cron.ExpireCron"/>

        <task:scheduler id="serverScheduler" pool-size="5"/>
        <task:scheduled-tasks scheduler="serverScheduler">
            <task:scheduled ref="expireCronJob" method="runTask" cron="0 0/30 * * * ?"/> <!-- every thirty minute -->
        </task:scheduled-tasks>

    </beans>

</beans>
Run Code Online (Sandbox Code Playgroud)

我不知道为什么,但这是在我的本地开发和生产,但其他变化,如果我使我必须小心,因为它可能工作本地和开发但不生产


Sha*_*eed 7

如果有人正在使用@Scheduled,这可能对你有用。

@Scheduled(cron = "${name-of-the-cron:0 0/30 * * * ?}")

这对我有用。