如果您正在创建自己的线程,那很可能是问题所在。在任何应用程序服务器中,您都应该让容器管理线程池并安排您可能想要运行的任何作业。
将此添加到您的 web.xml:
<resource-ref>
<res-ref-name>timer/MyTimer/res-ref-name>
<res-type>commonj.timers.TimerManager</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Unshareable</res-sharing-scope>
</resource-ref>
在您的 applicationContext.xml 中(需要 spring-context-support JAR):
<bean id="scheduler" class="org.springframework.scheduling.commonj.TimerManagerTaskScheduler" scope="singleton">
<property name="timerManagerName" value="java:comp/env/timer/MyTimer"/>
</bean>
然后在你的 contextInitialized(...) 做:
scheduledFuture = scheduler.schedule(new MyJob());
在你的上下文中Destroyed(...) 做;
scheduledFuture.cancel(true);
在我的项目中,我找不到任何关于在应用程序服务器级别配置计时器的信息,所以我猜它“正常工作”。
如果要异步生成作业(通过java.lang.concurrent.Executor
接口),过程类似,但需要在 Weblogic 中配置线程池:
在 web.xml 中;
<resource-ref>
<res-ref-name>wm/MyWorkManager</res-ref-name>
<res-type>commonj.work.WorkManager</res-type>
<res-auth>Container</res-auth>
</resource-ref>
在 applicationContext.xml 中:
<bean id="executor" class="org.springframework.scheduling.commonj.WorkManagerTaskExecutor" scope="singleton">
<property name="workManagerName" value="java:comp/env/wm/MyWorkManager"/>
</bean>
在您的 weblogic.xml(或 EAR 的等效项)中,类似:
<work-manager>
<name>wm/MyWorkManager</name>
<min-threads-constraint>
<name>minThreads</name>
<count>1</count>
</min-threads-constraint>
<max-threads-constraint>
<name>maxThreads</name>
<count>20</count>
</max-threads-constraint>
</work-manager>
在您的代码中:
executor.execute(new MyRunnable());
阅读有关计时器和工作管理器的 Weblogic 文档,了解有关此特定应用服务器的更多作业调度相关信息。