问题描述
我正在尝试使用 ejb2 和 solace 资源适配器将 solace 与 web sphere自由 20 版本集成。我在 ejb 中配置了 MDB bean,它侦听队列。我能够在 MDB 上获取消息,但是在处理时我需要将响应发布回队列,并且这个队列名称是基于来自上游系统的消息动态的。所以我不能在容器中将发布者配置为无状态 bean。
现在我想在使用 solace 资源适配器的 MDB 的 server.xml 中配置的发布者代码中使用连接工厂。
我尝试了以下方法。
在 server.xml 中:
<featureManager>
<feature>javaee-8.0</feature>
<feature>localConnector-1.0</feature>
<feature>jndi-1.0</feature>
<feature>wasJmsClient-2.0</feature>
<feature>wasJmsServer-1.0</feature>
<feature>wasJmsSecurity-1.0</feature>
<feature>mdb-3.2</feature>
<feature>servlet-4.0</feature>
<feature>ejb-3.2</feature>
<feature>ejbHome-3.2</feature>
<feature>adminCenter-1.0</feature>
<feature>jca-1.7</feature>
<feature>jms-2.0</feature>
<feature>webProfile-8.0</feature>
<resourceAdapter autoStart="true" id="solace" location="sol-jms-ra-10.10.0.rar">
<properties.solace ConnectionURL="URL" UserName="user1" Password="pwd" MessageVPN="TEST_VPN"/>
</resourceAdapter>
<jmsActivationSpec id="JNDI/LISTENER">
<properties.solace connectionFactoryJndiName="myCF" destination="queue" destinationType="javax.jms.Queue" />
</jmsActivationSpec >
<jmsConnectionFactory id="JNDI/J2C/CF" jndiName="JNDI/J2C/CF">
<properties.solace ConnectionFactoryJndiName="myCF"/>
</jmsConnectionFactory>
在我的发布商代码中,按如下方式进行 jndi 查找。
Context ctx = new InitialContext();
connectionFactory = (QueueConnectionFactory) ctx.lookup("java:comp/env/JNDI/J2C/CF");
connection = connectionFactory.createQueueConnection();
但得到以下异常
javax.naming.NameNotFoundException: javax.naming.NameNotFoundException: java:comp/env/JNDI/J2C/CF
at com.ibm.ws.jndi.url.contexts.javacolon.internal.JavaURLContext.lookup(JavaURLContext.java:355)
at com.ibm.ws.jndi.url.contexts.javacolon.internal.JavaURLContext.lookup(JavaURLContext.java:370)
at org.apache.aries.jndi.DelegateContext.lookup(DelegateContext.java:149)
at javax.naming.InitialContext.lookup(InitialContext.java:417)
有人可以帮我吗。
解决方法
您需要定义从资源引用名称 java:comp/env/JNDI/J2C/CF
到配置的 jndiName JNDI/J2C/CF
的绑定。
一种标准方法是在您的 web 或 ejb 组件中使用 @Resource
注释来查找它(您也可以使用 @Resource
注释注入的值而不是查找)。例如,
@Resource(name = "java:comp/env/JNDI/J2C/CF",lookup = "JNDI/J2C/CF")
QueueConnectionFactory qcf;
另一种可以用来定义资源引用的方法是在 ejb-jar.xml 文件中进行。例如,
<ejb-jar xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd"
version="2.1">
<enterprise-beans>
<message-driven>
<ejb-name>MyMessageDrivenBean</ejb-name>
<ejb-class>org.example.MyMessageDrivenBean</ejb-class>
<messaging-type>javax.jms.MessageListener</messaging-type>
<transaction-type>Bean</transaction-type>
<resource-ref>
<res-ref-name>java:comp/env/JNDI/J2C/CF</res-ref-name>
<res-type>javax.jms.QueueConnectionFactory</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
<lookup-name>JNDI/J2C/CF</lookup-name>
</resource-ref>
</message-driven>
</enterprise-beans>
</ejb-jar>
或
<ejb-jar xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd"
version="2.1">
<enterprise-beans>
<session>
<ejb-name>MyStatelessBean</ejb-name>
<ejb-class>org.example.MyStatelessBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Bean</transaction-type>
<resource-ref>
<res-ref-name>java:comp/env/JNDI/J2C/CF</res-ref-name>
<res-type>javax.jms.QueueConnectionFactory</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
<lookup-name>JNDI/J2C/CF</lookup-name>
</resource-ref>
</session>
</enterprise-beans>
</ejb-jar>
另外,如果你想要一个 QueueConnectionFactory,你需要使用 jmsQueueConnectionFactory 配置元素,
<jmsQueueConnectionFactory id="JNDI/J2C/CF" jndiName="JNDI/J2C/CF">
<properties.solace ConnectionFactoryJndiName="myCF"/>
</jmsQueueConnectionFactory>