響應式編程(reactive programming)是一種基於數據流(data stream)和變化傳遞(propagation of change)的聲明式(declarative)的編程范式
在命令式編程(我們的日常編程模式)下,式子a=b+c,這就意味着a的值是由b和c計算出來的。如果b或者c后續有變化,不會影響到a的值
在響應式編程下,式子a:=b+c,這就意味着a的值是由b和c計算出來的。但如果b或者c的值后續有變化,會影響到a的值
我認為上面的例子已經可以幫助我們理解變化傳遞(propagation of change)
lambda:
函數式編程接口:
@FunctionalInterface
其中在函數式接口中有一些只適合在函數式中使用的官方封裝好的接口方法
如BiFunction,Tuple
BiFunction適用於兩個參數,而Function適用於一個參數
Function的使用:
/**
* 一個參數的調用返回
*
* @param option
* @param funOptionService
* @return
*/
public static Mono<ServerResponse> oneOptionalParams(Optional<String> option,
Function<Optional<String>, Mono<JSONObject>> funOptionService,
String judgeParam) {
//option is not Present and the judgeParams not null then badRequest
if (!option.isPresent() && (null != judgeParam) && judgeParam != "") {
return (Mono<ServerResponse>) ServerResponseBuild.badRequestMsg("參數" + judgeParam + "不能為空");
}
Mono<JSONObject> jsonObjectMono = funOptionService.apply(option);
return jsonObjectMono.flatMap(
resultJson -> {
if (resultJson.getInteger("errcode") != 0) {
return ServerResponseBuild.badRequestMsg(resultJson.getString("errmsg"));
}
log.info("\n返回數據:{}", resultJson);
return ServerResponseBuild.okRequestObj(resultJson, JSONObject.class);
}
).switchIfEmpty(ServerResponseBuild.badRequestMsg("請求錯誤")
).onErrorResume(error -> ServerResponseBuild.badRequestMsg(error.getMessage()));
}
BiFunction兩個參數的
/**
* 兩個參數的調用返回
* @param option1
* @param option2
* @param funBiOptionService
* @param judgeParams
* @return
*/
public static Mono<ServerResponse> twoOptionalParams(Optional<String> option1, Optional<String> option2,
BiFunction<Optional<String>, Optional<String>, Mono<JSONObject>> funBiOptionService,
String... judgeParams) {
//省略judgeParams...
Mono<JSONObject> jsonObjectMono = null;
try {
jsonObjectMono = funBiOptionService.apply(option1, option2);
} catch (Exception e) {
return Mono.error(e);
}
return jsonObjectMono.flatMap(
resultJson -> {
if (resultJson.getInteger("errcode") != 0) {
return ServerResponseBuild.badRequestMsg(resultJson.getString("errmsg"));
}
log.info("\n返回數據:{}", resultJson);
return ServerResponseBuild.okRequestObj(resultJson, JSONObject.class);
}
).switchIfEmpty(ServerResponseBuild.badRequestMsg("請求錯誤")
).onErrorResume(error -> ServerResponseBuild.badRequestMsg(error.getMessage()));
}
函數式調用
這樣寫起來確實看上去簡潔了許多
且這樣調用可以調用接收多個參數
一個參數:
/**
* 獲取訪問用戶身份
*
* @param request
* @return
*/
public Mono<ServerResponse> getuserInfo(ServerRequest request) {
return ServerResponseBuild.oneOptionalParams(request.queryParam("code"),
option -> manIdentityVerifyService.getUserInfo(option),
"code");
}
上面的寫法可以改為:
/**
* 獲取訪問用戶身份
*
* @param request
* @return
*/
public Mono<ServerResponse> getuserInfo(ServerRequest request) {
return ServerResponseBuild.oneOptionalParams(request.queryParam("code"),
manIdentityVerifyService::getUserInfo,
"code");
}
兩個參數的同樣可以使用::方式來調用方法
/**
* ~~~~不給你看注釋
* @return
*/
public Mono<ServerResponse> getConversations(ServerRequest request) {
return ServerResponseBuild.twoOptionalParams(
request.queryParam("seq"),
request.queryParam("limit"),
chatMsgSaveService::monoGetChatContent,
"seq", "limit");
}