• AsyncHttpSupport并发发送请求


    public class AsyncHttpSupportTest {

    @InjectMocks
    private AsyncHttpSupport asyncHttpSupport;
    @Mock
    private HttpSupport support;


    public static void main(String[] args) throws InterruptedException {
    AsyncHttpSupport asyncHttpSupport = new AsyncHttpSupport();

    asyncHttpSupport.addDefaultHttpHeader("Cache-Control", "no-Cache");
    asyncHttpSupport.addDefaultHttpHeader("X-Service-Chain", "prj00804");
    asyncHttpSupport.addDefaultHttpHeader("Content-Type", "application/json");

    String test = "{"bizContent":"{\"kdtId\":\"30338372\",\"mchId\":\"30338372\"}","method":"test2","service":"XXX.pay.test","partnerId": "820000000003", "sign":"38F7FBBA82AA6424A69F810E95059FC5", "version": "1.0.3"}";
    String url = "http://10.9.169.40:7001/gw";

    String queryBankCardList = "{"bizContent":"{}","method":"bankinfo","service":"XXX.pay.merchant.withdraw","partnerId": "820000000003", "sign":"38F7FBBA82AA6424A69F810E95059FC5", "version": "1.0.0"}";

    AtomicInteger reject = new AtomicInteger(0);
    AtomicInteger counter = new AtomicInteger(0);

    Thread[] pools = new Thread[Runtime.getRuntime().availableProcessors()];

    for (int i = 0; i < Runtime.getRuntime().availableProcessors(); i++) {
    new Thread(() -> {
    try {
    for (int j = 0; j < 100; j++) {
    counter.incrementAndGet();
    Future<HttpResponse> res = asyncHttpSupport.post(url,j % 2 == 0 ? test : queryBankCardList);
    HttpResponse httpResponse = res.get();
    if (EntityUtils.toString(httpResponse.getEntity()).contains(""code":50002")) {
    reject.incrementAndGet();
    }
    }
    } catch (Exception e) {
    }
    }
    ).start();
    }
    TimeUnit.SECONDS.sleep(50);
    System.out.println(reject.get());
    System.out.println(counter.get());
    }

  • 相关阅读:
    Redis常见数据类型二:Hash
    Redis常见数据类型一:String
    了解Docker
    微信小程序倒计时秒杀
    笛卡尔积求二维数组所有组合
    git好网站网址搜集
    npm i 报错Can't find Python executable "python2.7", you can set the PYTHON env variable
    css_注意小事项总结_随时更新
    echarts使用时报错cannot read property 'querycomponents' of undefined解决方案
    echarts中获取各个省份地图的链接
  • 原文地址:https://www.cnblogs.com/ceshi2016/p/9969340.html
Copyright © 2020-2023  润新知