gpt4 book ai didi

java - 带有消息监听器的事务处理 session ,未使用消息

转载 作者:行者123 更新时间:2023-12-02 06:55:22 25 4
gpt4 key购买 nike

我正在使用 Websphere java 类在我的应用程序中实现 jms。

发件人代码:

        MQQueueConnectionFactory connectionFactory = new MQQueueConnectionFactory();
connectionFactory.setHostName(environment.getProperty(MQ_CONNECTION_HOSTNAME));
connectionFactory.setPort(Integer.parseInt(environment.getProperty(MQ_CONNECTION_PORT)));
connectionFactory.setQueueManager(environment.getProperty(MQ_CONNECTION_QMANAGER));
connectionFactory.setChannel(environment.getProperty(MQ_CONNECTION_CHANNEL));
connectionFactory.setTransportType(1);
final String username = environment.getProperty(MQ_CONNECTION_USERNAME);
final String password = environment.getProperty(MQ_CONNECTION_PASSWORD);
MQQueueConnection connection = null;
if(username != null && username.trim().length() > 0 && password != null && password.trim().length() > 0) {
connection = (MQQueueConnection) connectionFactory.createQueueConnection(username, password);
}
else {
connection = (MQQueueConnection) connectionFactory.createQueueConnection();
}
senderSession = (MQQueueSession) connection.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
MQQueue queue = (MQQueue) senderSession.createQueue("queue:///" + environment.getProperty(MQ_CONNECTION_QUEUE));
MQQueueSender sender = (MQQueueSender) senderSession.createSender(queue);
JMSMessage message = (JMSMessage)senderSession.createTextMessage(messageContent);
connection.start();
sender.send(message);
message.acknowledge()

接收者代码:

 final MQQueueConnection connection = (MQQueueConnection) (useAuth ? connectionFactory.createQueueConnection(username, password) :      connectionFactory.createQueueConnection());
connection.start();
final MQQueueSession receiverSession = (MQQueueSession) connection.createQueueSession(true, Session.CLIENT_ACKNOWLEDGE);
final MQQueue queue = MQQueue)receiverSession.createQueue(queueName);
MQQueueReceiver receiver = createReceiver(session, queue);
receiver.setMessageListener(listener);

当我使用这些设置发送消息时,消息监听器永远不会收到任何消息。但是当我以错误的方式打开两个 session 时,一切似乎都工作正常。我能理解原因。我想要进行交易 session 。

如果需要任何其他详细信息,请告诉我

最佳答案

如果您想使用本地事务(同步点),那么在发送消息后您需要提交它。即

senderSession.commit();

关于java - 带有消息监听器的事务处理 session ,未使用消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17445057/

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