计时器在java中执行两次..

时间:2014-05-23 08:12:15

标签: java timer timertask

面对可怕的问题......

我有两个定时器类java.util.Timer,scheduledReportTimer发送每日电子邮件和scheduleImmediateReportTimer,每10分钟发送一次电子邮件。     scheduledReportTimer工作得非常好。     scheduleImmediateReportTimer每10分钟运行一次,但它似乎在10分钟内运行两次,或者为scheduleImmediateReportTimer创建了两个线程(我不确定它究竟是什么)     但它正在调用两次电子邮件发送方法     我测试了很多条件的电子邮件发送逻辑,这是完美的。

请帮帮我。

public class ScheduleReportManager implements ServletContextListener{

    private ServletContext application  = null;
    private Environment environment =null;
    private Timer scheduledReportTimer=null;
    private Timer scheduleImmediateReportTimer=null; //daily timer 
    private ConcurrentHashMap scheduledReportTimerTasks;
    private ConcurrentHashMap scheduledImmediateReportTimerTasks; //daily timer hash map
    ApplicationContext webContext=null;

    public ScheduleReportManager() {
    }
    public void contextInitialized(ServletContextEvent servletContextEvent) {

        try{
            this.application= servletContextEvent.getServletContext();
            webContext = (ApplicationContext) application.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

            //daily timer 
            if(application.getAttribute("scheduledReportTimer")==null)
            {
                application.setAttribute("scheduleReportManager",this);
                this.scheduledReportTimer= new Timer(true);
                setupSchedule(scheduledReportTimer, application);
                application.setAttribute("scheduledReportTimer", scheduledReportTimer);
            }

            //timer for 10 mins
            if(application.getAttribute("scheduleImmediateReportTimer")==null)
            {
                this.scheduleImmediateReportTimer= new Timer(true);
                setupImmediateSchedule(scheduleImmediateReportTimer, application);
                application.setAttribute("scheduleImmediateReportTimer", scheduleImmediateReportTimer);
            }
            Logger.global.log(Level.INFO, "ScheduledReportTimer: " + application.getServletContextName() + ": Setup completed");
        } catch (Exception e)
        {
            Logger.global.log(Level.SEVERE, "Setup Report Timer Exception - " + e.toString());
        }
    }

    //timer for 10 mins
    public void setupImmediateSchedule(Timer scheduledImmediateReportTimer,ServletContext application ) {
        scheduledImmediateReportTimerTasks= new ConcurrentHashMap();
        ScheduleImmediateReportTimerTask immediateReportTimerTask = new ScheduleImmediateReportTimerTask(application,environment,this);
        scheduledImmediateReportTimerTasks.put(environment.getCode(),immediateReportTimerTask);
        scheduledImmediateReportTimer.schedule(immediateReportTimerTask,1000);
    }

    //timer for 10 mins
    public void setTimerForImmediateReportExecution(ScheduleImmediateReportTimerTask immediateReportTimerTask){

        Environment environment = immediateReportTimerTask.getEnvironment();
        ServletContext application = immediateReportTimerTask.getApplication();
        ScheduleImmediateReportTimerTask reportTimerTask= new ScheduleImmediateReportTimerTask(application,environment,this);
        String environmentCode = environment.getCode();

        synchronized (scheduledImmediateReportTimerTasks){
            scheduledImmediateReportTimerTasks.put(environmentCode,reportTimerTask);
            scheduleImmediateReportTimer.schedule(reportTimerTask,600000);      // set timer for running every 10 mins
        }
    }

    //daily timer 
    public void setTimerForNextExecution(ScheduledReportTimerTask timerTask, DateTime nextExecutionDateTime)
    {
        if(nextExecutionDateTime == null)
            return;

        Environment environment = timerTask.getEnvironment();
        ServletContext application = timerTask.getApplication();
        ScheduledReportTimerTask scheduledReportTimerTask =  new ScheduledReportTimerTask(application,environment,this);
        java.util.Date nextScheduleTime = nextExecutionDateTime.getDate();
        String environmentCode = environment.getCode();
        synchronized (scheduledReportTimerTasks){
            scheduledReportTimerTasks.put(environmentCode,scheduledReportTimerTask);
            Logger.global.log(Level.INFO, "ScheduledReportManager: next execution time is " + nextScheduleTime.toString());
            scheduledReportTimer.schedule(scheduledReportTimerTask,nextScheduleTime);
        }
    }
    //daily timer 
    public void setupSchedule(Timer scheduledReportTimer,  ServletContext application) throws Exception{
        this.environment = SpringBridgeUtil.retrieveEnvironment(application);
        this.scheduledReportTimerTasks= new ConcurrentHashMap();
        ScheduledReportTimerTask scheduledReportTimerTask = new ScheduledReportTimerTask(application,environment,this);
        scheduledReportTimerTasks.put(environment.getCode(),scheduledReportTimerTask);
        scheduledReportTimer.schedule(scheduledReportTimerTask,1000);
    }

    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        String contextName = application.getServletContextName();
        if (scheduledReportTimer != null) {
            scheduledReportTimer.cancel();
        }
        if(scheduleImmediateReportTimer !=null)
        {
            scheduleImmediateReportTimer.cancel();
        }
        Logger.global.log(Level.INFO, "scheduledReportTimer: " + contextName
                + ": Tasks cancelled due to context removal");
    }

}

// sends email on every 10 mins
public class ScheduleImmediateReportTimerTask extends TimerTask {
    @Override
    public void run() {
        try{

            // send mail 
            sendNotifiationEmail();

        }catch (Exception e) {
            e.printStackTrace();
            Logger.global.log(Level.INFO,"immediateScheduledReportTimerTask Exception " + e.toString());
        }

    }
}

我正在使用jdk 1.7和spring mvc 3.5.0。 使用java.util.Timer和import java.util.TimerTask;

的类

请告诉我这是否是正确的方法,还是有其他方法可以定期获取执行计划任务。

0 个答案:

没有答案