将 activemq 配置为事务性的
Configure activemq to be transactional
这更像是一个概念性问题:我目前有一个正在工作的 activemq 队列,它被 Java Spring 应用程序使用。现在我希望队列不会永久删除消息,直到 Java 应用程序告诉它消息已正确保存在数据库中。阅读文档后,我知道我必须使用 commit() / rollback() 方法进行事务处理。如果我在这里错了,请纠正我。
我在 Internet 上找到的每个示例都告诉我配置应用程序以这种或那种方式工作,但我的鼻子告诉我我应该设置队列本身以按照我想要的方式工作。而且我找不到方法。
否则,队列是否只是根据消费者应用程序的工作配置以不同的方式工作?我哪里错了?
提前致谢
队列本身不知道任何事务系统,但您可以将第一个参数布尔值传递给 true 以创建事务会话,但我建议在创建会话时使用 INDIVIDUAL_ACKNOWLEDGE,因为您可以管理消息一个。可以在 spring jms DefaultMessageListenerContainer
.
上设置
ActiveMQSession.INDIVIDUAL_ACKNOWLEDGE
并调用此方法来确认消息,除非未调用该方法消息被视为已派发但未确认。
ActiveMQTextMessage.acknowledge();
更新:
ActiveMQSession.INDIVIDUAL_ACKNOWLEDGE
可以这样使用:
onMessage(ActiveMQTextMessage message)
try {
do some stuff in the database
jdbc.commit(); (unless auto-commit is enabled on the JDBC)
message.acknowledge();
}
catch (Exception e) {
}
There are 2 kinds of transaction support in ActiveMQ.
JMS transactions - the commit() / rollback() methods on a Session (which is like doing commit() / rollback() on a JDBC connection)
XA Transactions - where the XASession acts as an XAResource by communicating with the Message Broker, rather like a JDBC Connection takes place in an XA transaction by communicating with the database.
http://activemq.apache.org/how-do-transactions-work.html
Should I use XA transactions (two phase commit?)
A common use of JMS is to consume messages from a queue or topic, process them using a database or EJB, then acknowledge / commit the message.
If you are using more than one resource; e.g. reading a JMS message and writing to a database, you really should use XA - its purpose is to provide atomic transactions for multiple transactional resources. For example there is a small window from when you complete updating the database and your changes are committed up to the point at which you commit/acknowledge the message; if there is a network/hardware/process failure inside that window, the message will be redelivered and you may end up processing duplicates.
这更像是一个概念性问题:我目前有一个正在工作的 activemq 队列,它被 Java Spring 应用程序使用。现在我希望队列不会永久删除消息,直到 Java 应用程序告诉它消息已正确保存在数据库中。阅读文档后,我知道我必须使用 commit() / rollback() 方法进行事务处理。如果我在这里错了,请纠正我。
我在 Internet 上找到的每个示例都告诉我配置应用程序以这种或那种方式工作,但我的鼻子告诉我我应该设置队列本身以按照我想要的方式工作。而且我找不到方法。
否则,队列是否只是根据消费者应用程序的工作配置以不同的方式工作?我哪里错了?
提前致谢
队列本身不知道任何事务系统,但您可以将第一个参数布尔值传递给 true 以创建事务会话,但我建议在创建会话时使用 INDIVIDUAL_ACKNOWLEDGE,因为您可以管理消息一个。可以在 spring jms DefaultMessageListenerContainer
.
ActiveMQSession.INDIVIDUAL_ACKNOWLEDGE
并调用此方法来确认消息,除非未调用该方法消息被视为已派发但未确认。
ActiveMQTextMessage.acknowledge();
更新:
ActiveMQSession.INDIVIDUAL_ACKNOWLEDGE
可以这样使用:
onMessage(ActiveMQTextMessage message)
try {
do some stuff in the database
jdbc.commit(); (unless auto-commit is enabled on the JDBC)
message.acknowledge();
}
catch (Exception e) {
}
There are 2 kinds of transaction support in ActiveMQ. JMS transactions - the commit() / rollback() methods on a Session (which is like doing commit() / rollback() on a JDBC connection) XA Transactions - where the XASession acts as an XAResource by communicating with the Message Broker, rather like a JDBC Connection takes place in an XA transaction by communicating with the database.
http://activemq.apache.org/how-do-transactions-work.html
Should I use XA transactions (two phase commit?) A common use of JMS is to consume messages from a queue or topic, process them using a database or EJB, then acknowledge / commit the message. If you are using more than one resource; e.g. reading a JMS message and writing to a database, you really should use XA - its purpose is to provide atomic transactions for multiple transactional resources. For example there is a small window from when you complete updating the database and your changes are committed up to the point at which you commit/acknowledge the message; if there is a network/hardware/process failure inside that window, the message will be redelivered and you may end up processing duplicates.