簡體   English   中英

為什么我的Spring @Scheduled任務停止?

[英]Why does my Spring @Scheduled task stop?

我正在使用Spring的@Scheduled功能在使用Spring 3.1.1的WAS 8.5環境中每天每8小時運行一次任務。 它會啟動並運行一段時間,然后無故停止。 我的日志中沒有任何內容指示失敗。 任何想法可能導致這種情況發生嗎? 現在已經發生了幾次。 這不是由於服務器重啟等引起的。

package com.my.project.scheduler;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.context.ContextLoaderListener;

import com.my.project.scheduler.model.RetransmitEvent;

public class RetransmitTimer {    

    private static final Logger LOG = Logger.getLogger( RetransmitTimer.class );

    @Scheduled( cron = "${retransmit.cron.interval.1}" )
    public void retransmitSchedule1() {
        retransmit();
    }
    @Scheduled( cron = "${retransmit.cron.interval.2}" )
    public void retransmitSchedule2() {
        retransmit();
    }
    @Scheduled( cron = "${retransmit.cron.interval.3}" )
    public void retransmitSchedule3() {
        retransmit();
    }
    private void retransmit() {
        LOG.info( "############# AUTOMATIC RETRANSMIT EXECUTING ##############" );        
        try
        {
            [code to gather retransmitEvents omitted]
            if ( retransmitEvents.size() > 0 ) {
                LOG.info( "Total retransmits found=" + retransmitEvents.size() );
                submitRetransmits( retransmitEvents );
            }
        }    
        finally
        {    [method cleanup, etc]
            LOG.info( "############# AUTOMATIC RETRANSMIT COMPLETED ##############" );
        }
    }

    private void submitRetransmits( List<RetransmitEvent> retransmitEvents ) {
        ApplicationContext context = ContextLoaderListener.getCurrentWebApplicationContext();
        DataSource ds1 = (DataSource) context.getBean("jdbc/DS1");
        DataSource ds2 = (DataSource) context.getBean("jdbc/DS2");
        [rest of the code to process database updates omitted]
    }
}

我的applicationContext.xml

<?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:context="http://www.springframework.org/schema/context"
    xmlns:task="http://www.springframework.org/schema/task"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.1.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-3.0.xsd">

    <context:property-placeholder location="classpath:my-scheduler.properties" />
    <context:annotation-config/>
    <context:component-scan base-package="com.my.project" />

    <task:annotation-driven/> <!-- for the Scheduled annotations in the timer code -->

    <bean id="retransmitTimer" class="com.my.project.scheduler.RetransmitTimer" scope="singleton"/>

    <!-- The following Spring beans replace what would normally go into the web.xml file -->
    <!-- They're for the attachmentViewer.jar attachment retry process -->
    <bean id="jdbc/DS1" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="jdbc/DS1" />
        <property name="lookupOnStartup" value="false" />
        <property name="cache" value="true" />
        <property name="proxyInterface" value="javax.sql.DataSource" />
    </bean>

    <bean id="jdbc/DS2" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="jdbc/DS2" />
        <property name="lookupOnStartup" value="false" />
        <property name="cache" value="true" />
        <property name="proxyInterface" value="javax.sql.DataSource" />
    </bean>
</beans>

my-scheduler.properties

# Retransmit preferences
#   CRON parms: Second Minute Hour Day-of-Month Month Day-of-the-Week
#   So, we're set to run everyday at the top of the hour at 4am, 12pm and 8pm
retransmit.cron.interval.1=0 0 4 * * ?
retransmit.cron.interval.2=0 0 12 * * ?
retransmit.cron.interval.3=0 0 20 * * ?

我知道我可以在一行中每8小時設置一次CRON parm,但是用戶可以根據自己的需要進行創新,所以我將3個條目保留了下來。

這是日志片段,顯示它已經運行了幾天。 該服務器已於下午3/4重新啟動,此后一直未重新啟動。

[2016-03-04 20:00:00,047] [pool-7-thread-1] INFO  com.excellus.blue2.scheduler.RetransmitTimer - ############# AUTOMATIC RETRANSMIT EXECUTING ##############
[2016-03-04 20:00:01,853] [pool-7-thread-1] INFO  com.excellus.blue2.scheduler.RetransmitTimer - ############# AUTOMATIC RETRANSMIT COMPLETED ##############
[2016-03-05 04:00:00,111] [pool-7-thread-1] INFO  com.excellus.blue2.scheduler.RetransmitTimer - ############# AUTOMATIC RETRANSMIT EXECUTING ##############
[2016-03-05 04:00:00,531] [pool-7-thread-1] INFO  com.excellus.blue2.scheduler.RetransmitTimer - ############# AUTOMATIC RETRANSMIT COMPLETED ##############
[2016-03-05 12:00:00,167] [pool-7-thread-1] INFO  com.excellus.blue2.scheduler.RetransmitTimer - ############# AUTOMATIC RETRANSMIT EXECUTING ##############
[2016-03-05 12:00:00,172] [pool-7-thread-1] INFO  com.excellus.blue2.scheduler.RetransmitTimer - ############# AUTOMATIC RETRANSMIT COMPLETED ##############
[2016-03-05 20:00:00,197] [pool-7-thread-1] INFO  com.excellus.blue2.scheduler.RetransmitTimer - ############# AUTOMATIC RETRANSMIT EXECUTING ##############
[2016-03-05 20:00:00,312] [pool-7-thread-1] INFO  com.excellus.blue2.scheduler.RetransmitTimer - ############# AUTOMATIC RETRANSMIT COMPLETED ##############
[2016-03-06 04:00:00,200] [pool-7-thread-1] INFO  com.excellus.blue2.scheduler.RetransmitTimer - ############# AUTOMATIC RETRANSMIT EXECUTING ##############
[2016-03-06 04:00:00,438] [pool-7-thread-1] INFO  com.excellus.blue2.scheduler.RetransmitTimer - ############# AUTOMATIC RETRANSMIT COMPLETED ##############
[2016-03-06 12:00:00,193] [pool-7-thread-1] INFO  com.excellus.blue2.scheduler.RetransmitTimer - ############# AUTOMATIC RETRANSMIT EXECUTING ##############
[2016-03-06 12:00:00,198] [pool-7-thread-1] INFO  com.excellus.blue2.scheduler.RetransmitTimer - ############# AUTOMATIC RETRANSMIT COMPLETED ##############
[2016-03-06 20:00:00,193] [pool-7-thread-1] INFO  com.excellus.blue2.scheduler.RetransmitTimer - ############# AUTOMATIC RETRANSMIT EXECUTING ##############
[2016-03-06 20:00:00,565] [pool-7-thread-1] INFO  com.excellus.blue2.scheduler.RetransmitTimer - ############# AUTOMATIC RETRANSMIT COMPLETED ##############
[2016-03-07 04:00:00,201] [pool-7-thread-1] INFO  com.excellus.blue2.scheduler.RetransmitTimer - ############# AUTOMATIC RETRANSMIT EXECUTING ##############
[2016-03-07 04:00:00,207] [pool-7-thread-1] INFO  com.excellus.blue2.scheduler.RetransmitTimer - ############# AUTOMATIC RETRANSMIT COMPLETED ##############
[2016-03-07 12:00:00,204] [pool-7-thread-1] INFO  com.excellus.blue2.scheduler.RetransmitTimer - ############# AUTOMATIC RETRANSMIT EXECUTING ##############
[2016-03-07 12:00:00,260] [pool-7-thread-1] INFO  com.excellus.blue2.scheduler.RetransmitTimer - ############# AUTOMATIC RETRANSMIT COMPLETED ##############
[2016-03-07 20:00:00,197] [pool-7-thread-1] INFO  com.excellus.blue2.scheduler.RetransmitTimer - ############# AUTOMATIC RETRANSMIT EXECUTING ##############
[2016-03-07 20:00:01,405] [pool-7-thread-1] INFO  com.excellus.blue2.scheduler.RetransmitTimer - ############# AUTOMATIC RETRANSMIT COMPLETED ##############
[2016-03-08 04:00:00,198] [pool-7-thread-1] INFO  com.excellus.blue2.scheduler.RetransmitTimer - ############# AUTOMATIC RETRANSMIT EXECUTING ##############
[2016-03-08 04:00:00,568] [pool-7-thread-1] INFO  com.excellus.blue2.scheduler.RetransmitTimer - ############# AUTOMATIC RETRANSMIT COMPLETED ##############

3/8 4am之后沒有更多條目。 我還應注意,在此期間未找到會調用子方法submitRetransmits()的重新發送條目。

知道為什么這些計划的任務會停止嗎? 提前致謝。

嘗試發送kill -3來查看堆棧跟蹤。 可能某些進程是堆棧,無法繼續。 您還可以檢查傳輸中是否有超時,如果沒有,則最好進行設置。

原來我的調度程序應用程序一直在運行。 這是一個日志記錄問題。 我將日志記錄與其他項目分開,它表明它仍然可以正常運行。 感謝您的所有建議。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM