我正在进行一次单击就可以自动访问多个服务器urls (现在这些urls是手动的),作为响应,我将为每个响应获得一个登录页面,这意味着jvm已经启动并运行。
问题是jvm可能需要最多5分钟的响应时间,并且有多达10个urls,因此检查响应代码的总时间最多需要50分钟(最糟糕的情况是,所有jvm都关闭)。
所以,我必须做两件事--我们将要点击的服务器urls,在一次点击中应该同步发生(这将使整个响应时间从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分钟的时间间隔内从任何url获得响应,我们就会把这种情况当作jvm关闭。我不知道如何执行我的第二套要求,如果有人能帮助我,那将是很有帮助的。谢谢!
发布于 2016-09-17 19:36:43
做这件事有多种方法。我这样做的方式如下。
你想做的是:
对于步骤1,将statusCode添加到GetThread。您已经在run()中设置了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
);https://stackoverflow.com/questions/39549656
复制相似问题