gpt4 book ai didi

jms - 如何将 JMS 消息从 WildFly 10 发送到远程 ActiveMQ

转载 作者:行者123 更新时间:2023-12-04 15:19:41 36 4
gpt4 key购买 nike

在互联网上摸索了这么多之后,令人惊讶的是,我找不到使用带有 ActiveMQ (Artemis) 的 WildFly 10 中的 JMS 推送到远程消息队列的示例配置。使情况恶化standalone-full.xml未绑定(bind)到模式(为什么???),当我最终找到它的 XSD 时 here on GitHub ,它不包含说明每个节点/属性的含义以及可以放入什么值的文档。

下面是来自standalone-full.xml 的原始配置。

    <subsystem xmlns="urn:jboss:domain:messaging-activemq:1.0">
<server name="default">
<security-setting name="#">
<role name="guest" delete-non-durable-queue="true" create-non-durable-queue="true" consume="true" send="true"/>
</security-setting>
<address-setting name="#" message-counter-history-day-limit="10" page-size-bytes="2097152" max-size-bytes="10485760" expiry-address="jms.queue.ExpiryQueue" dead-letter-address="jms.queue.DLQ"/>
<http-connector name="http-connector" endpoint="http-acceptor" socket-binding="http"/>
<http-connector name="http-connector-throughput" endpoint="http-acceptor-throughput" socket-binding="http">
<param name="batch-delay" value="50"/>
</http-connector>
<in-vm-connector name="in-vm" server-id="0"/>
<http-acceptor name="http-acceptor" http-listener="default"/>
<http-acceptor name="http-acceptor-throughput" http-listener="default">
<param name="batch-delay" value="50"/>
<param name="direct-deliver" value="false"/>
</http-acceptor>
<in-vm-acceptor name="in-vm" server-id="0"/>
<jms-queue name="ExpiryQueue" entries="java:/jms/queue/ExpiryQueue"/>
<jms-queue name="DLQ" entries="java:/jms/queue/DLQ"/>
<connection-factory name="InVmConnectionFactory" entries="java:/ConnectionFactory" connectors="in-vm"/>
<connection-factory name="RemoteConnectionFactory" entries="java:jboss/exported/jms/RemoteConnectionFactory" connectors="http-connector"/>
<pooled-connection-factory name="activemq-ra" transaction="xa" entries="java:/JmsXA java:jboss/DefaultJMSConnectionFactory" connectors="in-vm"/>
</server>
</subsystem>

下面是我的 CDI 队列客户端,它能够将消息发布到 WildFly 中的本地 Artemis 实例。
@ApplicationScoped
public class QueueClient {

private static final Gson GSON = new Gson();

@Resource(mappedName = "java:jboss/DefaultJMSConnectionFactory")
private ConnectionFactory connectionFactory;

public void sendMessage(String destinationName, Object message) throws JMSException {
try (Connection conn = connectionFactory.createConnection();
Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE)) {

Queue queue = session.createQueue(destinationName);
final Message consignment = session.createMessage();
consignment.setStringProperty("MEDIA_TYPE", "application/json");
consignment.setStringProperty("BODY", GSON.toJson(message));
session.createProducer(queue).send(consignment);
}
}
}

我的目标 : 将消息发布到 远程 ActiveMQ 实例。

我有什么 : server url , topic name , usernamepassword .

我的问题 : 我如何修改配置来实现这个目标?

替代问题 : 如果以上都不能回答,那我还怎么实现这个目标呢?

谢谢!

最佳答案

您是否检查过以下文档。

https://docs.jboss.org/author/display/WFLY10/Connect+a+pooled-connection-factory+to+a+Remote+Artemis+Server

它可能解决了您当前的担忧。

谢谢。

编辑:在这篇文章的负面标记。

正如上面列出的文档清楚地描述的那样。
您需要创建一个使用远程连接器的池连接工厂,并且远程连接器引用远程 Active MQ 服务器的 IP x 套接字。
请参阅有关如何执行此操作的文档,前三个步骤。

这种池连接工厂的配置分 3 步完成,我引用:

  1. create an outbound-socket-binding pointing to the remote messaging server: /socket-binding-group=standard-sockets/remote-destination-outbound-socket-binding=remote-artemis:add(host=, port=61616)

  2. create a remote-connector referencing the outbound-socket-binding created at step (1). /subsystem=messaging-activemq/server=default/remote-connector=remote-artemis:add(socket-binding=remote-artemis)

  3. create a pooled-connection-factory referencing the remote-connector created at step (2). /subsystem=messaging-activemq/server=default/pooled-connection-factory=remote-artemis:add(connectors=[remote-artemis], entries=[java:/jms/remoteCF])



最后,一旦正确配置了池连接工厂,就可以将其用作 JEE 容器可用的任何 JNDI 资源。
只需将池连接工厂注入(inject)您的 bean 并使用它来创建 session 。

在这里,您有一个关于如何将 JMS 消息发送到服务器的示例。
在您的情况下,只需将连接工厂 JNDI 引用修复到与远程事件 MQ 关联的池连接工厂。

http://www.mastertheboss.com/jboss-server/jboss-jms/sending-jms-messages-over-xa-with-wildfly-jboss-as

我引用:
@Stateless
public class JMSService {


@Resource(mappedName = "java:jboss/jms/queue/exampleQueue")
private Queue queueExample;

@Resource(mappedName = "java:/JmsXA")
private ConnectionFactory cf;

private Connection connection;
private MessageProducer publisher;
private Session session;

@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void sendMessage(String txt) {

try {

connection = cf.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

publisher = session.createProducer(queueExample);

connection.start();

TextMessage message = session.createTextMessage(txt);
publisher.send(message);


}
catch (Exception exc) {
exc.printStackTrace();
}
finally {

if (publisher != null) try { publisher.close(); } catch (Exception ignore) { }
if (session != null) try { session.close(); } catch (Exception ignore) { }
if (connection != null) try { connection.close(); } catch (Exception ignore) { }


}
}
}

在这种情况下,您的连接工厂应该:
@Inject

 @JMSConnectionFactory("java:/jms/remoteCF")

 private JMSContext context;

您需要注意的另一件事是,您要注入(inject) bean 的队列是远程队列。
访问远程队列的方式是在远程事件 MQ 服务器上执行 JNDI 查找。
这在这里解释。
http://activemq.apache.org/artemis/docs/1.1.0/using-jms.html#jndi-configuration

我引用文档中有趣的部分开始。如果您继续阅读,您会看到如何使用 JNDI 初始上下文来查找 artemis 队列。

And if the client wanted to bind this queue to "queues/OrderQueue" then the JNDI properties would be configured like so:

java.naming.factory.initial=org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory java.naming.provider.url=tcp://myhost:5445 queue.queues/OrderQueue=OrderQueue

It is also possible to look-up JMS destinations which haven't been configured explicitly in the JNDI context environment. This is possible using dynamicQueues/ or dynamicTopics/ in the look-up string. For example, if the client wanted to look-up the aforementioned "OrderQueue" it could do so simply by using the string "dynamicQueues/OrderQueue". Note, the text that follows dynamicQueues/ or dynamicTopics/ must correspond exactly to the name of the destination on the server.



请通读有关此的 artemis 配置。

最后,您可以尝试探索另一种方法。
这些是 JMS 队列桥。
你太谷歌了。
但本质上,JMS 网桥可以允许队列的内容通过隧道传输到远程队列。
因此,这种桥接机制应该使您能够像针对本地 artemis JMS 服务器一样对代码进行编程,但是所有队列都被桥接,正在读取或发布的数据实际上来自或去往远程服务器。

但是你必须告诉自己,看看什么最适合你。

我真诚地希望这会有所帮助。

关于jms - 如何将 JMS 消息从 WildFly 10 发送到远程 ActiveMQ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39284009/

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