如何根据应用程序服务器动态更改TaskExecutor实现
我使用 Spring 3.0.x 从应用程序服务器获取 WorkManager 并使用它来运行需要数据库访问的预定作业。问题是,该应用程序可以部署到不同的 Java 应用程序服务器 - 在本例中为 Websphere 7.0 和 GlassFish 3.1 OSE。 (疯狂的组合,我知道...)
我遇到的问题是,在部署到任一服务器之前,我必须更改 bean 以引用用于每个服务器的相应 TaskExecutor 类。我让 Spring 加载了一个包含这些 bean 的 applicationContext.xml 。
对于Websphere,我必须从外部拉取WorkManager并使用以下bean:
<bean id="myTaskExecutor" class="org.springframework.scheduling.commonj.WorkManagerTaskExecutor">
<property name="workManagerName" value="wm/default" />
<property name="resourceRef" value="true"/>
</bean>
并且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>
对于GlassFish,我可以只使用该bean:
<bean id="myTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
</bean>
我可以以某种方式动态更改即时使用的 TaskExecutor 实现吗?
我可以轻松检查 Websphere 资源以确定我是否在 Websphere 上,然后回退到 GlassFish。但是如何使用 Spring(使用注释或其他方式)加载这样的类让我感到困惑。
任何帮助表示赞赏。谢谢!
PS 我并不担心 GlassFish 是否符合 J2EE - 只是 Websphere 迫使您这样做(因此需要使用外部 WorkManager)
I am using Spring 3.0.x to get a WorkManager from an application server and use it to run scheduled jobs that need database access. The problem is, is that this application could be deployed to different Java application servers - in this case, to Websphere 7.0 and GlassFish 3.1 OSE. (Crazy combo, I know...)
The problem I am having is that before deploying to either, I have to change the bean to reference the appropriate TaskExecutor class used for each server. I have Spring load up an applicationContext.xml with these beans in it.
For Websphere, I have to pull the WorkManager externally and use the following bean:
<bean id="myTaskExecutor" class="org.springframework.scheduling.commonj.WorkManagerTaskExecutor">
<property name="workManagerName" value="wm/default" />
<property name="resourceRef" value="true"/>
</bean>
and the web.xml has something like this:
<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>
For GlassFish, I can just use the bean:
<bean id="myTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
</bean>
Can I somehow dynamically change which TaskExecutor implementation is used on the fly?
I could easily check for the Websphere resource to determine if I am on Websphere, then fall back to GlassFish. But how to load a class like that with Spring (using Annotations or otherwise) is baffling me.
Any help is appreciated. Thanks!
P.S. I am not worried about being J2EE compliant for GlassFish - it's just that Websphere forces you to be so (hence pulling an external WorkManager)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以轻松地使用自定义 VM 参数来确定您正在运行的环境,并使用它来加载适当的上下文文件。
每个受支持的环境都有一个文件,所有这些环境都定义相同的 bean。
添加所需的 VM 参数。
然后,无论您在何处实际加载 bean,都可以包含基于 PropertyPlaceholderConfigurer 的适当文件。
根据评论中的信息进行编辑。
在这种情况下,您可以覆盖您确实可以控制的环境。
现在您有一个名为 spring/was.properties 的文件,它定义了默认情况下读取的属性 app.server=was。在 Glassfish 中,您提供与 VM 参数相同的属性,该属性现在将覆盖从文件读取的属性。
You can easily use a custom VM argument to determine what environment you are running in, and use that to load the appropriate context file.
One file for each supported environment, all of which define the same bean.
Add a required VM argument.
Then, wherever you actually load the bean you include the appropriate file based on a PropertyPlaceholderConfigurer.
Edits based on information in the comments.
You can override for the environment you do have control over in this case.
Now you have a file called spring/was.properties that defines a property app.server=was, which is read by default. In Glassfish, you supply the same property as a VM argument, which will now override the property read from the file.