内存管理Java应用程序突然停止几天后(810)
我创建了java独立应用程序。此应用程序每5秒发送一个手机和事件,其中一个线程(主)和应用程序停止事件请求启动。。。或移动请求启动。。。在下面的请求方法中输入行。如何修复此错误
我不能得到不扔的信息
我可以在数据库连接和网络连接正常时处理这个问题 断开连接
public void run() {
while (true) {
long startTime = System.currentTimeMillis();
Connection connection = openConnection();
try {
mobileEventList = null;
if (connection != null) {
mobileEventList = Dao.getMobileEvents(connection);
}
if (failEventFlag) {
if (failMobileEventList != null && failMobileEventList.size() > 0) {
mobileEventList.addAll(failMobileEventList);
LOGGER.info("Fail Event , failMobileEventList Size->" + mobileEventList.size());
}
}
mobileList = null;
if (connection != null) {
mobileList = Dao.getMobiles(username, connection);
}
int responseEvent = setLogEvents(endPointEvent);
int responseMobile = setLogMobile(endPointMobile);
if (mobileEventList != null && mobileList != null && responseEvent != 0 && responseMobile != 0) {
if (connection != null) {
Dao.updateHeartBeat(appName, connection);
LOGGER.info("HeartBeat updated.");
}
}
closeConnection(connection);
} catch (Exception e) {
LOGGER.error(e.getMessage());
}
long endTime = System.currentTimeMillis();
LOGGER.info("Gecen milisaniye->" + ((endTime - startTime)));
if (((endTime - startTime)) < this.repeatTime) {
try {
Thread.sleep(this.repeatTime);
} catch (InterruptedException e) {
LOGGER.error(e.getMessage());
}
}
}
}
每个getEvents和getMobiles发送请求->
private static int request(String params, String endPoint, boolean isEvent) {
if (isEvent) {
LOGGER.info(" Event Request Starting...");
} else {
LOGGER.info(" Mobile Request Starting...");
}
DataOutputStream wr = null;
HttpURLConnection conn = null;
URL url = null;
int responseCode = 0;
try {
url = new URL(endPoint);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json;charset=" + StandardCharsets.UTF_8);
conn.setUseCaches(false);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setConnectTimeout(5000);
conn.connect();
wr = new DataOutputStream(conn.getOutputStream());
wr.writeBytes(params);
wr.flush();
wr.close();
responseCode = conn.getResponseCode();
isFailEvent(responseCode, isEvent);
} catch (UnsupportedEncodingException e) {
LOGGER.error(e.getMessage());
} catch (IOException e) {
isFailEvent(responseCode, isEvent);
LOGGER.error(e.getMessage());
} catch (Throwable e) {
LOGGER.error(e.getMessage());
} finally {
try {
if (wr != null) {
wr.close();
}
if (conn != null) {
conn.disconnect();
}
} catch (Exception e) {
LOGGER.error(e.getMessage());
}
params = null;
}
if (isEvent) {
LOGGER.info(" Event Request End Http Code : " + responseCode + " End Point: " + endPoint);
} else {
LOGGER.info(" Mobile Request End Http Code : " + responseCode + " End Point: " + endPoint);
}
return responseCode;
}
# 1 楼答案
当我们发送请求,然后在HTTPUrlConnection方法中用setDoOutput(true)返回对responseCode的响应时,我们希望post能够工作。有时响应时间会持续30秒,因此setReadTimeOut(0)是默认的url连接。有时得不到响应代码和可疑程序。因此,我们必须添加setReadTimeout参数,比如conn.setReadTimeout(40000)