List of usage examples for java.util Map notify
@HotSpotIntrinsicCandidate public final native void notify();
From source file:org.killbill.notificationq.TestNotificationQueue.java
@Test(groups = "slow") public void testManyNotifications() throws Exception { final Map<NotificationEvent, Boolean> expectedNotifications = new TreeMap<NotificationEvent, Boolean>(); final NotificationQueue queue = queueService.createNotificationQueue("test-svc", "many", new NotificationQueueHandler() { @Override/*from www. j a v a 2 s .c om*/ public void handleReadyNotification(final NotificationEvent eventJson, final DateTime eventDateTime, final UUID userToken, final Long searchKey1, final Long searchKey2) { synchronized (expectedNotifications) { log.info("Handler received key: " + eventJson.toString()); expectedNotifications.put(eventJson, Boolean.TRUE); expectedNotifications.notify(); } } }); queue.startQueue(); final DateTime now = clock.getUTCNow(); final int MAX_NOTIFICATIONS = 100; for (int i = 0; i < MAX_NOTIFICATIONS; i++) { final int nextReadyTimeIncrementMs = 1000; final UUID key = UUID.randomUUID(); final int currentIteration = i; final NotificationEvent eventJson = new TestNotificationKey(new Integer(i).toString()); expectedNotifications.put(eventJson, Boolean.FALSE); final DBI dbi = getDBI(); dbi.inTransaction(new TransactionCallback<Object>() { @Override public Object inTransaction(final Handle conn, final TransactionStatus status) throws Exception { queue.recordFutureNotificationFromTransaction(conn.getConnection(), now.plus((currentIteration + 1) * nextReadyTimeIncrementMs), eventJson, TOKEN_ID, SEARCH_KEY_1, SEARCH_KEY_2); return null; } }); // Move time in the future after the notification effectiveDate if (i == 0) { ((ClockMock) clock).setDeltaFromReality(nextReadyTimeIncrementMs); } else { ((ClockMock) clock).addDeltaFromReality(nextReadyTimeIncrementMs); } } // Wait a little longer since there are a lot of callback that need to happen int nbTry = MAX_NOTIFICATIONS + 1; boolean success = false; do { synchronized (expectedNotifications) { final Collection<Boolean> completed = Collections2.filter(expectedNotifications.values(), new Predicate<Boolean>() { @Override public boolean apply(final Boolean input) { return input; } }); if (completed.size() == MAX_NOTIFICATIONS) { success = true; break; } log.info(String.format("BEFORE WAIT : Got %d notifications at time %s (real time %s)", completed.size(), clock.getUTCNow(), new DateTime())); expectedNotifications.wait(1000); } } while (nbTry-- > 0); queue.stopQueue(); log.info("GOT SIZE " + Collections2.filter(expectedNotifications.values(), new Predicate<Boolean>() { @Override public boolean apply(final Boolean input) { return input; } }).size()); assertEquals(success, true); }
From source file:org.killbill.notificationq.TestNotificationQueue.java
/** * Test that we can post a notification in the future from a transaction and get the notification * callback with the correct key when the time is ready * * @throws Exception/* www . j a v a 2 s. c om*/ */ @Test(groups = "slow") public void testSimpleNotification() throws Exception { final Map<NotificationEvent, Boolean> expectedNotifications = new TreeMap<NotificationEvent, Boolean>(); final NotificationQueue queue = queueService.createNotificationQueue("test-svc", "foo", new NotificationQueueHandler() { @Override public void handleReadyNotification(final NotificationEvent eventJson, final DateTime eventDateTime, final UUID userToken, final Long searchKey1, final Long searchKey2) { synchronized (expectedNotifications) { log.info("Handler received key: " + eventJson); expectedNotifications.put(eventJson, Boolean.TRUE); expectedNotifications.notify(); } } }); queue.startQueue(); final DateTime now = new DateTime(); final DateTime readyTime = now.plusMillis(2000); final UUID key1 = UUID.randomUUID(); final NotificationEvent eventJson1 = new TestNotificationKey(key1.toString()); expectedNotifications.put(eventJson1, Boolean.FALSE); final DBI dbi = getDBI(); dbi.inTransaction(new TransactionCallback<Object>() { @Override public Object inTransaction(final Handle conn, final TransactionStatus status) throws Exception { queue.recordFutureNotificationFromTransaction(conn.getConnection(), readyTime, eventJson1, TOKEN_ID, 1L, SEARCH_KEY_2); log.info("Posted key: " + eventJson1); return null; } }); final UUID key2 = UUID.randomUUID(); final NotificationEvent eventJson2 = new TestNotificationKey(key2.toString()); expectedNotifications.put(eventJson2, Boolean.FALSE); dbi.inTransaction(new TransactionCallback<Object>() { @Override public Object inTransaction(final Handle conn, final TransactionStatus status) throws Exception { queue.recordFutureNotificationFromTransaction(conn.getConnection(), readyTime, eventJson2, TOKEN_ID, SEARCH_KEY_1, 1L); log.info("Posted key: " + eventJson2); return null; } }); final UUID key3 = UUID.randomUUID(); final NotificationEvent eventJson3 = new TestNotificationKey(key3.toString()); expectedNotifications.put(eventJson3, Boolean.FALSE); dbi.inTransaction(new TransactionCallback<Object>() { @Override public Object inTransaction(final Handle conn, final TransactionStatus status) throws Exception { queue.recordFutureNotificationFromTransaction(conn.getConnection(), readyTime, eventJson3, TOKEN_ID, SEARCH_KEY_1, SEARCH_KEY_2); log.info("Posted key: " + eventJson3); return null; } }); final UUID key4 = UUID.randomUUID(); final NotificationEvent eventJson4 = new TestNotificationKey(key4.toString()); expectedNotifications.put(eventJson4, Boolean.FALSE); dbi.inTransaction(new TransactionCallback<Object>() { @Override public Object inTransaction(final Handle conn, final TransactionStatus status) throws Exception { queue.recordFutureNotificationFromTransaction(conn.getConnection(), readyTime, eventJson4, TOKEN_ID, SEARCH_KEY_1, SEARCH_KEY_2); log.info("Posted key: " + eventJson4); return null; } }); Assert.assertEquals(queue.getInProcessingNotifications().size(), 0); final List<NotificationEventWithMetadata<TestNotificationKey>> futuresAll = queue .getFutureNotificationForSearchKeys(SEARCH_KEY_1, SEARCH_KEY_2); Assert.assertEquals(futuresAll.size(), 2); int found = 0; for (int i = 0; i < 2; i++) { if (futuresAll.get(i).getEvent().getValue().equals(key3.toString()) || futuresAll.get(i).getEvent().getValue().equals(key4.toString())) { found++; } } Assert.assertEquals(found, 2); final List<NotificationEventWithMetadata<TestNotificationKey>> futures2 = queue .getFutureNotificationForSearchKey2(SEARCH_KEY_2); Assert.assertEquals(futures2.size(), 3); found = 0; for (int i = 0; i < 3; i++) { if (futures2.get(i).getEvent().getValue().equals(key3.toString()) || futures2.get(i).getEvent().getValue().equals(key4.toString()) || futures2.get(i).getEvent().getValue().equals(key1.toString())) { found++; } } Assert.assertEquals(found, 3); // Move time in the future after the notification effectiveDate ((ClockMock) clock).setDeltaFromReality(3000); // Notification should have kicked but give it at least a sec' for thread scheduling await().atMost(1, MINUTES).until(new Callable<Boolean>() { @Override public Boolean call() throws Exception { return expectedNotifications.get(eventJson1) && expectedNotifications.get(eventJson2) && expectedNotifications.get(eventJson3) && expectedNotifications.get(eventJson4); } }); queue.stopQueue(); }