2017-06-12 118 views
1

獲得安全上下文我得到的錯誤無法從計劃任務

org.springframework.scheduling.support.TaskUtils$LoggingErrorHandler - Unexpected error occurred in scheduled task. 

java.lang.SecurityException: No security context bound to the current thread 

使用核心模塊在計劃任務在應用程序啓動一次執行任務:

<task:scheduled-tasks scheduler="scheduler"> 
    <task:scheduled ref="app_OlapService" method="initialize" fixed-rate="9223372036854775807"/> 
</task:scheduled-tasks> 

我已經在注入的Authentication實例上嘗試了@Authenticated註解和authentication.begin()。

它看起來像一個單獨的調度觸發的錯誤,我也有一個空方法體相同的錯誤。

我也開放了替代方法,考慮到我需要使用事務和@PostConstruct AppContext.Listener不允許我。

回答

1

也許你正在調用一個服務的方法。這樣的調用被攔截並檢查是否存在有效的用戶會話,因此它在調用時應該已經存在。它在從客戶端或已經過身份驗證的中間件代碼調用服務時起作用。

就你而言,我會建議從服務中提取邏輯到託管bean,並從服務和調度程序調用它。

至於AppContext.Listener,在applicationStarted()方法,你可以做任何你想要的,包括編程事務管理。