gpt4 book ai didi

java - 如何检查天气客户端是否在 java 中使用 PoolingHttpClientConnectionManager

转载 作者:太空宇宙 更新时间:2023-11-03 14:10:00 26 4
gpt4 key购买 nike

我正在尝试使用我正在使用的客户端代码在服务器上使用连接池(已配置 Https SSL)PoolingHttpClientConnectionManager 和我有一个带连接池的工作代码,但我想知道我的代码是否正在使用 PoolingHttpClientConnectionManager 或没有如何弄清楚

如果我的代码没有使用池管理器如何让它使用它

我的代码:

static PoolingHttpClientConnectionManager cm ;
static CloseableHttpClient httpClient;
static
{


SslConfigurator sslConfig = SslConfigurator.newInstance()
.securityProtocol("TLS")
.keyStoreFile("/path")
.keyStorePassword("passw")
.keyStoreType("JKS")
.trustStoreFile("/path");

SSLContext sslCtx = sslConfig.createSSLContext();
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslCtx,NoopHostnameVerifier.INSTANCE);
HttpClientContext clientContext = HttpClientContext.create();


final Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory> create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", sslSocketFactory)
.build();


cm = new PoolingHttpClientConnectionManager(registry);

client = HttpClients.custom()
.setSSLSocketFactory(sslSocketFactory)
.setConnectionManager(cm)
.build();

}
public static void main(String a[]) throws ClientProtocolException, IOException, JSONException
{

JSONObject jsonResponse;




StringEntity se = new StringEntity(jsonRequest.toString());

HttpPost httpPost = new HttpPost(path);
httpPost.setEntity(se);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
httpPost.setHeader("Connection", "keep-alive");
CloseableHttpResponse response2;
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss:SSS");



int i;
for(i=0;i<10;i++)
{
response2 = client.execute(httpPost);

System.out.println(response2.getStatusLine());
HttpEntity entity2 = response2.getEntity();

String result = EntityUtils.toString(entity2);
System.out.println(result);

Date date = new Date();
System.out.println(dateFormat.format(date));
response2.close();
}

}

最佳答案

当遇到如此复杂的问题时,这里有一些经验法则......

创建一个简单的类,比如“BrokenPHCCM”,它扩展了 org.apache.http.impl.conn.PoolingHttpClientConnectionManager 并且在这个新类中只需覆盖 org.apache.http。 impl.conn.PoolingHttpClientConnectionManager#connect 方法基本上抛出 java.lang.RuntimeException

当您要将此类型的实例传递给 http 客户端构建器 .setConnectionManager(brokenCM) 并尝试连接时,您应该得到运行时异常,对吧?

这是我的简化版本

public static class BrokenPoolingHttpClientConnectionManager
extends PoolingHttpClientConnectionManager
{

public BrokenPoolingHttpClientConnectionManager(
Registry<ConnectionSocketFactory> socketFactoryRegistry) {

super(socketFactoryRegistry);
}

@Override
public void connect(
HttpClientConnection managedConn,
HttpRoute route,
int connectTimeout,
HttpContext context) throws IOException {

throw new RuntimeException("As expected");
}
}

static PoolingHttpClientConnectionManager cm;
static CloseableHttpClient httpClient;

static {

HttpClientContext clientContext = HttpClientContext.create();

final Registry<ConnectionSocketFactory>
registry =
RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.build();

cm = new BrokenPoolingHttpClientConnectionManager(registry);

httpClient = HttpClients.custom()
.setConnectionManager(cm)
.build();

}


public static void main(String a[]) throws ClientProtocolException, IOException, JSONException {

HttpGet httpRequest = new HttpGet("http://www.google.com/");
httpRequest.setHeader("Connection", "keep-alive");
CloseableHttpResponse response2;
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss:SSS");

int i;
for (i = 0; i < 10; i++) {
response2 = httpClient.execute(httpRequest);

System.out.println(String.format("response status=%s", response2.getStatusLine()));

String result = EntityUtils.toString(response2.getEntity());
System.out.println(String.format("response size=%d", result.length()));

Date date = new Date();
System.out.println(dateFormat.format(date));
response2.close();
}
}

您的控制台将显示如下内容:

Exception in thread "main" java.lang.RuntimeException: As expected at com.something.Demo$BrokenPoolingHttpClientConnectionManager.connect(Demo.java:43) at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:363) at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:219) at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195) at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:86) at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108) at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184) at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82) at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106) at com.something.Demo.main(Demo.java:78) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

除此之外,您应该依赖文档和诸如 java 的静态类型方面之类的东西:)

关于java - 如何检查天气客户端是否在 java 中使用 PoolingHttpClientConnectionManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32753257/

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