gpt4 book ai didi

java - 同时向一个 URL 发送多个 POST 请求

转载 作者:可可西里 更新时间:2023-11-01 16:22:55 26 4
gpt4 key购买 nike

<分区>

主要目标是通过 REST 服务将 xml 文件从一个文件夹发送到 Cassandra DB。我想要做的只是读取某个文件夹中的所有文件,并创建一个设置了文件路径的 Worker 对象。

while (RUNS > 0) {
ExecutorService executor = Executors.newFixedThreadPool(N_THREADS);

File dir = new File(PATH_TO_SAMPLES);
File[] listFiles = dir.listFiles();

if (listFiles != null) {

for (File file : listFiles) {

Worker worker = new Worker();
worker.setPath(file.toPath());

executor.submit(worker);
}
}

executor.shutdown();

// Wait until all threads are finish
while (!executor.isTerminated()) {
}

Thread.sleep(1000);

RUNS--;
}

在该执行器获得一个工作实例并转到目录中的下一个文件之后。RUNS 用迭代总数的值初始化,默认为 100_000。N_THREADS - 线程总数,默认设置为 100。

Worker 类实现 Runnable。运行方法:

@Override
public void run() {

String url = getUrl();
String payload = "xml_file_representation";

MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();

HttpClient client = new HttpClient();

HttpConnectionManagerParams httpConnectionManagerParams = new HttpConnectionManagerParams();
connectionManager.setParams(httpConnectionManagerParams);

client.setHttpConnectionManager(connectionManager);

PostMethod postMethod = new PostMethod(url);

try {

postMethod.setRequestHeader("User-Agent", USER_AGENT);
postMethod.setRequestHeader("Content-Type", "application/xml");

postMethod.setRequestEntity(new StringRequestEntity(payload, "application/xml", StandardCharsets.UTF_8.toString()));

int statusCode = client.executeMethod(postMethod);

InputStream body = postMethod.getResponseBodyAsStream();

if (statusCode == HttpStatus.SC_OK) {

//OK
}

} catch (Exception e) {
LOG.error("POST: ERROR!");
} finally {
postMethod.releaseConnection();
connectionManager.shutdown();
}
}

如果我删除等待,即

Thread.sleep(1000);

在运行结束时,当 ~16_000 请求被发送时,我会得到一个异常:

java.net.BindException: Address already in use

它与BindException: address already in use on a client socket? 非常相似

无论如何,接受的答案对我没有帮助。我不知道我需要做什么来关闭这些“连接”以防止该错误。

像 Thread.sleep() 这样的解决方法看起来也不是好的解决方案。感谢您提供任何帮助或建议。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com