3

I wrote a java block to execute a task asynchronously by using the "CompletableFuture". It worked well thru all the codes above the "CallableStatement" block, but it didn't execute all the codes below the block.

I tried to return simple response from the CallableStatement block before it gets executed. With that, the rest of the codes were executed. But they were not executed after i returned response after the CallableStatement got executed.

Here is my java block

CompletableFuture.runAsync(() -> {
    System.out.println("Async task started...");
    Map<String,Object> reg = new HashMap<String,Object>();
    reg.put("id", 1993);
    reg.put("name", "Wilson");
    reg.put("sex", "M");
    JSONObject obReg = new Registration().register(reg);

    // the info was successfully registred as i can see in the database, but the below code didn't get executed.

    String respCode = obReg.getString("respCode");
    if (respCode.equals("success")) {
        /*
        i will send notification to user.
        */
    }
    System.out.println("Async task completed...");
});

And here is the Registration class

public class Registration {
    public JSONObject register(Map<String,Object> obj) {
        JSONObject resp = new JSONObject();
        resp.put("respCode", "failed");
        Connection connection = null;
        try {
            connection = getConnection();
            CallableStatement cs = connection.prepareCall("{Call register(?,?,?,?)}");
            cs.setString("id", Integer.parseInt(obj.get("id").toString()));
            cs.setString("namme", obj.get("name").toString());
            cs.setString("sex", obj.get("sex").toString());
            cs.registerOutParameter("respCode", OracleTypes.VARCHAR);

            // if I return response before it gets executed, the CompletaleFuture works as expected.
            /*
                if (true) {
                    resp.put("respCode", "success");
                    return objRes;
                }
            */

            cs.execute();
            String respCode = cs.getString("respCode");
            cs.close();
            connection.close();
            if (respCode.equals("success")){
                resp.put("respCode", "success");
                resp.put("regCode", "123456");
                return resp;
            }
        } 
        return resp;
    }
}

How would I do to get the rest of the codes in the java block get executed after a call to CallableStatement?

Any help is appreciated. Thanks so much.

Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55
Wilson
  • 39
  • 6
  • Everything looks ok, and I suppose ALL the code inside completable future is executed. Most probably `respCode.equals("success")` is never `true`. And I would check the values of `respCode` in `Registration` class (after a connection is closed). – Max Farsikov Sep 08 '19 at 19:44
  • @MaxFarsikov Thanks for your response. Actually, the ```System.out.println("Async task completed...");``` was not printed. The true/false of if condition doesn't affect it. – Wilson Sep 09 '19 at 08:02
  • what is waiting on the result of CompletableFuture.runAsync? is this running in a long lived process, or is the java block a part of a small java app that just does that within main? if nothing is keeping the process alive, then the program may be ending before the statement has finished. if it's a simple main program, try adding a .get() call on the result of runAsync – Kyle Winter Sep 10 '19 at 03:55
  • @KyleWinter Nothing is waiting for the result. I just want to return response to client before the async task completed because i don't need to any result of it. But when it completes, the result that is required will be sent to client separately. However, i found the mistake that came from the CallableStatement itself that blocked the rest of the code in java block from being executed. It didn't even print the stack trace. Anyway, thanks all for your efforts. Pretty much appreciated. :) – Wilson Sep 11 '19 at 06:40

0 Answers0