0

我有一小段代码可以注册多个 DBMS_ALERT,并在 servlet 中异步运行,返回 JSONArray 中的响应。问题是它注册并仅适用于 FIRST 警报而不适用于其他警报?

当将其作为 SQL 脚本运行时,一切都很好。

我使用的 SQL 脚本:

DECLARE
alt varchar(100);
msg varchar(100);
sts integer;

BEGIN

dbms_alert.register('appdelete');
dbms_alert.register('devicelock');
dbms_alert.register('userlostapp');
dbms_alert.register('userlostdevice');

dbms_alert.waitany(alt,msg,sts,60);
dbms_output.put_line('alt= '||alt||' msg= '||msg||' sts= '||sts||chr(10));

END;

此外,它永远不会在我的方法中运行 System.out 部分。这是为什么?我是否在使用 AsynkTask 时犯了一些愚蠢的错误(我第一次使用它,所以请温柔一点)?

我在这里想念什么?


注册 DBMS_ALERT 的代码:

protected JSONArray dbmsAlert() {

    JSONArray DBMSResponse = new JSONArray();
    JSONObject responseRow = new JSONObject();

    CallableStatement cs1 = null;
    CallableStatement cs2 = null;

    String sql, sql1, sql2, sql3, sql4 = null;

    try {
        System.out.println("Trying to establish connection...");
        conn = ds1.getConnection();

        sql1 = "{call dbms_alert.register('appdelete')}";
        sql2 = "{call dbms_alert.register('devicelock')}";
        sql3 = "{call dbms_alert.register('userlostapp')}";
        sql4 = "{call dbms_alert.register('userlostdevice')}";

        System.out.println("Connection established!");

        for (int i = 1; i < 4; i++) {
            System.out.println("Entered FOR LOOP!");
            cs1 = conn.prepareCall("sql" + i);
            System.out.println("sql" + i);
            cs1.execute();
        }

        sql = "{call dbms_alert.waitone(?, ?, ?, 86400)}";

        System.out.println("Preparring Call for cs2...");
        cs2 = conn.prepareCall(sql);

        System.out.println("Registering Parametrs for cs2...");
        cs2.registerOutParameter(1, Types.VARCHAR);
        cs2.registerOutParameter(2, Types.VARCHAR);
        cs2.registerOutParameter(3, Types.INTEGER);

        int x = 0;
        int i = 0;

        System.out.println("DONE Preparring Call and Registering Parametrs!");
        while(x == 0) {
            System.out.println("Entered WHILE LOOP!");
            i++;
            cs2.execute();
            Integer result = cs2.getInt(3);

            if (result == 1) {
                System.out.println("dbmsAlert |DBMSResponse| for 1 : " + result);
            } else {
                String Result2 = cs2.getString(2);
                responseRow.put("DBMS", Result2);
                DBMSResponse.put(i, responseRow);

            }
        }
    } catch (Exception e) {
         //TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        try {
            cs2.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            cs1.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    System.out.println("dbmsAlert |DBMSResponse| for 0 : " + DBMSResponse);
    return DBMSResponse;
}



dbmsAlert()异步运行的代码:

1. doGet() 部分:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    final AsyncContext asyncContext = request.startAsync(request, response);
    asyncContext.setTimeout(0);
    contexts.add(asyncContext);

    System.out.println("| contexts | contains : " + contexts.size() + " asyncContext elements");

    try {
        System.out.println("| HANDLING REQUEST |");
        handleRequests(request, response);
        System.out.println("| HANDLING REQUEST end |");
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

2. doPost() 部分:

protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {

    doGet(request, response);

    List<AsyncContext> asyncContexts = new ArrayList<>(this.contexts);
    this.contexts.clear();

    System.out.println("| HANDLING RESPONSE |");

    System.out.println("| doPost | FOR REACHED!");
    for (AsyncContext asyncContext : asyncContexts) {

        int i = 1;

        System.out.println("| doPost | TRY REACHED!");
        try {
            asyncContext.getResponse();
            System.out.println("| dbmsAlert | REACHED! " + i++);
            dbmsAlert();
            System.out.println("| dbmsAlert | PASSED! " + i++);
            asyncContext.complete();
            System.out.println("| HANDLING RESPONSE end |");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}



谢谢。

4

1 回答 1

0

发现问题了!

这段代码(现在我看着它伤害了我的眼睛):

for (int i = 1; i < 4; i++) {
    System.out.println("Entered FOR LOOP!");
    cs1 = conn.prepareCall("sql" + i);
    System.out.println("sql" + i);
    cs1.execute();
}


应该是这样的:

String[] sqls = new String[] {sql1, sql2, sql3, sql4};

for (int i = 0; i < 4; i++) {
    ALERTS.add(i, sqls[i]);
    System.out.println("ALERTS : " + ALERTS.get(i));

    csA = conn.prepareCall(ALERTS.get(i));
    csA.execute();
}



真的真的真的……愚蠢的错误。

于 2013-09-16T14:50:14.213 回答