【问题标题】:Configuring the jmsConnectionFactory in server.xml for solace resource adapter doesn't work在 server.xml 中为 solace 资源适配器配置 jmsConnectionFactory 不起作用
【发布时间】:2021-05-14 09:23:16
【问题描述】:

我正在尝试使用 ejb2 和 solace 资源适配器将 solace 与 web sphere liberty 20 版本集成。我在 ejb 中配置了侦听队列的 MDB bean。我能够在 MDB 上获取消息,但是在处理时我需要将响应发布回队列,并且此队列名称是动态的,基于来自上游系统的消息。所以我不能将发布者配置为容器中的无状态 bean。

现在我想使用在 server.xml 中为 MDB 配置的发布者代码中的连接工厂,使用 solace 资源适配器。

我尝试了以下方法。

在 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)

有人可以帮我吗?

【问题讨论】:

    标签: jms websphere-liberty solace open-liberty


    【解决方案1】:

    您需要定义从资源引用名称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>
    

    【讨论】:

    • EJB 2 正在代码中使用。所以不能使用@Resource 注释。我们需要在 ejb-jar.xml 中使用 resource-ref 标签吗?如果是这样,我们该怎么做?
    • 是的,我在ejb-jar.xml中添加了一些resource-ref的例子来回答
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-09
    • 2018-03-28
    • 2019-10-22
    • 2016-08-13
    • 1970-01-01
    • 2019-03-15
    • 1970-01-01
    相关资源
    最近更新 更多