我设置了一个EJB项目,使用JMS将持久性实体对象发送到MDB。我使用Apache ActiveMQ作为消息传递提供程序使用JBoss EAP 7。我像这样设置了ConnectionFactory和Queue:
这是我的消息制作人,他们收到了一个'帐户'实体对象作为参数并将其发送到队列:
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的新手。任何帮助表示赞赏。
答案 0 :(得分:1)
感谢@mendieta我解决了这个问题。我读了JMS会话,这很有道理。在 EntityEnqueueBean.java 中更改此行就可以解决问题:
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);