2017-06-12 209 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()方法,你可以做任何你想要的,包括编程事务管理。