![](/img/trans.png)
[英]java Quartz Scheduler cron expression for every after 30 minutes quarter past the hours
[英]Spring cron expression for every after 30 minutes
我每 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>
根据Quartz-Scheduler Tutorial它应该是value="0 0/30 * * * ?"
cronExpression 的字段顺序是
1.秒
2分钟
3小时
4.月份
5.月
6.星期几
7.Year(可选字段)
确保你至少有 6 个参数,否则你会得到一个错误(年份是可选的)
从图形上看,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
因此,如果您想每 30 分钟运行一次命令,您可以使用以下任一命令:
0 0/30 * * * * ?
0 0,30 * * * * ?
您可以使用以下任一方法检查 crontab 表达式:
<property name="cronExpression" value="0 0/30 * * * ?" />
在我的 Java Spring Web 应用程序中,这对我有用:
cron="0 0/30 * * * ?"
这将在例如上午 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">
<!-- every thirty minutes -->
<task:scheduled ref="expireCronJob" method="runTask" cron="0 0/30 * * * ?"/>
</task:scheduled-tasks>
</beans>
</beans>
我不知道为什么,但这适用于我的本地开发和生产,但是如果我进行了其他更改,我必须小心,因为它可能适用于本地和开发,但不适用于生产。
如果有人使用 @Sceduled 这可能对你有用。
@Scheduled(cron = "${name-of-the-cron:0 0/30 * * * ?}")
这对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.