- public class ReplayWithoutProblem {
- public List < String > loadMockRequest(int n) {
- List < String > cache = new ArrayList < String > (n);
- for (int i = 0; i < n; i++) {
- cache.add("http://www.baidu.com?a=" + i);
- }
- return cache;
- }
- public void start(List < String > cache) throws InterruptedException {
- HttpAsyncClient httpClient = new HttpAsyncClient();
- int i = 0;
- while (true) {
- String url = cache.get(i % cache.size());
- final HttpGet request = new HttpGet(url);
- httpClient.execute(request, new FutureCallback < HttpResponse > () {
- public void completed(final HttpResponse response) {
- System.out.println(request.getRequestLine() + "->" + response.getStatusLine());
- }
- public void failed(final Exception ex) {
- System.out.println(request.getRequestLine() + "->" + ex);
- }
- public void cancelled() {
- System.out.println(request.getRequestLine() + " cancelled");
- }
- });
- i++;
- Thread.sleep(100);
- }
- }
- }
来源: https://juejin.im/entry/59ddcff86fb9a0452340da0e