Use semaphore instead of AtomicInteger counter
This commit is contained in:
parent
d9f23cdff9
commit
ec93c4b726
1 changed files with 16 additions and 17 deletions
|
|
@ -21,14 +21,14 @@ import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.BlockingQueue;
|
import java.util.concurrent.BlockingQueue;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.Semaphore;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
public class KafkaMonitoringService {
|
public class KafkaMonitoringService {
|
||||||
private volatile boolean isShuttingdDown = false;
|
private volatile boolean isShuttingdDown = false;
|
||||||
private final AtomicInteger activeProcessCount = new AtomicInteger(0);
|
private final Semaphore processingPermits = new Semaphore(8); // количество равно concurency листенера
|
||||||
|
|
||||||
private final BlockingQueue<SignalKafkaEvent> signalEventsQueue;
|
private final BlockingQueue<SignalKafkaEvent> signalEventsQueue;
|
||||||
|
|
||||||
|
|
@ -53,17 +53,22 @@ public class KafkaMonitoringService {
|
||||||
)
|
)
|
||||||
public void listen(List<PackageMessage> records,
|
public void listen(List<PackageMessage> records,
|
||||||
Acknowledgment acknowledgment) {
|
Acknowledgment acknowledgment) {
|
||||||
|
if (!processingPermits.tryAcquire()) {
|
||||||
|
log.warn("KafkaMonitoringService is shutting down, rejecting new messages");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
if (isShuttingdDown) {
|
if (isShuttingdDown) {
|
||||||
log.warn("KafkaMonitoringService is shutting down, rejecting new messages");
|
log.warn("KafkaMonitoringService is shutting down, rejecting new messages");
|
||||||
return; // без ака, завершаем обработку сообщений, которые уже попали в очередь, новые не обрабатываем
|
return; // без ака, завершаем обработку сообщений, которые уже попали в очередь, новые не обрабатываем
|
||||||
}
|
}
|
||||||
try {
|
|
||||||
acknowledgment.acknowledge();
|
acknowledgment.acknowledge();
|
||||||
activeProcessCount.incrementAndGet();
|
|
||||||
process(records);
|
process(records);
|
||||||
log.info("Processed {} messages from Kafka", records.size());
|
log.info("Processed {} messages from Kafka", records.size());
|
||||||
} finally {
|
} finally {
|
||||||
activeProcessCount.decrementAndGet();
|
processingPermits.release();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -149,17 +154,12 @@ public class KafkaMonitoringService {
|
||||||
}
|
}
|
||||||
|
|
||||||
@PreDestroy
|
@PreDestroy
|
||||||
public void stopKafkaMonitoringService() {
|
public void stopKafkaMonitoringService() throws InterruptedException {
|
||||||
log.info("Stopping KafkaMonitoringService...");
|
log.info("Stopping KafkaMonitoringService...");
|
||||||
isShuttingdDown = true;
|
isShuttingdDown = true;
|
||||||
|
|
||||||
while (activeProcessCount.get() > 0) {
|
for (int i = 0; i < 8; i++) {
|
||||||
try {
|
processingPermits.acquire();
|
||||||
Thread.sleep(1000);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
Thread.currentThread().interrupt();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
@ -172,9 +172,8 @@ public class KafkaMonitoringService {
|
||||||
stopEvent.setEventType(EventType.STOP);
|
stopEvent.setEventType(EventType.STOP);
|
||||||
signalEventsQueue.put(stopEvent);
|
signalEventsQueue.put(stopEvent);
|
||||||
log.info("STOP event sent to queue");
|
log.info("STOP event sent to queue");
|
||||||
} catch (InterruptedException e) {
|
} finally {
|
||||||
log.error("Error while sending ROTATE and STOP event", e);
|
processingPermits.release();
|
||||||
Thread.currentThread().interrupt();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log.info("KafkaMonitoringService stopped");
|
log.info("KafkaMonitoringService stopped");
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue