【问题标题】:Connect OpenLiberty to RabbitMQ over JMS通过 JMS 将 OpenLiberty 连接到 RabbitMQ
【发布时间】:2020-05-09 04:11:32
【问题描述】:

我正在编写一个想要连接到 RabbitMQ 总线的 Java EE 8 应用程序。该应用程序部署在 OpenLiberty 中。由于应用程序使用 Java EE 8,我想使用 JMS 连接到 RabbitMQ。我之前有connected to RabbitMQ over JMS in Tomcat 作为概念验证,所以我知道它一定是可能的。

根据我找到的文档和博客,我需要一个资源适配器来将 OpenLiberty 连接到除了其内部 JMS 引擎之外的所有其他东西。我找到了AMQP 1.0 resource adapter 并想试一试。

我的 server.xml:

<featureManager>
  <feature>beanValidation-2.0</feature>
  <feature>cdi-2.0</feature>
  <feature>jaxrs-2.1</feature>
  <feature>jsp-2.3</feature>
  <feature>servlet-4.0</feature>
  <feature>jms-2.0</feature>

  <feature>appSecurity-3.0</feature>
  <feature>socialLogin-1.0</feature>
</featureManager>

<!-- the resource-adapter-1.0.0.rar comes from the aforementioned GitHub project -->
<resourceAdapter id="amqp" location="${server.config.dir}/resource-adapter-1.0.0.rar">
  <classloader apiTypeVisibility="+third-party"/>
</resourceAdapter>

<jmsConnectionFactory jndiName="jms/JmsFactory">
  <!-- using .amqp as that is the ID for the resource adapter -->
  <properties.amqp ConnectionFactory=""
                   JndiParameters=""
                   DeleteTemporaryDestinations="true"
                   UserName=""
                   Password="" />
</jmsConnectionFactory>

然后,在我的应用程序代码中,我有

@Inject
@JMSConnectionFactory("jms/JmsFactory")
private JMSContext context;

只要我调用context.createQueue("ExampleQueue")(或任何方法调用),我就会得到

javax.naming.NameNotFoundException: Intermediate context does not exist: jms/JmsFactory

我也尝试过手动查找,如下所示:

final InitialContext context = new InitialContext();
final Context environment = (Context) context.lookup("java:comp/env");

final QueueConnectionFactory factory = (QueueConnectionFactory) environment.lookup("jms/JmsFactory");
final Queue queue = InitialContext.doLookup("jms/ExampleQueue");

environment.lookupjavax.naming.NameNotFoundException: java:comp/env/jms/JmsFactory 失败。

我做错了什么,还是在这里监督?

【问题讨论】:

  • 你配置了哪些功能?
  • @Alasdair 感谢您的提问。我已经更新了 server.xml sn-p。
  • 能否将 jndi-1.0 添加到功能列表中,看看是否可行?
  • @Alasdair 我已启用 jndi-1.0 并重新启动 OpenLiberty。我得到了完全相同的异常。
  • 我一直在尝试自己,但我根本没有注射任何东西。还没有深究。

标签: rabbitmq jms open-liberty


【解决方案1】:

Liberty 期望 JMS 资源适配器如何定义与该资源适配器如何定义自身之间存在不匹配。 Liberty 期望 JMS 资源适配器使用 ra.xml 中的 JMS 类作为接口定义,因此 javax.jms.ConnectionFactory。相反,此 RA 使用org.jboss.resource.adapter.jms.JmsConnectionFactory。在运行时这是一个javax.jms.ConnectionFactory,但不在 ra.xml 中。

有一个简单的解决方法,只需使用 JCA connectionFactory 元素。我发现以下配置有效:

<connectionFactory jndiName="jms/JmsFactory">
   <!-- using .amqp as that is the ID for the resource adapter -->
   <properties.amqp DeleteTemporaryDestinations="true"
       ConnectionFactory="factory1"
       JndiParameters="java.naming.factory.initial=org.apache.qpid.jms.jndi.JmsInitialContextFactory;connectionFactory.factory1=amqp://localhost:5672"              
       UserName=""
       Password="" />
</connectionFactory>

Liberty 中有一个针对此问题 12088 的错误,但我建议只使用 JCA connectionFactory 元素即可。

【讨论】:

    猜你喜欢
    • 2022-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-17
    • 2019-12-31
    • 1970-01-01
    • 2016-01-27
    相关资源
    最近更新 更多