调用setRollbackOnly

时间:2019-02-27 18:59:00

标签: jms websphere websphere-8 jms-topic

我正在实现一个JMS MDB解决方案,以调用从应用程序中的多个点触发的Web服务。为了验证我的解决方案,我现在仅传递Integer对象。我强迫代码在Integer大于5时重新传递消息。但是,如果邮件已重新发送两次,则应忽略/提交。在Websphere中配置的等待时间为5分钟,每条消息的最大失败传递次数为0(在“总线上的异常目标”下-> {总线}->目标-> {目标}下)。

问题是当对象6的消息在5分钟后发送回队列重新发送时,7、8、9 ...的消息也被保留,直到6重新交付。虽然我知道JMS不保证消息的传递顺序,但是通常它是FIFO。但是,一旦将一条消息发送回队列并重新传递,其余消息的顺序就不是连续的。即它类似于6、8、9、7。 在“ JMS激活”规范下,最大批处理大小为1,每个端点的最大并发MDB调用为10。

有什么主意如何处理由于保留邮件而被阻止的新邮件?我在此链接上提到了这个问题,它说不应阻塞队列。

JMS and MDB with setRollbackOnly

还请提供有关维护消息顺序的建议。

代码段:

public void onMessage(javax.jms.Message msg) {
    try {
        if (msg instanceof ObjectMessage) {
            ObjectMessage objMsg = (ObjectMessage)msg;
            Object obj = objMsg.getObject();

            UserTransaction userTxn = getMessageDrivenContext().getUserTransaction();
            userTxn.begin();
            InitialContext initialContext = new InitialContext();
            ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("jms/cf");
            javax.jms.Connection conn = cf.createConnection();
            Session jmsSession = conn.createSession(true, -1);
            Queue queue = (Queue) initialContext.lookup("jms/queue1");
            QueueBrowser queueBrowser = jmsSession.createBrowser(queue);
            Enumeration enumeration = queueBrowser.getEnumeration();
            while (enumeration.hasMoreElements()) {
                System.out.println("Browse [" + enumeration.nextElement() + "]");
            }
            if ((Integer)obj >= 6) {
                int count = Integer.valueOf(msg.getStringProperty("JMSXDeliveryCount"));
                if (count > 2) {
                    System.out.println("********** max rollback");
                    userTxn.commit();
                } else {
                    System.out.println("********** rollback");
                    userTxn.setRollbackOnly();
                }
            } else {
                System.out.println("********** commit");
                userTxn.commit();
            }
        } else {
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new EJBException("Transaction failed: " + ex.getMessage());
    } catch (Throwable t) {
    }       
}

0 个答案:

没有答案