0
我有一個Java應用程序,將消息推送到一堆網址。這些ulrs是不同的服務器類型,如apache,IIS,...。不能關閉我的http連接從java到ASP.Net服務器
現在我需要發送此消息到每個網址。如果Url向我回應一切正常,如果他們沒有(直到特定超時),我想要關閉連接。所以我爲此使用了CloseableHttpClient。
我的代碼如下:
public void pushToURL(Message message, String url, int notifierId) throws NotificationException, IOException {
long responseTime;
long startTime = System.currentTimeMillis();
RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(this.socketTimeOut)
.setSocketTimeout(this.socketTimeOut)
.setConnectTimeout(this.socketTimeOut)
.build();
CloseableHttpClient httpClient = HttpClientBuilder.create()
.setDefaultRequestConfig(requestConfig)
.setConnectionReuseStrategy(NoConnectionReuseStrategy.INSTANCE)
.build();
URIBuilder builder = null;
try {
builder = new URIBuilder(url);
builder = builder.addParameter("msgBody", receivedMessage.getMsgBody());
builder = builder.addParameter("receivedTime", String.valueOf(receivedMessage.getReceivedTime()));
URI uri = builder.build();
HttpGet httpGet = new HttpGet(uri);
logMaster.debug(className, "notifier #" + notifierId + ": Try Sending notification to httpserver: " + url + " receivedTime:" + message.getReceivedTime());
CloseableHttpResponse response = httpClient.execute(httpGet);
responseTime = System.currentTimeMillis() - startTime;
int code = response.getStatusLine().getStatusCode();
if (code < 300) {
logMaster.debug(className, "notifier #" + notifierId + ": Notification to httpserver sent: code:" + code + " url:" + url + " receivedTime:" + message.getReceivedTime() + ", Executed in " + responseTime + " milliSec");
if (response != null)
httpGet.abort();
return;
} else {
if (response != null)
httpGet.abort();
logMaster.info(className, "notifier #" + notifierId + ": Sending notification failed. Http response error");
throw new NotificationException("Sending notification failed. Http response error");
}
} catch (URISyntaxException e) {
logMaster.warn(className, "Notification problem, URISyntaxException, " + e.getMessage() + ", to:" + url + ", in:" + (System.currentTimeMillis() - startTime) + " milliseconds. notifierId: " + notifierId);
throw new NotificationException(e.getMessage());
} catch (ClientProtocolException e) {
logMaster.warn(className, "Notification problem, ClientProtocolException, " + e.getMessage() + ", to:" + url + ", in:" + (System.currentTimeMillis() - startTime) + " milliseconds. notifierId: " + notifierId);
throw new NotificationException(e.getMessage());
} catch (ConnectTimeoutException e) {
logMaster.warn(className, "Notification problem, ConnectTimeoutException, " + e.getMessage() + ", to:" + url + ", in:" + (System.currentTimeMillis() - startTime) + " milliseconds. notifierId: " + notifierId);
throw new NotificationException(e.getMessage());
} catch (InterruptedIOException e) {
logMaster.warn(className, "Notification problem, InterruptedIOException, " + e.getMessage() + ", to:" + url + ", in:" + (System.currentTimeMillis() - startTime) + " milliseconds. notifierId: " + notifierId);
throw new NotificationException(e.getMessage());
} catch (UnknownHostException e) {
logMaster.warn(className, "Notification problem, UnknownHostException, " + e.getMessage() + ", to:" + url + ", in:" + (System.currentTimeMillis() - startTime) + " milliseconds. notifierId: " + notifierId);
throw new NotificationException(e.getMessage());
} catch (IOException e) {
logMaster.warn(className, "Notification problem, IOException, " + e.getMessage() + ", to:" + url + ", in:" + (System.currentTimeMillis() - startTime) + " milliseconds. notifierId: " + notifierId);
throw new NotificationException(e.getMessage());
} catch (Exception e) {
logMaster.warn(className, "Notification problem, " + e.getMessage() + ", to:" + url + ", in:" + (System.currentTimeMillis() - startTime) + " milliseconds. notifierId: " + notifierId);
throw new NotificationException(e.getMessage());
} finally {
// Release the connection.
httpClient.close();
logMaster.debug(className, "notifier " + notifierId + " closed the httpClient. " +
"url: " + url + " receivedTime:" + message.getReceivedTime());
}
}
有跡象表明,使用此功能將郵件發送到URL 10個線程。
在我不知道的某些情況下,有些網址無法強制關閉連接(實際上是在超時之後)。我所知道的一切都是這些URL是ASP.net的,有時只是這樣做的。這就像我與他們建立了連接,甚至通過武力,我無法關閉連接!
這太奇怪了,我的工作超過一個星期。我搜索了很多解決方案,但沒有人幫助。有沒有人可以幫助解決這個問題?
在此先感謝。