packagecn.rc.controller;importcn.rc.common.entity.JsonResponse;importcn.rc.filter.AccessLimit;importcn.rc.rpc.TestLimitApi;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.PathVariable;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;import java.util.*;import java.util.concurrent.*;
@RestController
@RequestMapping("test")public classTestAccessLimitController {
@AutowiredprivateTestLimitApi testLimitApi;//CountDownLatch count = new CountDownLatch(3);
@GetMapping("accessLimit")public JsonResponse testAccessLimit() throwsInterruptedException, ExecutionException {
System.out.println(newDate());//List list1 = this.getList1();//List list2 = this.getList2();//List list3 = this.getList3();//Map map = new HashMap<>();//map.put("aList",list1);//map.put("bList",list2);//map.put("cList",list3);
ThreadPoolExecutor poolExecutor= new ThreadPoolExecutor(5, 10, 2000, TimeUnit.SECONDS, new ArrayBlockingQueue(5));
List1 task1= newList1();
List2 task2= newList2();
List3 task3= newList3();
Future submit1=poolExecutor.submit(task1);
Future submit2=poolExecutor.submit(task2);
Future submit3=poolExecutor.submit(task3);//try {//count.await();//} catch (InterruptedException e) {//e.printStackTrace();//}
poolExecutor.shutdown();
Map map= newHashMap();
map.put("aList",submit1.get());
map.put("bList",submit2.get());
map.put("cList",submit3.get());
System.out.println(newDate());return new JsonResponse(1,"success",map);
}class List1 implementsCallable{
@Overridepublic List call() throwsException {
List list1=getList1();//count.countDown();
returnlist1;
}
}class List2 implementsCallable{
@Overridepublic List call() throwsException {
List list2=getList2();//count.countDown();
returnlist2;
}
}class List3 implementsCallable{
@Overridepublic List call() throwsException {
List list3=getList3();//count.countDown();
returnlist3;
}
}
@GetMapping("slep/{id}")public JsonResponse slep(@PathVariable("id") Integer id) {returntestLimitApi.testLimit(id);
}publicList getList1() {try{
Thread.sleep(5000);
}catch(Exception e){
}
List arrayList= newArrayList();
arrayList.add(1);returnarrayList;
}publicList getList2(){try{
Thread.sleep(10000);
}catch(Exception e){
}
List arrayList= newArrayList();
arrayList.add(2);returnarrayList;
}publicList getList3(){try{
Thread.sleep(20000);
}catch(Exception e){
}
List arrayList= newArrayList();
arrayList.add(3);returnarrayList;
}
}