现在版本3.8.4,加入Promise之后,逻辑更加清楚了,因为有点闲,重构下以前写的小小的web。
1.如果是单次访问,那么不需要compose,来做个简单封装。
/**
* 查询数据集
*
* @param ctx
* @param sql
* @param params
*/
public static void rows(RoutingContext ctx, String sql, JsonArray params) {
HttpServerResponse res = ctx.response();
res.putHeader("content-type", "application/json");
SQLClient client = ConfigVerticle.client;
CallResult<List<JsonObject>> result = new CallResult<List<JsonObject>>();
try {
client.getConnection(cr -> {
if (cr.succeeded()) {
SQLConnection connection = cr.result();
if (connection != null) {
connection.queryWithParams(sql, params, qr -> {
if (qr.succeeded()) {
res.end(result.ok(qr.result().getRows()).toString());
} else {
res.end(result.err().toString());
}
connection.close();
});
} else {
res.end(result.err("the DB connect is null.").toString());
}
} else {
res.end(result.err("get DB connect err.").toString());
}
});
} catch (Exception e) {
res.end(result.err(e.getMessage()).toString());
e.printStackTrace();
}
}
调用也是非常方便的,片段:
/**
* 工作联系单时间线查询
*/
public void handleTimeline(RoutingContext ctx) {
JsonObject rp = ctx.getBodyAsJson();
String sql = "select a.*,b.contactType from contact_log a,contact b " + //
" where a.contactId = ? and a.contactId = b.contactId order by a.operationdate";
JsonArray params = new JsonArray().add(rp.getInteger("contactId"));
JdbcHelper.rows(ctx, sql, params);
}
2.如果是多次访问,且要保持队形的,可将访问封装成Future,并以Function<>或Supplier<>接口包裹,如果需要上次访问结果的用Function,如不需要传值,用Supplier即可。片段:
// 1.1 添加-ID
Supplier<Future<Integer>> getIdFuture = () -> {
Future<Integer> f = Future.future(promise -> {
String sql = "select f_getSequ(?) as seq from dual";
JsonArray params = new JsonArray().add("LXDSEQ");
connection.queryWithParams(sql, params, r -> {
if (r.succeeded()) {
promise.complete(r.result().getRows().get(0).getInteger("SEQ"));// newId
} else {
promise.fail("get contact id error.");
}
});
});
return f;
};
// 1.2 添加-保存联系单信息
Function<Integer, Future<Integer>> sf = newId -> {
Future<Integer> f = Future.future(promise -> {
String sql = "insert into contact(contactid,contacttype,department,"//
+ "linkman,phone,linksystem,contactlevel,content,status,checkinuser,"//
+ "checkindate,infin,finlink) values(?,?,?,?,?,?,?,?,?,?,?,?,?)";
JsonArray params = new JsonArray();
params.add(newId);
params.add(rp.getString("contactType"));// contactType
params.add(rp.getString("department"));// department
params.add(rp.getString("linkman"));// linkMan
params.add(rp.getString("phone"));// phone
params.add(rp.getString("linkSystem"));// linkSystem
params.add(rp.getString("contactLevel"));// contactLevel
params.add(rp.getString("content"));// content
params.add("CHECKIN");// status
params.add(rp.getString("checkinUser"));// checkinUser
params.add(rp.getString("checkinDate"));// checkinDate
params.add(rp.getString("inFin"));// inFin
params.add(rp.getString("finLink"));// finLink
connection.updateWithParams(sql, params, r -> {
if (r.succeeded()) {
promise.complete(newId);
} else {
promise.fail("save contact error.");
}
});
});
return f;
};
然后将这些封装的Future组合起来:
getIdFuture.get().compose(r -> {
return sf.apply(r);
}).compose(r -> {
return cf.apply(r);
}).compose(r -> {
return alf.apply(r);
}).setHandler(r -> {
if (r.succeeded()) {
String message = "联系单" + r.result() + "已创建";
notifyHandler.sendMsg(r.result(), message);
uploadHandler.fileTheDocument(rp.getString("checkinUser"), r.result(),
rp.getString("fileList"));
res.end(result.ok().toString());
} else {
res.end(result.err(r.cause().getMessage()).toString());
}
connection.close();
});
看起来队形比较整齐,不象原来那样乱。
网友评论