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.