gpt4 book ai didi

java - JMS消息生产者不会将消息发送到队列

转载 作者:行者123 更新时间:2023-11-30 07:13:47 25 4
gpt4 key购买 nike

我设置了一个 EJB 项目来使用 JMS 将持久性实体对象发送到 MDB。我正在使用 JBoss EAP 7,并使用 Apache ActiveMQ 作为消息传递提供程序。我像这样设置 ConnectionFactory 和 Queue:

AccountAccessQueue

ConnectionFactory

这是我的消息生成器,它接收“Account”实体对象作为参数并将其发送到队列:

EntityEnqueueBean.java

@Stateless
@LocalBean
public class EntityEnqueueBean {

Context context = null;

public void enqueueEntity(Account accountEntity) {
try {
context = new InitialContext();
ConnectionFactory connectionFactory = (ConnectionFactory) context.lookup("java:/ConnectionFactory");
Destination queue = (Destination) context.lookup("java:/jms/queue/AccountAccessQueue");
Connection connection = connectionFactory.createConnection();
Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
MessageProducer messageProducer = session.createProducer(queue);
ObjectMessage objectMessage = session.createObjectMessage(accountEntity);
messageProducer.send(objectMessage);

messageProducer.close();
session.close();
connection.close();
} catch (JMSException | NamingException e) {
e.printStackTrace();
}
}
}

MDB从队列接收消息并处理它:

AccountPersistenceMDB.java

@MessageDriven(
activationConfig = { @ActivationConfigProperty(
propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
@ActivationConfigProperty( propertyName = "destination",
propertyValue ="java:/jms/queue/AccountAccessQueue")
})
public class AccountPersitenceMDB implements MessageListener {

public void onMessage(Message message) {
ObjectMessage objectMessage = null;
objectMessage = (ObjectMessage) message;
try {
Account accountEntity = (Account) objectMessage.getObject();
System.out.println("MDB accessCount: " + accountEntity.getAccessCount());
} catch (JMSException e) {
e.printStackTrace();
}
}
}

不知道我做错了什么。我可能设置 ConnectionFactory 和队列的代码是错误的,但我不确定,因为我是 Java EE 的新手。如有任何帮助,我们将不胜感激。

最佳答案

感谢@mendieta,我解决了这个问题。我阅读了 JMS session ,它非常有意义。更改 EntityEnqueueBean.java 中的这一行可以解决问题:

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

关于java - JMS消息生产者不会将消息发送到队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38761481/

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