DailyStatisticsFeeder implements ApplicationListener<MockInvocationEvent> { @Override @Async public void onApplicationEvent(MockInvocationEvent event){ log.debug("Received a MockInvocationEvent on " + event.getServiceName() + " - v" + event.getServiceVersion()); Calendar calendar = Calendar.getInstance(); calendar.setTime(event.getInvocationTimestamp()); int month = calendar.get(Calendar.MONTH) + 1; String monthStr = (month<10 ? "0" : "") + String.valueOf(month); int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH); String dayOfMonthStr = (dayOfMonth<10 ? "0" : "") + String.valueOf(dayOfMonth); String day = String.valueOf(calendar.get(Calendar.YEAR)) + monthStr + dayOfMonthStr; String hourKey = String.valueOf(calendar.get(Calendar.HOUR_OF_DAY)); String minuteKey = String.valueOf((60 * calendar.get(Calendar.HOUR_OF_DAY)) + calendar.get(Calendar.MINUTE)); if (log.isDebugEnabled()){ log.debug("hourKey for statistic is " + hourKey); log.debug("minuteKey for statistic is " + minuteKey); } DailyStatistic statistic = statisticsRepository.findByDayAndServiceNameAndServiceVersion(day, event.getServiceName(), event.getServiceVersion()); if (statistic == null){ log.debug("There's no statistics for " + day + " yet. Create one...."); statistic = new DailyStatistic(); statistic.setDay(day); statistic.setServiceName(event.getServiceName()); statistic.setServiceVersion(event.getServiceVersion()); statistic.setHourlyCount(initializeHourlyMap()); statistic.setMinuteCount(initializeMinuteMap()); statistic.setDailyCount(1); statistic.getHourlyCount().put(hourKey, 1); statistic.getMinuteCount().put(minuteKey, 1); statisticsRepository.save(statistic); } else { log.debug("Found an existing statistic document for " + day); statisticsRepository.incrementDailyStatistic(day, event.getServiceName(), event.getServiceVersion(), hourKey, minuteKey); } log.debug("Processing of MockInvocationEvent done !"); }  @Override @Async void onApplicationEvent(MockInvocationEvent event);  }
@Test public void testOnApplicationEvent() { Calendar today = Calendar.getInstance(); MockInvocationEvent event = new MockInvocationEvent(this, "TestService1", "1.0", "123456789", today.getTime(), 100); feeder.onApplicationEvent(event); SimpleDateFormat formater = new SimpleDateFormat("yyyyMMdd"); String day = formater.format(today.getTime()); DailyStatistic stat = statisticsRepository.findByDayAndServiceNameAndServiceVersion(day, "TestService1", "1.0"); assertNotNull(stat); assertNotNull(stat.getId()); assertEquals(day, stat.getDay()); assertEquals("TestService1", stat.getServiceName()); assertEquals("1.0", stat.getServiceVersion()); assertEquals(1, stat.getDailyCount()); assertEquals(new Integer(1), stat.getHourlyCount().get( String.valueOf(today.get(Calendar.HOUR_OF_DAY)) )); feeder.onApplicationEvent(event); stat = statisticsRepository.findByDayAndServiceNameAndServiceVersion(day, "TestService1", "1.0"); assertNotNull(stat); assertNotNull(stat.getId()); assertEquals(day, stat.getDay()); assertEquals("TestService1", stat.getServiceName()); assertEquals("1.0", stat.getServiceVersion()); assertEquals(2, stat.getDailyCount()); assertEquals(new Integer(2), stat.getHourlyCount().get( String.valueOf(today.get(Calendar.HOUR_OF_DAY)) )); }