弹簧cron每30分钟一次。

时间:2022-05-30 04:25:57

I have following Spring job to run after every 30 minutes. Please check my cron expression, is that correct?

我每隔30分钟就会有一份春季的工作。请检查我的cron表达式,对吗?

"0 0 0 * * 30"

"0 0 * * 30"


Here is a full cron job definition from the related Spring configuration file:

以下是来自相关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>

4 个解决方案

#1


97  

According to the Quartz-Scheduler Tutorial It should be value="0 0/30 * * * ?"

根据Quartz-Scheduler教程,它应该是value="0 0/30 * * * ?"

The field order of the cronExpression is

cronExpression的字段顺序是。

1.Seconds

1.秒

2.Minutes

2.分钟

3.Hours

3.小时

4.Day-of-Month

4.月的日期

5.Month

5.月

6.Day-of-Week

6.一周中的第几天

7.Year (optional field)

7所示。(可选字段)

Ensure you have at least 6 parameters or you will get an error (year is optional)

确保您至少有6个参数,否则您将会得到错误(年份是可选的)

#2


37  

<property name="cronExpression" value="0 0/30 * * * ?" />

#3


32  

Graphically, the cron syntax for Quarz is (source):

图形化的,Quarz的cron语法是(源):

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

So if you want to run a command every 30 minutes you can say either of these:

因此,如果你想每30分钟运行一个命令,你可以这样说:

0 0/30 * * * * ?
0 0,30 * * * * ?

You can check crontab expressions using either of these:

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

  • crontab.guru — (disclaimer: I am not related to that page at all, only that I find it very useful). This page uses UNIX style of cron that does not have seconds in it, while Spring does as the first field.
  • crontab。大师(声明:我不相关的页面,只有我发现它非常有用)。这个页面使用的是UNIX风格的cron,它没有几秒钟的时间,而Spring作为第一个字段。
  • Cron Expression Generator & Explainer - Quartz — cron formatter, allowing seconds also.
  • Cron表达式生成器和解释器-石英- Cron格式化程序,允许秒。

#4


1  

in web app java spring what worked for me

在web应用程序java spring中,什么对我有效。

0 0/30 * * * ?

0/30 * * * ?

This will trigger on for example 10:00AM then 10:30AM etc...

这将触发例如10:00AM然后10:30AM等等…

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

I dont know why but this is working on my local develop and production, but other changes if i made i have to be careful because it may work local and on develop but not on production

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

#1


97  

According to the Quartz-Scheduler Tutorial It should be value="0 0/30 * * * ?"

根据Quartz-Scheduler教程,它应该是value="0 0/30 * * * ?"

The field order of the cronExpression is

cronExpression的字段顺序是。

1.Seconds

1.秒

2.Minutes

2.分钟

3.Hours

3.小时

4.Day-of-Month

4.月的日期

5.Month

5.月

6.Day-of-Week

6.一周中的第几天

7.Year (optional field)

7所示。(可选字段)

Ensure you have at least 6 parameters or you will get an error (year is optional)

确保您至少有6个参数,否则您将会得到错误(年份是可选的)

#2


37  

<property name="cronExpression" value="0 0/30 * * * ?" />

#3


32  

Graphically, the cron syntax for Quarz is (source):

图形化的,Quarz的cron语法是(源):

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

So if you want to run a command every 30 minutes you can say either of these:

因此,如果你想每30分钟运行一个命令,你可以这样说:

0 0/30 * * * * ?
0 0,30 * * * * ?

You can check crontab expressions using either of these:

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

  • crontab.guru — (disclaimer: I am not related to that page at all, only that I find it very useful). This page uses UNIX style of cron that does not have seconds in it, while Spring does as the first field.
  • crontab。大师(声明:我不相关的页面,只有我发现它非常有用)。这个页面使用的是UNIX风格的cron,它没有几秒钟的时间,而Spring作为第一个字段。
  • Cron Expression Generator & Explainer - Quartz — cron formatter, allowing seconds also.
  • Cron表达式生成器和解释器-石英- Cron格式化程序,允许秒。

#4


1  

in web app java spring what worked for me

在web应用程序java spring中,什么对我有效。

0 0/30 * * * ?

0/30 * * * ?

This will trigger on for example 10:00AM then 10:30AM etc...

这将触发例如10:00AM然后10:30AM等等…

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

I dont know why but this is working on my local develop and production, but other changes if i made i have to be careful because it may work local and on develop but not on production

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