gpt4 book ai didi

java - 如何使用 RabbitMQ JMS 客户端从现有的 JMS 应用程序连接到 RabbitMQ?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:52:30 26 4
gpt4 key购买 nike

我有一个通用的独立 JMS 应用程序,它与以下 JMS 提供程序 WebSphere、HornetQ 和 ActiveMq 一起工作。我将 Context.INITIAL_CONTEXT_FACTORY 和 Context.PROVIDER_URL 作为参数传递给我的应用程序,并通过执行类似这样的操作从它们中创建命名上下文

Properties environmentParameters = new Properties();
environmentParameters.put(Context.INITIAL_CONTEXT_FACTORY, property.context);
environmentParameters.put(Context.PROVIDER_URL, property.provider);
namingContext = new InitialContext(environmentParameters);

并使用此上下文进行对象查找。

我知道 RabbitMQ 不是 JMS 提供者,因此它没有 InitialContext 类或提供者 URL,但它提供了一个 JMS 客户端,这是一个符合 JMS 规范的 Java 客户端的抽象。 RabbitMQ的JMS客户端documentation有一个将 JNDI 中的对象定义为 Web 应用程序一部分的资源配置的示例,但我完全无法弄清楚如何为我的独立应用程序做类似的事情,它使用 JMS 客户端的依赖项创建基于 JNDI 提供程序的命名上下文,或者从可用的依赖项中创建一个 InitialContext。

那么有人可以阐明如何做到这一点吗?希望我的问题很清楚。

最佳答案

为了让 JMSRabbitMQ 一起工作,您必须启用插件 rabbitmq_jms_topic_exchange
您可以按照本网站中的说明下载它(您需要登录):
https://my.vmware.com/web/vmware/details?downloadGroup=VFRMQ_JMS_105&productId=349

  1. 提取后,将文件 rjms-topic-selector-1.0.5.ez 放入文件夹 $RABBITMQ_SERVER\plugins 中。
  2. 使用以下命令启用插件:rabbitmq-plugins enable rabbitmq_jms_topic_exchange
  3. 使用以下命令检查插件是否正常运行:rabbitmq-plugins list
    List of RabbitMQ plugins
  4. 重启 RabbitMQ - 我不确定是否真的有必要,但以防万一;-)
  5. 在您的 RabbitMQ 网络管理 (http://localhost:15672/#/exchanges) 中,您可以检查可用的新 Exchange: Exchanges of the RabbitMQ
  6. 现在,理论上 :-),您已经能够使用标准 Java JMS API 连接到 RabbiMQ 服务器。

请记住,为了让 JNDI 找到您注册的对象,您必须创建一个 .bindings 文件。这是它的内容示例:


ConnectionFactory/ClassName=com.rabbitmq.jms.admin.RMQConnectionFactory
ConnectionFactory/FactoryName=com.rabbitmq.jms.admin.RMQObjectFactory
ConnectionFactory/RefAddr/0/Content=jms/ConnectionFactory
ConnectionFactory/RefAddr/0/Type=name
ConnectionFactory/RefAddr/0/Encoding=String
ConnectionFactory/RefAddr/1/Content=javax.jms.ConnectionFactory
ConnectionFactory/RefAddr/1/Type=type
ConnectionFactory/RefAddr/1/Encoding=String
ConnectionFactory/RefAddr/2/Content=com.rabbitmq.jms.admin.RMQObjectFactory
ConnectionFactory/RefAddr/2/Type=factory
ConnectionFactory/RefAddr/2/Encoding=String
# Change this line accordingly if the broker is not at localhost
ConnectionFactory/RefAddr/3/Content=localhost
ConnectionFactory/RefAddr/3/Type=host
ConnectionFactory/RefAddr/3/Encoding=String
# HELLO Queue
HELLO/ClassName=com.rabbitmq.jms.admin.RMQDestination
HELLO/FactoryName=com.rabbitmq.jms.admin.RMQObjectFactory
HELLO/RefAddr/0/Content=jms/Queue
HELLO/RefAddr/0/Type=name
HELLO/RefAddr/0/Encoding=String
HELLO/RefAddr/1/Content=javax.jms.Queue
HELLO/RefAddr/1/Type=type
HELLO/RefAddr/1/Encoding=String
HELLO/RefAddr/2/Content=com.rabbitmq.jms.admin.RMQObjectFactory
HELLO/RefAddr/2/Type=factory
HELLO/RefAddr/2/Encoding=String
HELLO/RefAddr/3/Content=HELLO
HELLO/RefAddr/3/Type=destinationName
HELLO/RefAddr/3/Encoding=String

然后……最后……代码:

    Properties environmentParameters = new Properties();    environmentParameters.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");    environmentParameters.put(Context.PROVIDER_URL, "file:/C:/rabbitmq-bindings");    namingContext = new InitialContext(environmentParameters);    ConnectionFactory connFactory = (ConnectionFactory) ctx.lookup("ConnectionFactory");

关于java - 如何使用 RabbitMQ JMS 客户端从现有的 JMS 应用程序连接到 RabbitMQ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33384341/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com