Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中注册和接收来自多个DBMS_警报的结果_Java_Servlets_Asynchronous_Plsql_Jboss - Fatal编程技术网

在Java中注册和接收来自多个DBMS_警报的结果

在Java中注册和接收来自多个DBMS_警报的结果,java,servlets,asynchronous,plsql,jboss,Java,Servlets,Asynchronous,Plsql,Jboss,我有一小段代码,用于注册多个DBMS_警报,并在返回JSONArray响应的servlet中异步运行问题是它只注册并适用于第一个警报,而不适用于其他警报? 当把它作为SQL脚本运行时,一切都很好 我使用的SQL脚本: DECLARE alt varchar(100); msg varchar(100); sts integer; BEGIN dbms_alert.register('appdelete'); dbms_alert.register('devicelock'); dbms_al

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

当把它作为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_警报的代码:

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;
}
2.doPost()部分:

受保护的void doPost(HttpServletRequest请求,
HttpServletResponse响应)引发ServletException,IOException{
doGet(请求、响应);
List asyncContexts=new ArrayList(this.contexts);
this.contexts.clear();
System.out.println(“|处理响应|”);
System.out.println(“| doPost |表示到达!”);
用于(异步上下文异步上下文:异步上下文){
int i=1;
System.out.println(“| doPost | TRY reach!”);
试一试{
asyncContext.getResponse();
System.out.println(“| dbmsAlert | reach!”+i++);
dbmsAlert();
System.out.println(“|dbmsAlert|PASSED!”+i++);
asyncContext.complete();
System.out.println(“|处理响应结束|”);
}捕获(例外e){
e、 printStackTrace();
}
}
}


谢谢。

找到了问题

这段代码(现在我看着它,眼睛都疼了):

for(int i=1;i<4;i++){
System.out.println(“为循环输入!”);
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();
}
String[]sqls=newstring[]{sql1,sql2,sql3,sql4};
对于(int i=0;i<4;i++){
添加(i,sqls[i]);
System.out.println(“警报:“+ALERTS.get(i));
csA=conn.prepareCall(ALERTS.get(i));
csA.execute();
}


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

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();
        }
    }
}
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();
}