我创建了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都会发送请求 - &gt;
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;
}
答案 0 :(得分:0)
我们希望帖子在我们发送请求时工作,然后在HTTPUrlConnection方法中使用setDoOutput(true)向responseCode返回响应。有时responseTime持续30秒,因此setReadTimeOut(0)默认url conenction。有时候没有得到响应代码和怀疑程序。所以我们必须像这样添加setReadTimeout参数 conn.setReadTimeout(40000);
conn.getResponseCode();// stay there