【发布时间】:2019-02-05 12:16:57
【问题描述】:
考虑以下代码:
@RestController
@RequestMapping("/requestLimit")
public class TestController {
@Autowired
private TestService service;
@GetMapping("/max3")
public String max3requests() {
return service.call();
}
}
@Service
public class TestService {
public String call() {
//some business logic here
return response;
}
}
我想要完成的是,如果 TestService 中的方法 call 同时被 3 个线程执行,则下一次执行会生成带有 HttpStatus.TOO_MANY_REQUESTS 代码的响应。
【问题讨论】:
-
您必须在控制器类中保留一个原子变量并递增。在你的方法中增加它并检查它是否超过 3,如果是,则返回,否则继续
-
谢谢@pvpkiran,有了你的推荐,我设法提出了一个答案
-
使用专为此设计的
Semaphore。
标签: spring-boot threadpool spring-restcontroller