有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

多线程如何在java中从一个线程运行多个get请求?

我想设置一个带有x个连接的客户端,每个连接都会随机向不同的URL发送一个新的GET请求

因此,我的方法是创建x个线程,每个线程将在循环中发送GET请求(直到程序终止)。 我使用的是Apache多线程HttpConnectionManager,其中我将可能的连接数设置为x


    ClientHandler(int numberOfClients) {
        this.numberOfClients = numberOfClients;

        connManager = new PoolingHttpClientConnectionManager();
        HttpClientBuilder clientBuilder = HttpClients.custom().setConnectionManager(connManager);

        connManager.setMaxTotal(numberOfClients);
        CloseableHttpClient httpClient = clientBuilder.build();

        generateMultiThreadedClient(httpClient);
    }

    private void generateMultiThreadedClient(CloseableHttpClient httpClient) {

        for (int i = 0; i < numberOfClients; i++) {
            String clientUrl = URL + i;
            HttpGet httpGet = new HttpGet(clientUrl);

            ClientMultiThreaded clientMultiThreaded = new ClientMultiThreaded(httpClient,httpGet, i);
            LOGGER.info(CLIENT_LOG + "A new thread for clientId " + i + " was created.");

            clientMultiThreaded.start();
            LOGGER.info(CLIENT_LOG + "Thread for clientId " + i + " started..");

        }
    }

这是线程的run()方法

ClientMultiThreaded(CloseableHttpClient httpClient, HttpGet httpGet, int clientId) {
        this.httpClient = httpClient;
        this.clientId = clientId;
        this.httpGet = httpGet;
        randomGenerator = new Random();
    }

    @Override
    public void run() {
        try{
            // Execute request for the first time
            executeRequest(httpGet);

            while (true) {
                int timeToSleep = randomGenerator.nextInt(BOUND_LIMIT) + 1;
                LOGGER.info(CLIENT_LOG + "Thread id " + clientId + " went to sleep for " + timeToSleep / 1000 + " seconds");
                sleep(timeToSleep);

                LOGGER.info(" ------- This is a test log printing for clientId: " +clientId);
                executeRequest(httpGet);
            }
        }catch(Exception e) {
            LOGGER.info(e.getMessage());
        }
    }

    private void executeRequest(HttpGet httpGet) throws IOException {
        CloseableHttpResponse response = httpClient.execute(httpGet);
        LOGGER.info(CLIENT_LOG + "clientId " + clientId + " sent get request to URL " + httpGet.getURI());

        statusCode = this.response.getStatusLine().getStatusCode();
        LOGGER.info(CLIENT_LOG + "Status received for clientId " + clientId + " is: " + statusCode);
    }

问题是我实际上收到了两个get请求,然后它就停止了


共 (1) 个答案

  1. # 1 楼答案

    您需要关闭响应,使用try with resources 使用以下代码:

    private void executeRequest(HttpGet httpGet) throws IOException {
        try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
            LOGGER.info(CLIENT_LOG + "clientId " + clientId + " sent get request to URL " + httpGet.getURI());
    
            statusCode = this.response.getStatusLine().getStatusCode();
            LOGGER.info(CLIENT_LOG + "Status received for clientId " + clientId + " is: " + statusCode);
        }
    }