2017-06-27 87 views
1

我有一個在彈簧框架maven上開發的應用程序。我有很多模塊(每個模塊都有自己的pom.xml文件),並且我有通用的pom.xml來編譯整個項目。在Spring/Maven創建的.war文件上配置一個計劃任務

使用行家我編譯.war文件,我部署此一個在碼頭服務器,但現在我有另一個問題。我需要配置一個函數,每隔幾分鐘執行一些代碼。 我試圖配置它像at the following link,所以有:

我編輯的具體pom.xml文件,我補充一點:

<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter</artifactId> 
    <version>1.2.1.RELEASE</version> 
</dependency> 

依存關係列表,我加了這個在插件列表:

<plugin> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-maven-plugin</artifactId> 
</plugin> 

我在同一模塊中創建的定義該類一個文件:

@Component 
public class ScheduledTasks { 
    private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class); 

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); 

    @Scheduled(fixedDelay = 5000) 
    public void reportCurrentTime() { 
     log.info("The time is now {}", dateFormat.format(new Date())); 
    } 
} 

但它不適合我。日誌不打印任何東西。 我一直在努力解決這個問題一段時間,但我沒有設法找到任何解決方案也看看其他相關的問題在StackOverflow。

我在做什麼錯?我怎樣才能解決這個問題?

在此先感謝。

回答

0

你應該用@Service@EnableScheduling更換@Component修復。

@Service 
@EnableScheduling 
public class ScheduledTasks { 
    . . . 
} 
1

您需要EnableScheduling在你的主配置

@EnableScheduling 
..... 
..... other config annotaions 
public class ApplicationConfig { 
} 
+0

我忘了寫,我創建了另一個類一樣,你可以在[以下鏈接]見(https://gist.github.com/marnone9/01a2dabe39632e6885c0fd4a11f3c725) –

+0

你有你的日誌記錄設置是否正確,在調試器中添加一個System.out或執行它。 –

+0

我試過在同一個軟件包的休息服務中做一個日誌,它工作正常 –