简体   繁体   English

Spring JMS消息侦听器无法找到JNDI查找

[英]Spring JMS Message Listener fails to find JNDI lookup

The application uses Spring JMS Listener to connect to JMS Queues and it is deployed in WAS 8.5.x. 该应用程序使用Spring JMS Listener连接到JMS队列,并部署在WAS 8.5.x中。 Using spring application context xml, org.springframework.jms.listener.DefaultMessageListenerContainer bean is initialized with ConnectionFactory , Destination as JndiObjectFactoryBean . 使用spring应用程序上下文xml,将org.springframework.jms.listener.DefaultMessageListenerContainer bean初始化为ConnectionFactory ,Destination为JndiObjectFactoryBean

Inside MessageListener class onMessage() method, the code is trying to look JNDI reference using InitialContext , code snippet is below, MessageListener类的onMessage()方法内部,代码尝试使用InitialContext查找JNDI引用,下面是代码段,

InitialContext ic = new InitialContext();
String name = (String) ic.lookup("jndiName");

Application throws below exception while processing a message. 应用程序在处理消息时将引发异常。

Exception : 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. 异常:javax.naming.ConfigurationException:无法完成对“ java:”名称的JNDI操作,因为服务器运行时无法将操作的线程与任何J2​​EE应用程序组件相关联。 This condition can occur when the JNDI client using the "java:" name is not executed on the thread of a server application request. 当未在服务器应用程序请求的线程上执行使用“ java:”名称的JNDI客户端时,会发生这种情况。 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. 确保J2EE应用程序不在静态代码块内或该J2EE应用程序创建的线程中的“ java:”名称上执行JNDI操作。 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. 这样的代码不一定在服务器应用程序请求的线程上运行,因此JNDI操作对“ java:”名称不支持。 [Root exception is javax.naming.NameNotFoundException: Name comp/env/cache not found in context "java:".] [根本异常是javax.naming.NameNotFoundException:在上下文“ java:”中找不到名称comp / env / cache。]

My understanding is, the Message Listener threads are created by the application during initialization under J2EE Web Container. 我的理解是,消息侦听器线程是由应用程序在J2EE Web容器下的初始化期间创建的。 The code is trying to access JNDI resource defined in WebSphere server, but not able to get the handle to Initial Context. 该代码正在尝试访问WebSphere服务器中定义的JNDI资源,但无法获取Initial Context的句柄。

Note: resource-ref tag with this JNDI entry is included in web.xml. 注意:带有此JNDI条目的resource-ref标记包含在web.xml中。

<resource-ref id="configCache">
  <description>Resource reference to Configuration Cache</description>
  <res-ref-name>cache/config</res-ref-name>
  <res-type>com.ibm.websphere.cache.DistributedMap</res-type>
  <res-auth>Container</res-auth>
  <res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>

Any help would be appreciated. 任何帮助,将不胜感激。

I was able to fix this problem. 我能够解决此问题。

Here is the solution, In web.xml I defined the Resource reference of type com.ibm.websphere.cache.DistributedMap. 这是解决方案,在web.xml中,我定义了com.ibm.websphere.cache.DistributedMap类型的Resource引用。 I created the JNDI (of type Object Cache) in WebSphere Admin console and map it to this resource during deployment. 我在WebSphere Admin控制台中创建了JNDI(类型为Object Cache),并在部署期间将其映射到该资源。

 <resource-ref id="sampleCacheName">
        <description>Resource reference to Cache</description>
        <res-ref-name>cache/sampleCache</res-ref-name>
        <res-type>com.ibm.websphere.cache.DistributedMap</res-type>
        <res-auth>Container</res-auth>
        <res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>

In my spring context xml, I defined the JMS Listener bean which takes Task Executor object from default WebSphere work manager, 在我的Spring上下文xml中,我定义了JMS侦听器Bean,该Bean从默认的WebSphere工作管理器获取Task Executor对象,

 <bean id="jmsContainerSampleEvent"
        class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="myConnectionFactory" />
        <property name="destination" ref="myQueue" />
        <property name="messageListener" ref="myEventListener" />
        <property name ="concurrentConsumers" value ="5"/>
    <property name ="maxConcurrentConsumers" value ="20"/>
        <property name="taskExecutor" ref="workManagerTaskExecutor" />
</bean>


<bean id="workManagerTaskExecutor"
          class="org.springframework.scheduling.commonj.WorkManagerTaskExecutor"
          p:workManagerName="wm/default" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM