@Test public void testDuplicateInProcess() { IncomingMessage existingMsg = new IncomingMessageImpl(); existingMsg.setMessageStatus(IncMessageStatus.PROCESSING); existingMsg.setContent(message); expect(mockCoreMgr.createIncomingMessageDAO()).andReturn(mockMsgDao); expect(mockMsgDao.getByContentNonDuplicatable(message)).andReturn( existingMsg); replay(mockCoreMgr, mockMsgDao, mockParser); try { instance.registerMessage(message); fail("should fail with duplicate exception"); } catch (DuplicateProcessingException e) { } catch (DuplicateMessageException de) { fail("should fail with duplicate processing exception"); } verify(mockCoreMgr, mockMsgDao, mockParser); }
@SuppressWarnings("unchecked") @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = DuplicateMessageException.class) public IncomingMessage registerMessage(String message) throws DuplicateMessageException { IncomingMessageDAO<IncomingMessage> msgDao = coreManager .createIncomingMessageDAO(); Calendar cal = Calendar.getInstance(); cal.add(Calendar.MINUTE, 0 - duplicatePeriod); Date beforeDate = cal.getTime(); IncomingMessage existingMsg = msgDao .getByContentNonDuplicatable(message); if (existingMsg != null) { if (existingMsg.getMessageStatus() == IncMessageStatus.PROCESSING) throw new DuplicateProcessingException( "message is already processing"); IncomingMessageForm existingMsgForm = existingMsg .getIncomingMessageForm(); if (existingMsgForm != null && (existingMsgForm.getMessageFormStatus() == IncMessageFormStatus.SERVER_VALID) && (existingMsgForm.getIncomingMsgFormDefinition() .getDuplicatable() == Duplicatable.DISALLOWED || (existingMsgForm .getIncomingMsgFormDefinition().getDuplicatable() == Duplicatable.TIME_BOUND && existingMsg .getDateCreated().after(beforeDate)))) { throw new DuplicateMessageException("message exists"); } } IncomingMessage newMsg = parser.parseRequest(message); return msgDao.save(newMsg); }
MessageRegistryImpl implements MessageRegistry { @SuppressWarnings("unchecked") @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = DuplicateMessageException.class) public IncomingMessage registerMessage(String message) throws DuplicateMessageException { IncomingMessageDAO<IncomingMessage> msgDao = coreManager .createIncomingMessageDAO(); Calendar cal = Calendar.getInstance(); cal.add(Calendar.MINUTE, 0 - duplicatePeriod); Date beforeDate = cal.getTime(); IncomingMessage existingMsg = msgDao .getByContentNonDuplicatable(message); if (existingMsg != null) { if (existingMsg.getMessageStatus() == IncMessageStatus.PROCESSING) throw new DuplicateProcessingException( "message is already processing"); IncomingMessageForm existingMsgForm = existingMsg .getIncomingMessageForm(); if (existingMsgForm != null && (existingMsgForm.getMessageFormStatus() == IncMessageFormStatus.SERVER_VALID) && (existingMsgForm.getIncomingMsgFormDefinition() .getDuplicatable() == Duplicatable.DISALLOWED || (existingMsgForm .getIncomingMsgFormDefinition().getDuplicatable() == Duplicatable.TIME_BOUND && existingMsg .getDateCreated().after(beforeDate)))) { throw new DuplicateMessageException("message exists"); } } IncomingMessage newMsg = parser.parseRequest(message); return msgDao.save(newMsg); } }
MessageRegistryImpl implements MessageRegistry { @SuppressWarnings("unchecked") @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = DuplicateMessageException.class) public IncomingMessage registerMessage(String message) throws DuplicateMessageException { IncomingMessageDAO<IncomingMessage> msgDao = coreManager .createIncomingMessageDAO(); Calendar cal = Calendar.getInstance(); cal.add(Calendar.MINUTE, 0 - duplicatePeriod); Date beforeDate = cal.getTime(); IncomingMessage existingMsg = msgDao .getByContentNonDuplicatable(message); if (existingMsg != null) { if (existingMsg.getMessageStatus() == IncMessageStatus.PROCESSING) throw new DuplicateProcessingException( "message is already processing"); IncomingMessageForm existingMsgForm = existingMsg .getIncomingMessageForm(); if (existingMsgForm != null && (existingMsgForm.getMessageFormStatus() == IncMessageFormStatus.SERVER_VALID) && (existingMsgForm.getIncomingMsgFormDefinition() .getDuplicatable() == Duplicatable.DISALLOWED || (existingMsgForm .getIncomingMsgFormDefinition().getDuplicatable() == Duplicatable.TIME_BOUND && existingMsg .getDateCreated().after(beforeDate)))) { throw new DuplicateMessageException("message exists"); } } IncomingMessage newMsg = parser.parseRequest(message); return msgDao.save(newMsg); }  }
MessageRegistryImpl implements MessageRegistry { @SuppressWarnings("unchecked") @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = DuplicateMessageException.class) public IncomingMessage registerMessage(String message) throws DuplicateMessageException { IncomingMessageDAO<IncomingMessage> msgDao = coreManager .createIncomingMessageDAO(); Calendar cal = Calendar.getInstance(); cal.add(Calendar.MINUTE, 0 - duplicatePeriod); Date beforeDate = cal.getTime(); IncomingMessage existingMsg = msgDao .getByContentNonDuplicatable(message); if (existingMsg != null) { if (existingMsg.getMessageStatus() == IncMessageStatus.PROCESSING) throw new DuplicateProcessingException( "message is already processing"); IncomingMessageForm existingMsgForm = existingMsg .getIncomingMessageForm(); if (existingMsgForm != null && (existingMsgForm.getMessageFormStatus() == IncMessageFormStatus.SERVER_VALID) && (existingMsgForm.getIncomingMsgFormDefinition() .getDuplicatable() == Duplicatable.DISALLOWED || (existingMsgForm .getIncomingMsgFormDefinition().getDuplicatable() == Duplicatable.TIME_BOUND && existingMsg .getDateCreated().after(beforeDate)))) { throw new DuplicateMessageException("message exists"); } } IncomingMessage newMsg = parser.parseRequest(message); return msgDao.save(newMsg); }  @SuppressWarnings("unchecked") @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = DuplicateMessageException.class) IncomingMessage registerMessage(String message); void setDuplicatePeriod(int duplicatePeriod); void setCoreManager(CoreManager coreManager); void setParser(IncomingMessageParser parser); }
MessageRegistryImpl implements MessageRegistry { @SuppressWarnings("unchecked") @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = DuplicateMessageException.class) public IncomingMessage registerMessage(String message) throws DuplicateMessageException { IncomingMessageDAO<IncomingMessage> msgDao = coreManager .createIncomingMessageDAO(); Calendar cal = Calendar.getInstance(); cal.add(Calendar.MINUTE, 0 - duplicatePeriod); Date beforeDate = cal.getTime(); IncomingMessage existingMsg = msgDao .getByContentNonDuplicatable(message); if (existingMsg != null) { if (existingMsg.getMessageStatus() == IncMessageStatus.PROCESSING) throw new DuplicateProcessingException( "message is already processing"); IncomingMessageForm existingMsgForm = existingMsg .getIncomingMessageForm(); if (existingMsgForm != null && (existingMsgForm.getMessageFormStatus() == IncMessageFormStatus.SERVER_VALID) && (existingMsgForm.getIncomingMsgFormDefinition() .getDuplicatable() == Duplicatable.DISALLOWED || (existingMsgForm .getIncomingMsgFormDefinition().getDuplicatable() == Duplicatable.TIME_BOUND && existingMsg .getDateCreated().after(beforeDate)))) { throw new DuplicateMessageException("message exists"); } } IncomingMessage newMsg = parser.parseRequest(message); return msgDao.save(newMsg); }  @SuppressWarnings("unchecked") @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = DuplicateMessageException.class) IncomingMessage registerMessage(String message); void setDuplicatePeriod(int duplicatePeriod); void setCoreManager(CoreManager coreManager); void setParser(IncomingMessageParser parser);  }