com.raynigon.ecs.logging.async.service.DefaultAsyncService Maven / Gradle / Ivy
package com.raynigon.ecs.logging.async.service;
import com.raynigon.ecs.logging.async.executor.MdcForkJoinPool;
import com.raynigon.ecs.logging.async.service.helper.SampleWrapper;
import com.raynigon.ecs.logging.async.service.helper.TimerWrapper;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.stereotype.Service;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ForkJoinTask;
import java.util.function.Supplier;
@Service
@AutoConfiguration
@RequiredArgsConstructor
public class DefaultAsyncService implements AsyncService {
private final MdcForkJoinPool forkJoinPool;
private final AsyncMetricsService metrics;
private static final Logger log = LoggerFactory.getLogger(DefaultAsyncService.class);
@Override
public CompletableFuture supplyAsync(Supplier supplier) {
TimerWrapper queueTimer = metrics.createQueueTimer(supplier.getClass());
TimerWrapper execTimer = metrics.createExecutionTimer(supplier.getClass());
SampleWrapper sample = queueTimer.start();
Supplier wrapped = () -> {
long nanoseconds = queueTimer.stop(sample);
log.trace("The supplier {} took {} ms in Queue", supplier.getClass(), nanoseconds / 1000000.0);
return execTimer.record(supplier);
};
log.trace("Add supplier {} to ForkJoinPool", supplier);
return CompletableFuture.supplyAsync(wrapped, forkJoinPool);
}
@Override
public ForkJoinTask submit(Callable callable) {
TimerWrapper queueTimer = metrics.createQueueTimer(callable.getClass());
TimerWrapper execTimer = metrics.createExecutionTimer(callable.getClass());
SampleWrapper sample = queueTimer.start();
Callable wrapped = () -> {
long nanoseconds = queueTimer.stop(sample);
log.trace("The callable {} took {} ms in Queue", callable.getClass(), nanoseconds / 1000000.0);
return execTimer.recordCallable(callable);
};
return forkJoinPool.submit(wrapped);
}
}