首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java应用程序在几天后(8-10天)突然停止

Java应用程序在几天后(8-10天)突然停止
EN

Stack Overflow用户
提问于 2018-01-19 07:28:39
回答 1查看 99关注 0票数 0

我创建了一个独立的app.This应用程序,每5秒发送一个移动和事件到一个线程(主)和应用程序停止事件请求启动.或者移动请求开始。在下面的请求method.How行中,我可以修复这个错误。

我不能得到任何信息。

当数据库连接和网络连接断开时,我可以处理这个问题。

代码语言:javascript
复制
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发送请求->

代码语言:javascript
复制
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;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-20 11:40:47

我们期望post在发送请求时工作,然后在responseCode method.Sometimes responseTime中为HTTPUrlConnection method.Sometimes responseTime中的setDoOutput (true)返回响应30秒,因此setReadTimeOut(0)默认url conenction。有时没有响应程序和可疑程序。因此,我们必须添加类似于以下conn.setReadTimeout(40000);的setReadTimeout参数

代码语言:javascript
复制
 conn.getResponseCode();// stay there
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48336066

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档