如何获得在SimpleAsyncTaskExecutor.createThread中创建的线程(Runnable runnable)

时间:2014-12-01 15:48:12

标签: spring spring-batch

在我使用Spring Batch 3.0.1的应用程序中,我需要访问 SimpleAsyncTaskExecutor.createThread(Runnable runnable)创建的线程,然后才能在 doExecute 中启动它。

需要这样做才能将安全上下文内容附加到新创建的线程中。

当然我可以扩展SimpleAsyncTaskExecutor并使用这个类,但这似乎是一个粗略的解决方案。 我尝试使用Spring AOP和拦截器,但无法拦截正确的方法。

有什么想法吗?

编辑:

protected void doExecute(Runnable task) {
    Thread thread = (this.threadFactory != null ? this.threadFactory.newThread(task) : createThread(task));
    thread.start();
}

1 个答案:

答案 0 :(得分:0)

我认为处理此类行为的正确方法是创建自己的ThreadFactory并将其传递给SimpleAsyncTaskExecutor。这将允许您在创建线程时访问它们。

您可以在此处详细了解SimpleAsyncTaskExecutorhttp://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/core/task/SimpleAsyncTaskExecutor.html

您可以在此处详细了解ThreadFactoryhttps://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadFactory.html

相关问题