我基本上做到:
- 打印標籤
- 更新數據庫話說:「標籤被印有」
什麼顯然是(出於某種原因)我有一個內部數據庫異常,因此在我看來,事務回滾,消息被重新遞送,我有一個無限打印標籤中的循環(無限打印)。
我想要的不是重新傳遞消息。
發佈這個問題之前,我閱讀這些文章:
- - https://www.java.net/forum/topic/glassfish/glassfish/infinite-message-redelivery-case-databaseexception-commit-time
- http://www.postseek.com/meta/cfc9cd4b1d2fe4b8a921f4b32e355c35
我使用Glassfish的2.1,ActiveMQ的,的EclipseLink,方法的onMessage包含嘗試/在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());
}
}
}
}
消息重新傳遞與事務回滾相關聯。被捕獲的異常是被捕獲的嗎?記錄後再次扔掉嗎? – ramp
我添加了一個片段,我不再拋出異常 – pikimota
失敗的方法是setSensorIssued() – pikimota