2015-03-25 115 views
2

我想通過外部配置文件禁用彈簧調度。我有配置文件設置,下面的示例中的任務會註銷以下內容。 INFO MainTaskScheduler:36 - scheduled task: Update converted bookings false.因此,大多數情況下,那裏。禁用與屬性文件的調度

我希望實現的是不必在每個任務方法內部放置邏輯來確定是否已啓用調度屬性。

所以這樣的事情在我的配置文件(這是無效代碼) @EnableScheduling(${enable.scheduling})

我的工作片段


AppConfiguration

@Configuration 
@EnableTransactionManagement 
@EnableScheduling 
@ComponentScan(/*etc*/}) 
public class AppConfiguration { 

} 

MainTaskScheduler

@Component 
public class MainTaskScheduler { 

    private Logger log = LoggerFactory.getLogger(getClass()); 

    @Value("${enable.scheduling}") 
    private Boolean enableScheduling; 

    @Scheduled(fixedRate=300) // every 5 minutes -- check if any existing quotes have been converted to bookings 
    public void updateConvertedBookings() { 
     log.info("scheduled task: Update converted bookings "+enableScheduling); 
     // logic for class here 
    } 
} 

application.properties

enable.scheduling=false 
+0

使用配置文件。爲'@ EnableScheduling'創建一個單獨的'@ Configuration'類,該類僅在特定配置文件中啓用。 – 2015-03-25 12:02:59

回答

2

如果使用的是春天開機,您可以使用@ConditionalOnExpression註解來啓用或禁用調度:

@ConditionalOnExpression("'${enable.scheduling}'=='true'") 
+0

我喜歡這個答案。然而,我沒有它設置爲春季啓動..有沒有辦法我可以使用這個沒有依賴? – 2015-03-25 12:27:17

+0

沒有這不可能,因爲這個註釋是春季啓動的一部分。 – Mithun 2015-03-25 12:29:46

+0

沒有工作......我試圖禁用春季開機測試的日程安排 – Hinotori 2017-05-24 14:01:56