, - ( ), , . , Guava RateLimiter, , , Spring AOP.
- , - ExecutorService:
@Service
public class DocumentService {
private final ExecutorService executor;
@Autowired
public DocumentService(
@Value("${some.config.property}") int maxConcurrentThreads) {
executor = Executors.newFixedThreadPool(maxConcurrentThreads);
}
private void doReplacementWithLimitedConcurrency(String s, int i){
Future<?> future = executor.submit(() -> doReplacement(s, i));
future.get();
}
private void doReplacement(String s, int i){
}
@PreDestroy
public void performThreadPoolCleanup() throws Exception {
executor.shutdown();
executor.awaitTermination(10, TimeUnit.SECONDS);
executor.shutdownNow();
}
}