为了统一 Websphere 7 和 GlassFish 3 环境之间的部署,我决定尝试在 GlassFish 中实现 CommonJ WorkManager 和 TimerManager。但它并没有完全按预期工作。我已完成以下操作:
使用位于 http://commonj.myfoo.de/ 的 myFOO CommonJ 实现并将库包含到我的domain/lib文件夹中(包括Spring库)
将以下内容添加到 <resources>
glassfish 域.xml 部分:
<custom-resource res-type="commonj.work.WorkManager" jndi-name="wm/default" factory-class="de.myfoo.commonj.work.FooWorkManagerFactory"></custom-resource>
<custom-resource res-type="commonj.timers.TimerManager" jndi-name="tm/default" factory-class="de.myfoo.commonj.timers.FooTimerManagerFactory"></custom-resource>
将引用文献包含在 <servers>
中/<server>
domain.xml 部分:
<resource-ref ref="wm/default"></resource-ref>
<resource-ref ref="tm/default"></resource-ref>
在我的测试应用程序的 web.xml 中添加适当的资源引用:
<resource-ref>
<description>WorkManager</description>
<res-ref-name>wm/default</res-ref-name>
<res-type>commonj.work.WorkManager</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
<resource-ref>
<description>TimerManager</description>
<res-ref-name>tm/default</res-ref-name>
<res-type>commonj.timers.TimerManager</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Unshareable</res-sharing-scope>
</resource-ref>
将以下 bean 添加到我的 applicationContext.xml 中:
<bean id="threadTestTaskExecutor" class="org.springframework.scheduling.commonj.WorkManagerTaskExecutor">
<property name="workManagerName" value="wm/default" />
<property name="resourceRef" value="true"/>
</bean>
<bean id="threadTestTimerExecutor" class="org.springframework.scheduling.commonj.TimerManagerTaskScheduler">
<property name="timerManagerName" value="tm/default" />
<property name="resourceRef" value="true" />
<property name="shared" value="false" />
</bean>
<bean id="threadTest" class="test.ThreadTester"></bean>
<task:scheduled-tasks scheduler="threadTestTimerExecutor">
<task:scheduled ref="threadTest" method="execute" fixed-delay="30000" /> <!-- 30 seconds -->
</task:scheduled-tasks>
完成所有这些设置后,所有内容都会加载 find 并运行 Web 应用程序;但是,ThreadTester 类不在 Timer 上运行。
我已经单步执行了 myFOO 代码,并且 TimerManager (FooTimerManager.java) 主循环正在运行,但它似乎从未认识到它应该每 30 秒启动该类。
我的问题:
有人有使用 GlassFish 3 和 Spring 实现 JSR 236/237 (CommonJ) 的经验吗?
除了 myFOO 之外,还有其他我可以使用和尝试的实现吗? 有人尝试做我所做的事情吗?如果你成功了,你愿意分享你的结果吗?
谢谢!
编辑 1:
我忘了提及,就 WorkManager 而言,将 myFOO CommonJ 实现与 GlassFish 结合使用确实有效。 TimerManager不起作用。这意味着我可以按需启动线程,但触发调度不起作用。
编辑2:
自从更新到 GlassFish 3.1.1 以来,TimerManager 的 myFOO CommonJ 实现工作正常。很好!这个问题现在更像是一个 HOWTO 指南。
最佳答案
我认为使用 myFoo CommonJ 的 TimerManager 不是一个好主意 - 除了休眠了大约 6 年之外,代码在某些地方很奇怪(引用 v1.1)。例如。 FooTimer 类的 isExpired 方法如下所示:
public boolean isExpired() {
return scheduledExcecutionTime >= System.currentTimeMillis();
}
那么,当它预定的下一次执行时间是在未来时,定时器就会到期吗?胡说八道 - 应该反过来!
在其他地方(TimerExecutor#run),对当前线程没有监视器的对象(TimerManager)调用notifyAll,从而不断导致java.lang.IllegalMonitorStateExceptions。
放手!
关于spring - 将 CommonJ 实现与 GlassFish 和 Spring 3 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7490297/