Quartz Spring CronTrigger 触发次数超过配置次数

发布于 2024-09-10 14:12:09 字数 1138 浏览 11 评论 0原文

我有一个用于作业“digestJob”的 cronTrigger:

<bean id="digestCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> 
    <property name="jobDetail" ref="digestJob" />
    <property name="cronExpression" value="0 35 15 * * ?" />
</bean>

这是我的 SchedulerFactoryBean 配置:

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="digestCronTrigger" />   
            </list>
        </property>
    </bean>

问题是,digestCronTrigger 应该每天下午 5:35 触发一次,但它在指定时间被​​触发两次。但是,当我使用 SimpleTrigger 时:

<bean id="digestTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
        <property name="jobDetail" ref="digestJob" />
        <property name="startDelay" value="0" />
        <property name="repeatInterval" value="10000" />
    </bean>

一切正常,并且触发器每 10 秒触发一次。为什么digestCronTrigger被触发两次?我的 cron 表达式有问题吗,或者我缺少任何属性吗?任何帮助将不胜感激。

I have a cronTrigger for a job "digestJob":

<bean id="digestCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> 
    <property name="jobDetail" ref="digestJob" />
    <property name="cronExpression" value="0 35 15 * * ?" />
</bean>

Here is my schedulerFactoryBean configuration:

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="digestCronTrigger" />   
            </list>
        </property>
    </bean>

The problem is, the digestCronTrigger is supposed to be fired ONCE everyday at 5:35 PM, but it is being fired TWICE at the specified time. However, when I use SimpleTrigger:

<bean id="digestTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
        <property name="jobDetail" ref="digestJob" />
        <property name="startDelay" value="0" />
        <property name="repeatInterval" value="10000" />
    </bean>

everything works fine and the trigger is fired exactly once every 10 seconds. Why digestCronTrigger is being fired twice? Is there something wrong with my cron expression, or is there any property that I am missing? Any help will be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

雨巷深深 2024-09-17 14:12:09

我在 springsource 论坛上发布了 同样的问题,在那里我得到了帮助来找出原因问题背后:
我加载应用程序上下文两次。后来我从这篇文章中找到了如何避免加载上下文两次。现在一切进展顺利。

I posted same question at springsource forums where I got help to figure out the cause behind the problem:
I was loading the application context twice. Later I found from this post how to avoid loading the context twice. Now things are working fine.

酒几许 2024-09-17 14:12:09

如果您在启动应用程序时创建触发器并在quartz.properties 文件中使用数据源,也可能会发生这种情况。每次启动服务器时,它都会向 QRTZ_CRON_TRIGGERSQRTZ_TRIGGERS 表写入一个新触发器,并在每次重新启动时使用所有触发器。

This can also happen if you're creating a trigger when you're starting your application and are using a datasource in your quartz.properties file. Every time you start your server it will write a new trigger to the QRTZ_CRON_TRIGGERS and QRTZ_TRIGGERS tables and use all of them on each restart.

毁梦 2024-09-17 14:12:09

试试这个:

    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="cronTrigger" />
            </list>
        </property>
    </bean>

    <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
      <property name="targetObject" ref="actionObject" />
      <property name="targetMethod" value="actionMethod" />
    </bean>

    <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail" ref="jobDetail"/>
        <property name="cronExpression" value="0 15 17 * * ?"/>
    </bean>

Try this:

    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="cronTrigger" />
            </list>
        </property>
    </bean>

    <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
      <property name="targetObject" ref="actionObject" />
      <property name="targetMethod" value="actionMethod" />
    </bean>

    <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail" ref="jobDetail"/>
        <property name="cronExpression" value="0 15 17 * * ?"/>
    </bean>
情何以堪。 2024-09-17 14:12:09

我也遇到这个问题,终于找到根本原因了。
我们的服务器 Tomcat 设置存在一些问题 /.../Tomcat/conf/server.xml

我们的server.xml如下所示,同一个tomcat中有2个定期运行相同的cronJob,但写入相同的日志文件和 db 同时,所以它按计划触发两次,但如果我们手动运行 cronjob,它只会触发一次......

解决方案当然是删除一台主机,希望这能有所帮助


<Server port="8005" shutdown="SHUTDOWN">
    ...
      <Service name="Catalina">
    ...
        <Engine name="Catalina" defaultHost="localhost">
            <Host name="localhost"  appBase="webapps"
                unpackWARs="true" autoDeploy="true">
                <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
                       prefix="localhost_access_log." suffix=".txt"
                       pattern="%h %l %u %t "%r" %s %b" />
            </Host>
            <Host name="<domain_name>" appBase="webapps" unpackWARs="true" autoDeploy="true">
                <Alias><domain_name></Alias>
                <Valve className="org.apache.catalina.valves.AccessLogValve"
                     directory="logs"   prefix="localhost_access_log." suffix=".txt"
                     pattern="%h %l %u %t "%r" %s %b" resolveHosts="false" />
            </Host>
        </Engine>
      </Service>
    </Server>

I also have this issue and we finally found the root cause.
There is some problem in our server Tomcat setting /.../Tomcat/conf/server.xml

Our server.xml looks like below, There is 2 <Host> in the same tomcat that run the same cronJob regularly, but writing to the same log file and db at same time, so it fires two times by schedule, but if we run cronjob manually, it only fire once...

The solution, of course, is to remove one Host, hope this help


<Server port="8005" shutdown="SHUTDOWN">
    ...
      <Service name="Catalina">
    ...
        <Engine name="Catalina" defaultHost="localhost">
            <Host name="localhost"  appBase="webapps"
                unpackWARs="true" autoDeploy="true">
                <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
                       prefix="localhost_access_log." suffix=".txt"
                       pattern="%h %l %u %t "%r" %s %b" />
            </Host>
            <Host name="<domain_name>" appBase="webapps" unpackWARs="true" autoDeploy="true">
                <Alias><domain_name></Alias>
                <Valve className="org.apache.catalina.valves.AccessLogValve"
                     directory="logs"   prefix="localhost_access_log." suffix=".txt"
                     pattern="%h %l %u %t "%r" %s %b" resolveHosts="false" />
            </Host>
        </Engine>
      </Service>
    </Server>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文