zoukankan      html  css  js  c++  java
  • 编写测试类实现并发访问固定URL(亲测能用!!!)

    1.类目录

     2.LatchTest.java类

    package com.test;

    import java.util.concurrent.CountDownLatch;

    public class LatchTest {
    static Runnable taskTemp = new Runnable() {

    // 注意,此处是非线程安全的留坑
    private int iCounter;

    @Override
    public void run() {
    System.out.println("=====222========");
    for(int i = 0; i < 10; i++) {
    // 发起请求
    //http://localhost:8080/insertUser?name=赵云&age=18&id=18
    //http://localhost:8080/insertUser?name=aaa&age=18&id=1811

    try {
    HttpClientOp.doGet("http://localhost:8080/insertUser?name=aaa&age=18&id=1811");//自己的URL地址,注意:name=汉字会报错
    } catch (Exception e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    iCounter++;
    System.out.println(System.nanoTime() + " [" + Thread.currentThread().getName() + "] iCounter = " + iCounter);
    try {
    Thread.sleep(100);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }
    };
    public static void main(String[] args) throws InterruptedException {
    System.out.println("=====111========");
    taskTemp.run();

    LatchTest latchTest = new LatchTest();
    latchTest.startTaskAllInOnce(5, taskTemp);
    }

    public long startTaskAllInOnce(int threadNums, final Runnable task) throws InterruptedException {
    final CountDownLatch startGate = new CountDownLatch(1);
    final CountDownLatch endGate = new CountDownLatch(threadNums);
    for(int i = 0; i < threadNums; i++) {
    Thread t = new Thread() {
    public void run() {
    try {
    // 使线程在此等待,当开始门打开时,一起涌入门中
    startGate.await();
    try {
    task.run();
    } finally {
    // 将结束门减1,减到0时,就可以开启结束门了
    endGate.countDown();
    }
    } catch (InterruptedException ie) {
    ie.printStackTrace();
    }
    }
    };
    t.start();
    }
    long startTime = System.nanoTime();
    System.out.println(startTime + " [" + Thread.currentThread() + "] All thread is ready, concurrent going...");
    // 因开启门只需一个开关,所以立马就开启开始门
    startGate.countDown();
    // 等等结束门开启
    endGate.await();
    long endTime = System.nanoTime();
    System.out.println(endTime + " [" + Thread.currentThread() + "] All thread is completed.");
    return endTime - startTime;
    }
    }

    2.HttpClientOp类

    package com.test;

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;

    public class HttpClientOp {
    public static String doGet(String httpurl) throws Exception {
    HttpURLConnection connection = null;
    InputStream is = null;
    BufferedReader br = null;
    String result = null;// 返回结果字符串
    // try {
    // 创建远程url连接对象
    URL url = new URL(httpurl);
    // 通过远程url连接对象打开一个连接,强转成httpURLConnection类
    connection = (HttpURLConnection) url.openConnection();
    System.out.println("======333======="+connection.getContent());
    // 设置连接方式:get
    connection.setRequestMethod("GET");
    // 设置连接主机服务器的超时时间:15000毫秒
    connection.setConnectTimeout(15000);
    // 设置读取远程返回的数据时间:60000毫秒
    connection.setReadTimeout(60000);
    // 发送请求
    connection.connect();
    // 通过connection连接,获取输入流
    /* if (connection.getResponseCode() == 200) {
    is = connection.getInputStream();
    // 封装输入流is,并指定字符集
    br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
    // 存放数据
    StringBuffer sbf = new StringBuffer();
    String temp = null;
    while ((temp = br.readLine()) != null) {
    sbf.append(temp);
    sbf.append(" ");
    }
    result = sbf.toString();
    }
    } catch (MalformedURLException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }*/ /*finally {
    // 关闭资源
    if (null != br) {
    try {
    br.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    if (null != is) {
    try {
    is.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    //connection.disconnect();// 关闭远程连接
    }

    */

    //绿色部分不要加,加上之后测试不能通过
    br.close();
    is.close();
    return result;
    }

    public static String doPost(String httpUrl, String param) {

    HttpURLConnection connection = null;
    InputStream is = null;
    OutputStream os = null;
    BufferedReader br = null;
    String result = null;
    try {
    URL url = new URL(httpUrl);
    // 通过远程url连接对象打开连接
    connection = (HttpURLConnection) url.openConnection();
    // 设置连接请求方式
    connection.setRequestMethod("POST");
    // 设置连接主机服务器超时时间:15000毫秒
    connection.setConnectTimeout(15000);
    // 设置读取主机服务器返回数据超时时间:60000毫秒
    connection.setReadTimeout(60000);

    // 默认值为:false,当向远程服务器传送数据/写数据时,需要设置为true
    connection.setDoOutput(true);
    // 默认值为:true,当前向远程服务读取数据时,设置为true,该参数可有可无
    connection.setDoInput(true);
    // 设置传入参数的格式:请求参数应该是 name1=value1&name2=value2 的形式。
    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    // 设置鉴权信息:Authorization: Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0
    connection.setRequestProperty("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0");
    // 通过连接对象获取一个输出流
    os = connection.getOutputStream();
    // 通过输出流对象将参数写出去/传输出去,它是通过字节数组写出的
    os.write(param.getBytes());
    // 通过连接对象获取一个输入流,向远程读取
    if (connection.getResponseCode() == 200) {

    is = connection.getInputStream();
    // 对输入流对象进行包装:charset根据工作项目组的要求来设置
    br = new BufferedReader(new InputStreamReader(is, "UTF-8"));

    StringBuffer sbf = new StringBuffer();
    String temp = null;
    // 循环遍历一行一行读取数据
    while ((temp = br.readLine()) != null) {
    sbf.append(temp);
    sbf.append(" ");
    }
    result = sbf.toString();
    }
    } catch (MalformedURLException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    // 关闭资源
    if (null != br) {
    try {
    br.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    if (null != os) {
    try {
    os.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    if (null != is) {
    try {
    is.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    // 断开与远程地址url的连接
    connection.disconnect();
    }
    return result;
    }
    }

    4.测试截图:

     报错问题没有解决,但是确实能入库操作实现并行访问,截图如下:

     

     5.HttpClientOp.doGet("http://localhost:8080/insertUser?name=aaa&age=18&id=1811");//自己的URL地址,注意:name=汉字会报错

    报错问题,大家谁解决了,欢迎回复,谢谢!!!

  • 相关阅读:
    HTTP请求中的GET-POST方式
    拦截器与过滤器的不同点
    SQL练习题(一)
    Maven聚合工程安装时排除掉不参与本次安装的子工程
    codeforce 796C
    [CF1216E] Numerical Sequence hard version
    【floyd+矩阵乘法】POJ 3613 Cow Relays
    BZOJ 3573米特运输
    Poj 3977 Subset
    【树形dp】Bzoj 1040骑士
  • 原文地址:https://www.cnblogs.com/curedfisher/p/11810373.html
Copyright © 2011-2022 走看看