事务回滚后如何使游标前进?

时间:2014-12-26 02:15:54

标签: transactions jms activemq

我想从基于ActiveMQ的队列中获取消息,如果消息不被预期,我想回滚事务以使消息离开

但问题是,一旦我回滚该事务,并尝试从队列中再次获取下一条消息,我总是得到第一条消息(这是我回滚的消息)。

所以我的问题是:当我做回滚时,如何让消息的推进向前推进?

以下代码

public void removeMessageFromQuenu(String queueName) throws JMSException {
    String user = this.conf.getProperty("activemq.user");
    String passwd = this.conf.getProperty("activemq.pwd");
    String url = this.conf.getProperty("activemq.url");

    try {

        mq = new ActiveMQFactory(user, passwd, url);
        conn = mq.getConn();
        mq.setTrans(true);
        mq.setAckMode("AUTO_ACKNOWLEDGE");


        ses = mq.getSec();
        destination = ses.createQueue(queueName);

        MessageConsumer consumer = ses.createConsumer(destination);
        System.out
                .println("++++++++++++++++ Summary Info For Message Queue ++++++++++++++++");
        while (true) {
            TextMessage message = (TextMessage) consumer.receive(1000);
            if (null != message) {
                String branch;
                String build_bit;
                String build_type;
                String build_num;
                String cn_url;
                String kr_url;
                branch = message.getStringProperty("BUILD_SVN_BRANCH");
                build_bit = message.getStringProperty("BUILD_BIT");
                build_type = message.getStringProperty("BUILD_TYPE");
                build_num = message.getStringProperty("BUILD_ID");
                cn_url = message.getStringProperty("BUILD_URLS");
                kr_url = message.getStringProperty("BUILD_URLS_KR");
                String key = branch + "_" + build_bit + "bit_" + build_type;

                if (maxVersionList.containsKey(key)
                        && build_num.equals(maxVersionList.get(key))) {
                    System.out.println("Max version of " + key + ":");
                    System.out.println("ID: " + build_num);
                    System.out.println(cn_url);
                    System.out.println(kr_url);
                    System.out
                            .println("------------------------------------------------------ ");
                    ses.rollback();
                } else {
                    ses.commit();
                }

            } else {
                break;
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (ses != null){
            ses.rollback();
            ses.close();}
        if (conn != null)
            conn.close();
        System.out
        .println("==============================================================================================");
        // System.out.println("Remove Done!");
    }

}    

1 个答案:

答案 0 :(得分:0)

我不认为你可以做你特别要求的事情而不做一些疯狂的事情,比如使用QueueBrowser来查看队列中的消息。 QueueBrowser不会将消息从队列中拉出来,我们可能不想开始如何处理竞争条件。

ActiveMQ确实在redelivery policy中有选项可以执行延迟重新发送等操作。如果可能的话,我会试着去那条路。