简体   繁体   English

如何在Activemq中回滚消息

[英]How to rollback messages in Activemq

I want to send acknowledgement to Activemq when variable x value is equals to 1 .If it not equals to 1 ,I want to redeliver messages to Activemq . 我想送确认到Activemq时候变量x值等于1 。如果它不等于1 ,我想重新传递消息, Activemq Then only Activemq delivers that messages again to subscribers.For this I written the following programs. 然后只有Activemq再次将该消息传递给订阅者。为此,我编写了以下程序。

MessageConsumer.java : MessageConsumer.java:

 public class Consumer extends HttpServlet {
 @Override
 protected void service(HttpServletRequest arg0, HttpServletResponse arg1)
    throws ServletException, IOException {
  try {
    ActiveMQConnectionFactory connectionFactory=new ActiveMQConnectionFactory("admin","admin","tcp://localhost:61617");
RedeliveryPolicy policy = new RedeliveryPolicy();
policy.setInitialRedeliveryDelay(1000L);
policy.setMaximumRedeliveries(RedeliveryPolicy.NO_MAXIMUM_REDELIVERIES);
connectionFactory.setRedeliveryPolicy(policy);
connectionFactory.setUseRetroactiveConsumer(true);
Connection connection=connectionFactory.createConnection();
final Session session=connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
Topic queue=session.createTopic("MessageTesting");
javax.jms.MessageConsumer consumer=session.createConsumer(queue);
//anonymous class
MessageListener listener = new MessageListener() {
        @Override
    public void onMessage(Message msg) {
        TextMessage msg1=(TextMessage)msg;
        try {
                String messageBody=msg1.getText();
                if (x==1) {
                   //Process was completely done,so I am sending acknowledge
                   session.commit();
                }  
                else {
                   //Process is not done sucessfully, So I want to redeliver messages, For this
                   session.rollback();
                 }
           }
          catch (Exception e) {
                 e.printStackTrace();
           }

    }
};
consumer.setMessageListener(listener);
connection.start();
}
 }

Is this correct way to do this.can you suggest me,Is there any efficient way. 这是这样做的正确方法吗?您能建议我吗,有什么有效的方法吗?

Thanks. 谢谢。

Ideally, you will have to : 理想情况下,您将必须:

Create a ActiveMQConnectionFactory to your binding address, Create a RedeliveryPolicy and set it to ActiveMQConnectionFactory . 创建一个ActiveMQConnectionFactory到您的绑定地址,创建一个RedeliveryPolicy并将其设置为ActiveMQConnectionFactory Create a Session and use session.commit() if true and session.rollback() if it failed. 创建一个会话 ,使用session.commit()如果真和session.rollback(),如果它失败了。

These two links might help you: 这两个链接可能会帮助您:

http://activemq.apache.org/message-redelivery-and-dlq-handling.html http://activemq.apache.org/message-redelivery-and-dlq-handling.html

http://activemq.apache.org/redelivery-policy.html http://activemq.apache.org/redelivery-policy.html

public class Consumer extends HttpServlet {
@Override
protected void service(HttpServletRequest arg0, HttpServletResponse arg1)
throws ServletException, IOException {
try {
...
MessageListener listener = new MessageListener() {
public void onMessage(Message msg) {
....
}
};
....
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM