我正在努力实现自动点击几个服务器URL(现在一个接一个地手动命中),只需单击一下,作为响应,我将获得每个响应的登录页面,这意味着jvm已启动并正在运行
问题是jvm可能需要5分钟来响应,并且有多达10个URL,因此检查响应代码所花费的总时间最多需要50分钟(最坏的情况 - 所有jvms都关闭)。
所以,我必须做两件事 - 只需一次点击即可实现我们即将点击的服务器网址(这将使整个响应时间从50分钟大幅降低到5-6分钟)。我使用了PoolingHttpClientConnectionManager方法 -
`PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
CloseableHttpClient httpClient = HttpClients.custom()
.setConnectionManager(cm)
.build();
static int statusCode;
// Here the servers string array have URLs defined in JSP Page -
<!-- <form name="input" action="welcome" method="get">
<input type="checkbox" name="server" value="http://hc.apache.org/" checked>Apache HC Home Page<br>
<input type="checkbox" name="server" value="http://hc.apache.org/httpcomponents-core-ga/" checked>HttpCore<br>
<input type="checkbox" name="server" value="https://gmail.com" checked>Gmail<br> -->
// create a thread for each URI
public void synchronizeUrls(String[] servers) throws InterruptedException
{
GetThread[] threads = new GetThread[servers.length];
for (int i = 0; i < threads.length; i++) {
HttpGet httpget = new HttpGet(servers[i]);
threads[i] = new GetThread(httpClient, httpget);
}
// start the threads
for (int j = 0; j < threads.length; j++) {
threads[j].start();
}
// join the threads
for (int j = 0; j < threads.length; j++) {
threads[j].join();
}
}
static class GetThread extends Thread {
private final CloseableHttpClient httpClient;
private final HttpContext context;
private final HttpGet httpget;
public GetThread(CloseableHttpClient httpClient, HttpGet httpget) {
this.httpClient = httpClient;
this.context = HttpClientContext.create();
this.httpget = httpget;
}
@Override
public void run() {
try {
CloseableHttpResponse response = httpClient.execute(
httpget, context);
final HttpClientConnectionManager connMgr = null;
try {
// HttpEntity entity = response.getEntity();
statusCode = response.getStatusLine().getStatusCode();
System.out.println(statusCode);
}
} // Handle protocol errors
catch (ClientProtocolException ex) {
System.out.println(ex.getMessage());
} catch (IOException ex) {
// Handle I/O errors
System.out.println(ex.getMessage());
}
}`
和 另一件事是,如果我们没有在5分钟的持续时间间隔内得到任何网址的响应,我们就会因为jvm关闭而处理这种情况。 我不确定,如何实施我的第二套要求,如果有人可以帮助我,将会非常有帮助。谢谢!
答案 0 :(得分:0)
有多种方法可以做到这一点。我这样做的方式如下。
您想要做的是:
对于第1步,将statusCode
添加到GetThread
。 [您已在statusCode
设置了run()
。]
private StatusCode statusCode;
public StatusCode getStatusCode() {
return statusCode;
}
对于第2步,在启动线程块之后添加此代码:
new java.util.Timer().schedule(
new java.util.TimerTask() {
@Override
public void run() {
for (int j = 0; j < threads.length; j++) {
if(threads[j].getStatusCode().equals(OK)) {
//threads[j]'s jvm is up
} else {
//threads[j]'s jvm is down
}
}
}
},
5 * 60 * 1000
);