2013-10-02 218 views
0

我已將以下內容添加到我的Spring配置中。我假設默認超時值以秒爲單位,所以我將其設置爲三分鐘。我設置了一個異步任務,讓當前線程休眠五分鐘。我觸發異步任務,並運行完成,沒有異常或中斷。我究竟做錯了什麼?春季異步超時不起作用

<mvc:annotation-driven> 
     <mvc:async-support default-timeout="180"/> 
</mvc:annotation-driven> 

以下是異步方法我打電話:

@Async 
    public void generateIDOIncentiveFiles(String sessionId, String userId) throws Exception 
    { 
     final long SLEEP_TIME_MILLS = 5 * 60 * 1000; 

     try 
     { 
      // Get the job entry from the JOBS table 
      Job job = jobsDao.getJob(RequestHelper.JOB_IDO_INCENTIVES); 

      // Check to see if the job is enabled. 
      if (job.isEnabled() == false) 
       throw new Exception ("Job is not enabled"); 

      // Check to see if the job is already running. 

      if (job.isRunning()) 
       throw new Exception ("Job is running"); 

      // Start the timer 
      StopWatch sw = new StopWatch(); 
      sw.start(); 

      // Capture the date/time when the job was started 
      Date jobStartDate = new Date(); 

      LOG.debug("Starting IDO Incentives Extract process..."); 

      String jobCurrentStatus = "running"; 
      String jobLastRunMsg = "Job started"; 
      Date jobLastRunDate = new Date(); 

      jobsDao.updateJobStarted(userId, RequestHelper.JOB_IDO_INCENTIVES, jobLastRunDate, jobLastRunMsg, jobCurrentStatus); 

      LOG.debug("Sleeping for five minutes..."); 

      Thread.sleep(SLEEP_TIME_MILLS); 

      LOG.debug("Back from sleep."); 

      jobCurrentStatus = "idle"; 

      // Capture the date/time when the job ended 
      sw.stop(); 
      double elapsedTime = sw.getTotalTimeSeconds(); 
      int elapsedTimeMinutes = (int) (elapsedTime/60); 
      Date jobEndDate = new Date(); 

      jobsDao.updateJobComplete(RequestHelper.JOB_IDO_INCENTIVES, "", "idle"); 

      // Add entry to JOB_HISTORY table 

      LOG.debug("Updating job history..."); 

      JobHistory jobHistory = new JobHistory(); 

      jobHistory.setFile_name("file name"); 
      jobHistory.setFile_path("file path"); 
      jobHistory.setElapsed_time(elapsedTimeMinutes); 
      jobHistory.setEnd_date(jobEndDate); 
      jobHistory.setJob_name(RequestHelper.JOB_IDO_INCENTIVES); 
      jobHistory.setStart_date(jobStartDate); 
      jobHistory.setStatus("Success"); 
      jobHistory.setUserid(userId); 

      jobHistoryDao.insertJobHistory(jobHistory); 

      LOG.debug("Job complete"); 

     } 
     catch (InterruptedException e) 
     { 
      e.printStackTrace(); 
     } 
    } 
+0

向我們展示如何使用異步任務。 ''用於異步請求處理。 –

+0

我已經添加了異步調用的方法。 –

回答

1

因此,這是我的想法。該

<mvc:annotation-driven> 
    <mvc:async-support default-timeout="180"/> 
</mvc:annotation-driven> 

async-support這裏有無關@Async。它與Servlet 3 asynchronous request handling.

沒有辦法爲@Async方法提供超時值。 Have a look at this answer for how to actually do it.

+0

我試着使用Future對象的get()方法,並且能夠獲得拋出的TimeoutException。但是,異步方法仍然保持運行。那麼,我猜Async方法可以保持永久運行?除了殺死JVM之外,沒有辦法阻止失控的異步進程? –

+0

@MichaelSobczak你確定它還在運行嗎?如果引發'Exception','ExecutorService'應該返回'Thread'到它的池。 –