@Override public void recordFutureNotification(final DateTime futureNotificationTime, final NotificationEvent event, final UUID userToken, final Long searchKey1, final Long searchKey2) throws IOException { final String eventJson = objectMapper.writeValueAsString(event); final UUID futureUserToken = UUID.randomUUID(); final Long searchKey2WithNull = Objects.firstNonNull(searchKey2, new Long(0)); final NotificationEventModelDao notification = new NotificationEventModelDao(Hostname.get(), clock.getUTCNow(), event.getClass().getName(), eventJson, userToken, searchKey1, searchKey2WithNull, futureUserToken, futureNotificationTime, getFullQName()); dao.insertEntry(notification); }
@Override public void recordFutureNotificationFromTransaction(final Transmogrifier transmogrifier, final DateTime futureNotificationTime, final NotificationEvent event, final UUID userToken, final Long searchKey1, final Long searchKey2) throws IOException { final NotificationSqlDao transactionalNotificationDao = transmogrifier.become(NotificationSqlDao.class); final String eventJson = objectMapper.writeValueAsString(event); final UUID futureUserToken = UUID.randomUUID(); final Long searchKey2WithNull = Objects.firstNonNull(searchKey2, new Long(0)); final NotificationEventModelDao notification = new NotificationEventModelDao(Hostname.get(), clock.getUTCNow(), event.getClass().getName(), eventJson, userToken, searchKey1, searchKey2WithNull, futureUserToken, futureNotificationTime, getFullQName()); dao.insertEntryFromTransaction(transactionalNotificationDao, notification); }
private List<T> fetchReadyEntries(int size) { final Date now = clock.getUTCNow().toDate(); final List<T> entries = sqlDao.getReadyEntries(now, Hostname.get(), size, config.getTableName()); return entries; }
@Override public void post(final BusEvent event) throws EventBusException { try { if (isStarted.get()) { final String json = objectMapper.writeValueAsString(event); final BusEventModelDao entry = new BusEventModelDao(Hostname.get(), clock.getUTCNow(), event.getClass().getName(), json, event.getUserToken(), event.getSearchKey1(), event.getSearchKey2()); dao.insertEntry(entry); } else { log.warn("Attempting to post event " + event + " in a non initialized bus"); } } catch (Exception e) { log.error("Failed to post BusEvent " + event, e); } }
public MockNotificationQueue(final Clock clock, final String svcName, final String queueName, final NotificationQueueHandler handler, final MockNotificationQueueService mockNotificationQueueService) { this.svcName = svcName; this.queueName = queueName; this.handler = handler; this.clock = clock; this.hostname = Hostname.get(); this.queueService = mockNotificationQueueService; this.recordIds = new AtomicLong(); notifications = new TreeSet<NotificationEventModelDao>(new Comparator<NotificationEventModelDao>() { @Override public int compare(final NotificationEventModelDao o1, final NotificationEventModelDao o2) { if (o1.getEffectiveDate().equals(o2.getEffectiveDate())) { return o1.getRecordId().compareTo(o2.getRecordId()); } else { return o1.getEffectiveDate().compareTo(o2.getEffectiveDate()); } } }); }
private boolean claimEntry(T entry) { final Date nextAvailable = clock.getUTCNow().plus(config.getClaimedTime().getMillis()).toDate(); final boolean claimed = (sqlDao.claimEntry(entry.getRecordId(), clock.getUTCNow().toDate(), Hostname.get(), nextAvailable, config.getTableName()) == 1); if (claimed && log.isDebugEnabled()) { log.debug(DB_QUEUE_LOG_ID + "Claiming entry " + entry.getRecordId()); } return claimed; }
private void clearFailedNotification(final NotificationEventModelDao cleared) { NotificationEventModelDao processedEntry = new NotificationEventModelDao(cleared, Hostname.get(), clock.getUTCNow(), PersistentQueueEntryLifecycleState.FAILED); dao.moveEntryToHistory(processedEntry); }
@Override public void postFromTransaction(final BusEvent event, final Transmogrifier transmogrifier) throws EventBusException { try { final PersistentBusSqlDao transactional = transmogrifier.become(PersistentBusSqlDao.class); if (isStarted.get()) { final String json = objectMapper.writeValueAsString(event); final BusEventModelDao entry = new BusEventModelDao(Hostname.get(), clock.getUTCNow(), event.getClass().getName(), json, event.getUserToken(), event.getSearchKey1(), event.getSearchKey2()); dao.insertEntryFromTransaction(transactional, entry); } else { log.warn("Attempting to post event " + event + " in a non initialized bus"); } } catch (Exception e) { log.error("Failed to post BusEvent " + event, e); } } }
private void clearNotification(final NotificationEventModelDao cleared) { NotificationEventModelDao processedEntry = new NotificationEventModelDao(cleared, Hostname.get(), clock.getUTCNow(), PersistentQueueEntryLifecycleState.PROCESSED); dao.moveEntryToHistory(processedEntry); }
@Override public void removeNotificationFromTransaction(final Transmogrifier transmogrifier, final Long recordId) { final NotificationSqlDao transactional = transmogrifier.become(NotificationSqlDao.class); final NotificationEventModelDao existing = transactional.getByRecordId(recordId, config.getTableName()); final NotificationEventModelDao removedEntry = new NotificationEventModelDao(existing, Hostname.get(), clock.getUTCNow(), PersistentQueueEntryLifecycleState.REMOVED); dao.moveEntryToHistoryFromTransaction(transactional, removedEntry); }
@Override public void removeNotification(final Long recordId) { final NotificationEventModelDao existing = dao.getSqlDao().getByRecordId(recordId, config.getTableName()); final NotificationEventModelDao removedEntry = new NotificationEventModelDao(existing, Hostname.get(), clock.getUTCNow(), PersistentQueueEntryLifecycleState.REMOVED); dao.moveEntryToHistory(removedEntry); }
} finally { if (lastException == null) { BusEventModelDao processedEntry = new BusEventModelDao(cur, Hostname.get(), clock.getUTCNow(), PersistentQueueEntryLifecycleState.PROCESSED); dao.moveEntryToHistory(processedEntry); } else if (errorCount <= config.getMaxFailureRetries()) { log.info("Bus dispatch error, will attempt a retry ", lastException); BusEventModelDao retriedEntry = new BusEventModelDao(cur, Hostname.get(), clock.getUTCNow(), PersistentQueueEntryLifecycleState.AVAILABLE, errorCount); dao.updateOnError(retriedEntry); } else { log.error("Fatal Bus dispatch error, data corruption...", lastException); BusEventModelDao processedEntry = new BusEventModelDao(cur, Hostname.get(), clock.getUTCNow(), PersistentQueueEntryLifecycleState.FAILED); dao.moveEntryToHistory(processedEntry);
} else if (errorCount <= config.getMaxFailureRetries()) { log.info("NotificationQ dispatch error, will attempt a retry ", lastException); NotificationEventModelDao failedNotification = new NotificationEventModelDao(cur, Hostname.get(), clock.getUTCNow(), PersistentQueueEntryLifecycleState.AVAILABLE, errorCount); dao.updateOnError(failedNotification); } else {
final DateTime effDt = new DateTime(); final NotificationEventModelDao notif = new NotificationEventModelDao(Hostname.get(), clock.getUTCNow(), eventJson.getClass().getName(), eventJson, UUID.randomUUID(), searchKey1, SEARCH_KEY_2, UUID.randomUUID(), effDt, "testBasic"); NotificationEventModelDao notificationHistory = new NotificationEventModelDao(notification, Hostname.get(), processedTime, PersistentQueueEntryLifecycleState.PROCESSED); dao.insertEntry(notificationHistory, notificationQueueConfig.getHistoryTableName()); assertEquals(notificationHistory.getEventJson(), eventJson); validateDate(notificationHistory.getEffectiveDate(), effDt); assertEquals(notificationHistory.getProcessingOwner(), Hostname.get()); assertEquals(notificationHistory.getProcessingState(), PersistentQueueEntryLifecycleState.PROCESSED); validateDate(notificationHistory.getNextAvailableDate(), processedTime);
private int doProcessEventsForQueue(final MockNotificationQueue queue) { int result = 0; final List<NotificationEventModelDao> processedNotifications = new ArrayList<NotificationEventModelDao>(); final List<NotificationEventModelDao> oldNotifications = new ArrayList<NotificationEventModelDao>(); List<NotificationEventModelDao> readyNotifications = queue.getReadyNotifications(); for (final NotificationEventModelDao cur : readyNotifications) { final NotificationEvent key = deserializeEvent(cur.getClassName(), objectMapper, cur.getEventJson()); queue.getHandler().handleReadyNotification(key, cur.getEffectiveDate(), cur.getFutureUserToken(), cur.getSearchKey1(), cur.getSearchKey2()); final NotificationEventModelDao processedNotification = new NotificationEventModelDao(cur.getRecordId(), Hostname.get(), Hostname.get(), clock.getUTCNow(), getClock().getUTCNow().plus(CLAIM_TIME_MS), PersistentQueueEntryLifecycleState.PROCESSED, cur.getClassName(), cur.getEventJson(), 0L, cur.getUserToken(), cur.getSearchKey1(), cur.getSearchKey2(), cur.getFutureUserToken(), cur.getEffectiveDate(), "MockQueue"); oldNotifications.add(cur); processedNotifications.add(processedNotification); result++; } queue.markProcessedNotifications(oldNotifications, processedNotifications); return result; }