数据库异常后,MDB中没有消息重新传递

时间:2015-01-13 09:01:44

标签: java glassfish jms

情景:

在onMessage of my MDB中我基本上做了:

  • 打印标签
  • 更新数据库说:“标签已打印”

什么附加(由于某种原因)我有一个内部数据库异常,因此在我看来,事务被回滚,消息被重新传递,并且我有一个无限循环打印标签(无限打印)。

我想要的不是重新传达信息。

在发布此问题之前,我阅读了这些文章:

我正在使用Glassfish 2.1,ActiveMQ,EclipseLink,方法onMessage包含try / catch子句,我记录了异常

代码段:

@MessageDriven(mappedName = JMSConstants.COMMAND_SPOOL_TOPIC_JNDI, activationConfig = {
    @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"),
    @ActivationConfigProperty(propertyName = "subscriptionDurability", propertyValue = "Durable"),
    @ActivationConfigProperty(propertyName = "clientId", propertyValue = "PrinterCommandSpoolMessageBean"),
    @ActivationConfigProperty(propertyName = "subscriptionName", propertyValue = "PrinterCommandSpoolMessageBean"),
    @ActivationConfigProperty(propertyName = "messageSelector", propertyValue = JMSSelector.PRINT_COMMAND_SELECTOR)
})
public class PrinterCommandSpoolMessageBean implements MessageListener {

    private final static Logger LOGGER = Logger.getLogger(PrinterCommandSpoolMessageBean.class.getName());

    @EJB
    private CoreServiceRemote coreService;

    @Override
    public void onMessage(Message message) {

        LOGGER.info("*********************  message arrived ********************");
        if (message instanceof MapMessage) {

            CommandSpool cs = null;
            try {
                MapMessage mapMessage = (MapMessage) message;

                //missing code

                if (actualCopies >= copies) {
                    coreService.updateCommandSpool(cs.getId(), CommandSpoolStatus.EXECUTED);            
                    coreService.setSensorIssued(ps.getSensorUUID());    
                } else {
                    coreService.updateCommandSpool(cs.getId(), CommandSpoolStatus.NOT_EXECUTED);
                }

            } catch (Exception ex) {
                LOGGER.log(Level.WARNING, "Exception", ex);
                if (cs != null) {
                    coreService.resetCommandSpool(cs.getId());
                }

        }
    }
}

1 个答案:

答案 0 :(得分:1)

你确定coreService.resetCommandSpool(cs.getId())没有抛出另一个异常吗?

鉴于您捕获了所有异常,您希望MDB是事务性的吗?您总是可以在ejb方法上启动新事务。