void stopProcessing(Collection<TopicPartition> partitions) { partitions.forEach((key) -> { PartitionProcessor processor = processors.get(key); if (processor == null) { logger.warn("Ignored operation: trying to stop a non-existing processor for partition {}", key); return; } processor.stopProcessing(); }); }
void waitForHandlersToTerminate(long timeoutMillis) { stopProcessing(); // ensure that we're shutting down try { boolean terminatedSuccessfully = executor.awaitTermination(timeoutMillis, TimeUnit.MILLISECONDS); if (!terminatedSuccessfully) { logger.warn("PartitionProcessor {}: still running message handlers after waiting {} ms to terminate.", partitionKey, timeoutMillis); } isTerminated.set(true); } catch (InterruptedException e) { logger.warn("PartitionProcessor {}: Interrupted while waiting to terminate.", partitionKey); } }
@Test public void afterProcesserIsStoppedMessagesToBeEnqueuedWillBeIgnored() throws InterruptedException { PartitionProcessor processor = givenAPartionProcessor(); processor.stopProcessing(); ConsumerRecord<String, byte[]> record = testRecordWithOffset(999); processor.enqueue(record); // should log a message that messages are ignored (and never committed to Kafka) // assert handler not called assertFalse((getTestHandler(processor)).onMessageCalled.await(100, TimeUnit.MILLISECONDS)); processor.waitForHandlersToTerminate(1); }
@Test public void shutDownEvenIfHandlerIsStillActive() throws InterruptedException { PartitionProcessor processor = givenAPartionProcessor(); ConsumerRecord<String, byte[]> record = testRecordWithOffset(999); processor.enqueue(record); (getTestHandler(processor)).onMessageCalled.await(); processor.stopProcessing(); assertFalse(processor.isTerminated()); processor.waitForHandlersToTerminate(10); // should log a warn about still running handlers assertTrue(processor.isTerminated()); assertFalse(processor.hasUncommittedMessages()); // If handler finishes, it will mark message as committable, but at this time, the processor is probably already removed. (getTestHandler(processor)).blockReturnFromOnMessage.countDown(); shortSleep(); // allow handler to continue assertTrue(processor.hasUncommittedMessages()); }