使用任务执行程序线程运行Spring计划任务 [英] Getting Spring scheduled tasks to run with the task executor thread
问题描述
后台:我在我的Websphere 7应用程序中使用Spring 3.0.x来获取CommonJ WorkManager和TimerManager。我使用这些在我部署的应用程序中以特定间隔执行任意任务。
Background: I use Spring 3.0.x in my Websphere 7 applications to grab the CommonJ WorkManager and TimerManager. I use these to do arbitrary tasks at certain intervals in my deployed applications.
问题:
我刚刚了解到在调度程序中设置bean时如下:
I just learned that when setting a bean in the scheduler like so:
<bean id="threadTest" class="test.ThreadTester" />
<task:scheduled-tasks scheduler="myTimerExecutor">
<task:scheduled ref="threadTest" method="execute" fixed-delay="300000" />
</task:scheduled-tasks>
在Websphere中,它将运行 ThreadTester
直接在TimerManager线程池中的类。此线程池具有比WorkManager线程池高得多的优先级(和更少的线程)。我希望线程与WorkManager一起运行,而不是使用适当的优先级并使用为线程设置的适当资源。
In Websphere, it will run the ThreadTester
class directly in the TimerManager thread pool. This thread pool has a much higher priority (and fewer threads) than the WorkManager thread pool. I want the thread to run with the WorkManager instead to be at the proper priority and use the proper resources set up for threads.
作为替代方案,不要设置bean在上面的调度程序中,我可以在 ThreadTester
类中使用Spring @Scheduled
注释,如下所示:
As an alternative, instead of setting the bean in the scheduler like above, I can use the Spring @Scheduled
annotation in the ThreadTester
class like so:
@Scheduled(fixedDelay = 300000)
public void execute() {
...
}
问题:
使用 @Scheduled
注释是否使计划的类在TimerManager线程池或WorkManager线程池中运行?
Does using the @Scheduled
annotation make the scheduled class run in the TimerManager thread pool or the WorkManager thread pool?
如果它使用WorkManager运行,那就太好了!这解决了我的问题。但是,如果它使用TimerManager直接执行类,那么我想我必须编写一些包装器来正确调用WorkManager。
If it runs using the WorkManager, then great! That solves my problem. However, if it is using the TimerManager to directly execute the class, then I guess I'll have to write some wrapper to correctly call the WorkManager.
感谢您的帮助!
编辑:这里我介绍了如何使用Websphere 7 commonj实现设置调度程序和执行程序:
Here I include how I set up the scheduler and executor with the Websphere 7 commonj implementations:
<bean id="myTaskExecutor" class="org.springframework.scheduling.commonj.WorkManagerTaskExecutor">
<property name="workManagerName" value="wm/default" />
<property name="resourceRef" value="true"/>
</bean>
<bean id="myTaskScheduler" class="org.springframework.scheduling.commonj.TimerManagerTaskScheduler">
<property name="timerManagerName" value="tm/default" />
<property name="resourceRef" value="true" />
<property name="shared" value="false" />
</bean>
<task:annotation-driven executor="myTaskExecutor" scheduler="myTaskScheduler" />
推荐答案
嗯,我发现是的,确实, @Scheduled
bean在Websphere 7上的TimerManager线程池中运行。
Well, I found out that yes, indeed, @Scheduled
beans are run within the TimerManager thread pool on Websphere 7.
我所要做的就是吐出堆栈线程的跟踪,以查看调用它的层次结构。
All I had to do was spit out the stack trace of the thread to see the hierarchy in which it was called.
for(StackTraceElement element: Thread.currentThread().getStackTrace()) {
logger.debug(element.toString());
}
这篇关于使用任务执行程序线程运行Spring计划任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!