Quartz计划的作业无法访问Websphere中的数据源 [英] Quartz scheduled jobs could not access datasource in Websphere

查看:107
本文介绍了Quartz计划的作业无法访问Websphere中的数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Web应用程序,其中批处理程序需要在特定时间运行.我使用Quartz库来安排作业.该Web应用程序已部署在Websphere 8.5.5上,并且可以正常工作,可以通过数据源(代码中给出的数据源为 java:comp/env/jdbc/db_datasource )访问表.在上述时间也会触发该作业.

I am developing a web app where batch programs need to run for specific times. I used Quartz library to schedule the jobs. The web app is deployed on Websphere 8.5.5 and its working fine, accessing the tables through datasources (Datasource given in code is java:comp/env/jdbc/db_datasource). The job is also triggered at the mentioned times.

当计划的作业通过数据源建立数据库连接时出现错误,错误是:

I am getting an error when the scheduled job makes a DB connection through the datasource and the error is:

javax.naming.ConfigurationException: A JNDI operation on a "java:" name cannot be completed because the server runtime is not able to associate the operation's thread with any J2EE application component.  This condition can occur when the JNDI client using the "java:" name is not executed on the thread of a server application request.  Make sure that a J2EE application does not execute JNDI operations on "java:" names within static code blocks or in threads created by that J2EE application.  Such code does not necessarily run on the thread of a server application request and therefore is not supported by JNDI operations on "java:" names. [Root exception is javax.naming.NameNotFoundException: Name comp/env/jdbc not found in context "java:".]
at com.ibm.ws.naming.java.javaURLContextImpl.throwExceptionIfDefaultJavaNS(javaURLContextImpl.java:522)
at com.ibm.ws.naming.java.javaURLContextImpl.throwConfigurationExceptionWithDefaultJavaNS(javaURLContextImpl.java:552)
at com.ibm.ws.naming.java.javaURLContextImpl.lookupExt(javaURLContextImpl.java:481)
at com.ibm.ws.naming.java.javaURLContextRoot.lookupExt(javaURLContextRoot.java:485)
at com.ibm.ws.naming.java.javaURLContextRoot.lookup(javaURLContextRoot.java:370)

我从错误消息中了解到,该作业正在J2ee容器外部运行,因此该作业无法使用数据源进行连接,我无法同意,因为Quartz是作为ServletContextListener实现的,因此在web.xml中提到.

I understand from the error message is that the job is running outside the J2ee container and so the datasource is not available for the Job to make the connection, which I cannot agree as the Quartz is implemented as the ServletContextListener and the same is mentioned in web.xml.

Web.xml

<listener>
    <listener-class>com.ehacampaign.helper.EHAJobSchedulerListener</listener-class>
</listener>

EHAJobSchedulerListener.java

public class EHAJobSchedulerListener implements ServletContextListener {..}

您可以看到该代码,该类已在Web中注册,我不明白为什么它不能使用J2EE容器中的数据源.

As you can see the code, the class is registered in the web and I do not understand why it cannot use the datasource in the J2EE container.

问题是:

  1. 为什么servlet注册类无法访问J2EE中的数据源容器?
  2. 如果无法使用容器中的数据源,那么如何制作一个执行作业时连接到数据库吗?

注意:我在JBoss AS 7.1中具有相同的设置,并且作业正在顺利访问JBoss AS 7.1中配置的数据源.我必须根据客户的需求在Websphere中进行开发.

NOTE: I have the same setup in JBoss AS 7.1 and the jobs are running smoothly accessing the datasource configured in JBoss AS 7.1. I have to develop this in Websphere as the customer demands it.

已更新

我已附加了修改后的石英属性文件.即使在添加了workmanagerthread之后,我仍然遇到相同的错误.

I have attached the modified quartz property file. Even after adding the workmanagerthread, I am getting the same error.

org.quartz.threadPool.threadCount=1
org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool

org.quartz.jobStore.class=org.quartz.simpl.RAMJobStore
org.quartz.threadExecutor.class=org.quartz.commonj.WorkManagerThreadExecutor
org.quartz.threadExecutor.workManagerName=wm/default

推荐答案

为了在WebSpehre中执行JNDI查找,您的代码必须在托管线程上运行.为了使Quartz在WebSphere的托管线程之一上运行,必须在 quartz.properties 中设置以下2个属性(如注释中提到的Alasdair所述):

In order to perform JNDI lookups in WebSpehre, your code must be running on a managed thread. In order to have Quartz run on one of WebSphere's managed threads, you must set the following 2 properties in your quartz.properties (as Alasdair mentioned in the comments):

org.quartz.threadExecutor.class=org.quartz.commonj.WorkManagerThreadExecutor
org.quartz.threadExecutor.workManagerName=wm/default

org.quartz.threadExecutor.workManagerName 的名称可以是您在WebSphere中配置的任何工作管理器的JNDI名称.我建议只使用 wm/default ,因为默认情况下它在您的配置中.

The name for org.quartz.threadExecutor.workManagerName can be the JNDI name of any Work Manager that you have configured in WebSphere. I recommend simply using wm/default because it is in your configuration by default.

这篇关于Quartz计划的作业无法访问Websphere中的数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆