Remove ReactiveResult.java

This commit is contained in:
lancer.hong
2021-11-07 19:04:17 +08:00
parent 96eecc3899
commit 4c3025e2a8
8 changed files with 70 additions and 137 deletions

View File

@@ -1,94 +0,0 @@
/*
* Copyright (C) 2020 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package we.util;
import java.util.Map;
/**
* @author hongqiaowei
*/
public class ReactiveResult<D> extends Result<D> {
public Map<Object, Object> context;
public ReactiveResult() {
super();
}
public ReactiveResult(int code, String msg, D data, Throwable t, Map<Object, Object> context) {
super(code, msg, data, t);
this.context = context;
}
public static <D> ReactiveResult<D> succ() {
return new ReactiveResult<D>(SUCC, null, null, null, null);
}
public static <D> ReactiveResult<D> succ(D data) {
ReactiveResult<D> r = succ();
r.data = data;
return r;
}
public static <D> ReactiveResult<D> fail() {
return new ReactiveResult<D>(FAIL, null, null, null, null);
}
public static <D> ReactiveResult<D> fail(String msg) {
ReactiveResult<D> r = fail();
r.msg = msg;
return r;
}
public static <D> ReactiveResult<D> fail(Throwable t) {
ReactiveResult<D> r = fail();
r.t = t;
return r;
}
public static <D> ReactiveResult<D> with(int code) {
return new ReactiveResult<D>(code, null, null, null, null);
}
public static <D> ReactiveResult<D> with(int code, String msg) {
ReactiveResult<D> r = with(code);
r.msg = msg;
return r;
}
public static <D> ReactiveResult<D> with(int code, D data) {
ReactiveResult<D> r = with(code);
r.data = data;
return r;
}
public static <D> ReactiveResult<D> with(int code, Throwable t) {
ReactiveResult<D> r = with(code);
r.t = t;
return r;
}
@Override
public void toStringBuilder(StringBuilder b) {
super.toStringBuilder(b);
if (context != null) {
b.append(',').append("ctx=").append(context);
}
}
}

View File

@@ -17,7 +17,8 @@
package we.util;
import org.apache.commons.lang3.StringUtils;
import java.util.Collections;
import java.util.Map;
/**
* @author hongqiaowei
@@ -36,18 +37,21 @@ public class Result<D> {
public Throwable t;
public Map<Object, Object> context = Collections.emptyMap();
public Result() {
}
public Result(int code, String msg, D data, Throwable t) {
public Result(int code, String msg, D data, Throwable t, Map<Object, Object> context) {
this.code = code;
this.msg = msg;
this.data = data;
this.t = t;
this.context = context;
}
public static <D> Result<D> succ() {
return new Result<D>(SUCC, null, null, null);
return new Result<D>(SUCC, null, null, null, null);
}
public static <D> Result<D> succ(D data) {
@@ -56,8 +60,14 @@ public class Result<D> {
return r;
}
public static <D> Result<D> succ(D data, Map<Object, Object> context) {
Result<D> r = succ(data);
r.context = context;
return r;
}
public static <D> Result<D> fail() {
return new Result<D>(FAIL, null, null, null);
return new Result<D>(FAIL, null, null, null, null);
}
public static <D> Result<D> fail(String msg) {
@@ -66,14 +76,32 @@ public class Result<D> {
return r;
}
public static <D> Result<D> fail(String msg, Map<Object, Object> context) {
Result<D> r = fail(msg);
r.context = context;
return r;
}
public static <D> Result<D> fail(Throwable t) {
Result<D> r = fail();
r.t = t;
return r;
}
public static <D> Result<D> fail(Throwable t, Map<Object, Object> context) {
Result<D> r = fail(t);
r.context = context;
return r;
}
public static <D> Result<D> with(int code) {
return new Result<D>(code, null, null, null);
return new Result<D>(code, null, null, null, null);
}
public static <D> Result<D> with(int code, Map<Object, Object> context) {
Result<D> r = with(code);
r.context = context;
return r;
}
public static <D> Result<D> with(int code, String msg) {
@@ -82,35 +110,38 @@ public class Result<D> {
return r;
}
public static <D> Result<D> with(int code, String msg, Map<Object, Object> context) {
Result<D> r = with(code, msg);
r.context = context;
return r;
}
public static <D> Result<D> with(int code, Throwable t) {
Result<D> r = with(code);
r.t = t;
return r;
}
public static <D> Result<D> with(int code, Throwable t, Map<Object, Object> context) {
Result<D> r = with(code, t);
r.context = context;
return r;
}
public static <D> Result<D> with(int code, D data) {
Result<D> r = with(code);
r.data = data;
return r;
}
@Override
public String toString() {
StringBuilder b = new StringBuilder();
toStringBuilder(b);
return b.toString();
public static <D> Result<D> with(int code, D data, Map<Object, Object> context) {
Result<D> r = with(code, data);
r.context = context;
return r;
}
public void toStringBuilder(StringBuilder b) {
b.append("code=").append(code);
if (StringUtils.isNotBlank(msg)) {
b.append(',').append("msg=") .append(msg);
}
if (data != null) {
b.append(',').append("data=").append(data);
}
if (t != null) {
b.append(',').append("err=") .append(t.getMessage());
}
@Override
public String toString() {
return JacksonUtils.writeValueAsString(this);
}
}

View File

@@ -29,7 +29,6 @@ import we.config.AggregateRedisConfig;
import we.config.SystemConfig;
import we.plugin.auth.ApiConfig;
import we.util.JacksonUtils;
import we.util.ReactiveResult;
import we.util.Result;
import we.util.UrlTransformUtils;
@@ -115,7 +114,7 @@ public class ApiPairingDocSetService {
rt.listenToChannel(channel)
.doOnError(
t -> {
result.code = ReactiveResult.FAIL;
result.code = Result.FAIL;
result.msg = "lsn error, channel: " + channel;
result.t = t;
log.error("lsn channel {} error", channel, t);

View File

@@ -27,7 +27,6 @@ import reactor.core.publisher.Mono;
import we.config.AggregateRedisConfig;
import we.config.SystemConfig;
import we.util.JacksonUtils;
import we.util.ReactiveResult;
import we.util.Result;
import javax.annotation.PostConstruct;
@@ -111,7 +110,7 @@ public class ApiPairingInfoService {
rt.listenToChannel(channel)
.doOnError(
t -> {
result.code = ReactiveResult.FAIL;
result.code = Result.FAIL;
result.msg = "lsn error, channel: " + channel;
result.t = t;
log.error("lsn channel {} error", channel, t);

View File

@@ -32,7 +32,7 @@ import we.proxy.CallbackReplayReq;
import we.proxy.CallbackService;
import we.util.Consts;
import we.util.JacksonUtils;
import we.util.ReactiveResult;
import we.util.Result;
import we.util.ThreadContext;
import javax.annotation.Resource;
@@ -61,7 +61,7 @@ public class CallbackController {
callbackService.replay(req)
.onErrorResume(
t -> {
return Mono.just(ReactiveResult.fail(t));
return Mono.just(Result.fail(t));
}
)
.map(
@@ -69,13 +69,12 @@ public class CallbackController {
StringBuilder b = ThreadContext.getStringBuilder();
b.append(req.id).append(' ').append(req.service).append(' ').append(req.path).append(' ');
ServerHttpResponse resp = exchange.getResponse();
if (r.code == ReactiveResult.SUCC) {
if (r.code == Result.SUCC) {
log.info(b.append("replay success").toString(), LogService.BIZ_ID, req.id);
resp.setStatusCode(HttpStatus.OK);
return Consts.S.EMPTY;
} else {
b.append("replay error:\n");
r.toStringBuilder(b);
b.append("replay error:\n").append(r);
log.error(b.toString(), LogService.BIZ_ID, req.id);
resp.setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
if (r.msg != null) {

View File

@@ -27,7 +27,6 @@ import reactor.core.publisher.Mono;
import we.config.AggregateRedisConfig;
import we.fizz.input.PathMapping;
import we.util.JacksonUtils;
import we.util.ReactiveResult;
import we.util.Result;
import javax.annotation.PostConstruct;
@@ -119,7 +118,7 @@ public class GlobalResourceService {
rt.listenToChannel(channel)
.doOnError(
t -> {
result.code = ReactiveResult.FAIL;
result.code = Result.FAIL;
result.msg = "lsn error, channel: " + channel;
result.t = t;
log.error("lsn channel {} error", channel, t);

View File

@@ -249,7 +249,7 @@ public class ApiConfigService implements ApplicationListener<ContextRefreshedEve
rt.listenToChannel(channel)
.doOnError(
t -> {
result.code = ReactiveResult.FAIL;
result.code = Result.FAIL;
result.msg = "lsn error, channel: " + channel;
result.t = t;
log.error("lsn channel {} error", channel, t);

View File

@@ -204,14 +204,14 @@ public class CallbackService {
.doOnError(throwable -> clean(remoteResp)).doOnCancel(() -> clean(remoteResp));
}
public Mono<ReactiveResult> replay(CallbackReplayReq req) {
public Mono<Result> replay(CallbackReplayReq req) {
HashSet<String> gatewayGroups = new HashSet<>();
gatewayGroups.add(req.gatewayGroup);
Result<ApiConfig> result = apiConfigService.get(gatewayGroups, req.app, req.service, req.method, req.path);
ApiConfig ac = result.data;
if (ac == null) {
return Mono.just(ReactiveResult.fail("no api config for " + req.path));
return Mono.just(Result.fail("no api config for " + req.path));
}
CallbackConfig cc = ac.callbackConfig;
@@ -259,21 +259,21 @@ public class CallbackService {
.collectList()
.map(
sendResults -> {
int c = ReactiveResult.SUCC;
int c = Result.SUCC;
Throwable t = null;
for (int i = 0; i < sendResults.size(); i++) {
Object r = sendResults.get(i);
if (r instanceof FizzFailClientResponse) {
c = ReactiveResult.FAIL;
c = Result.FAIL;
t = ((FizzFailClientResponse) r).throwable;
} else if (r instanceof FailAggregateResult) {
c = ReactiveResult.FAIL;
c = Result.FAIL;
t = ((FailAggregateResult) r).throwable;
} else if (r instanceof ClientResponse) {
clean((ClientResponse) r);
}
}
return ReactiveResult.with(c, t);
return Result.with(c, t);
}
)
;