Skip to content

Commit 393dd9e

Browse files
author
曾林辉
committed
周六第3题:改成泛型和反射,增加全局异常类,处理异常
1 parent 56cca62 commit 393dd9e

5 files changed

Lines changed: 45 additions & 44 deletions

File tree

07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/client/Rpcfx.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@
1919
public final class Rpcfx {
2020

2121
static {
22-
ParserConfig config = ParserConfig.getGlobalInstance();
23-
config.addAccept("io.kimmking");
24-
config.setAutoTypeSupport(true);
25-
26-
//ParserConfig.getGlobalInstance().addAccept("io.kimmking");
22+
ParserConfig.getGlobalInstance().addAccept("io.kimmking");
2723
}
2824

2925
public static <T> T create(final Class<T> serviceClass, final String url) {
@@ -39,6 +35,7 @@ public static class RpcfxInvocationHandler implements InvocationHandler {
3935

4036
private final Class<?> serviceClass;
4137
private final String url;
38+
4239
public <T> RpcfxInvocationHandler(Class<T> serviceClass, String url) {
4340
this.serviceClass = serviceClass;
4441
this.url = url;
@@ -56,19 +53,17 @@ public Object invoke(Object proxy, Method method, Object[] params) throws Throwa
5653
request.setParams(params);
5754

5855
RpcfxResponse response = post(request, url);
59-
if (response.isStatus()) {
60-
return response.getResult();
61-
} else {
62-
RpcfxException e = (RpcfxException) response.getException();
63-
return null;
64-
}
6556
// 这里判断response.status,处理异常
6657
// 考虑封装一个全局的RpcfxException
58+
if (!response.isStatus()) {
59+
throw new RpcfxException("invoke error", response.getException());
60+
}
61+
return JSON.parse(response.getResult().toString());
6762
}
6863

6964
private RpcfxResponse post(RpcfxRequest req, String url) throws IOException {
7065
String reqJson = JSON.toJSONString(req);
71-
System.out.println("req json: "+reqJson);
66+
System.out.println("req json: " + reqJson);
7267

7368
// 1.可以复用client
7469
// 2.尝试使用httpclient或者netty client
@@ -78,7 +73,7 @@ private RpcfxResponse post(RpcfxRequest req, String url) throws IOException {
7873
.post(RequestBody.create(JSONTYPE, reqJson))
7974
.build();
8075
String respJson = client.newCall(request).execute().body().string();
81-
System.out.println("resp json: "+respJson);
76+
System.out.println("resp json: " + respJson);
8277
return JSON.parseObject(respJson, RpcfxResponse.class);
8378
}
8479
}

07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/exception/RpcfxException.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ public RpcfxException(Throwable throwable) {
1010
super(throwable);
1111
}
1212

13-
public RpcfxException(String exception) {
14-
super(exception);
13+
public RpcfxException(String message) {
14+
super(message);
15+
}
16+
17+
public RpcfxException(String message, Throwable throwable) {
18+
super(message, throwable);
1519
}
1620

1721
}

07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/server/RpcfxInvoker.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,24 @@
1414

1515
public class RpcfxInvoker {
1616

17-
private RpcfxResolver resolver;
17+
// private RpcfxResolver resolver;
18+
//
19+
// public RpcfxInvoker(RpcfxResolver resolver){
20+
// this.resolver = resolver;
21+
// }
22+
private RpcfxReflectionResolver reflectionResolver;
1823

19-
public RpcfxInvoker(RpcfxResolver resolver){
20-
this.resolver = resolver;
24+
public RpcfxInvoker(RpcfxReflectionResolver reflectionResolver) {
25+
this.reflectionResolver = reflectionResolver;
2126
}
22-
//private RpcfxReflectionResolver reflectionResolver;
23-
24-
// public RpcfxInvoker(RpcfxReflectionResolver reflectionResolver) {
25-
// this.reflectionResolver = reflectionResolver;
26-
// }
2727

2828
public RpcfxResponse invoke(RpcfxRequest request) {
2929
RpcfxResponse response = new RpcfxResponse();
3030
String serviceClass = request.getServiceClass();
3131

3232
// 作业1:改成泛型和反射
33-
Object service = resolver.resolve(serviceClass);//this.applicationContext.getBean(serviceClass);
34-
//Object service = reflectionResolver.resolve(serviceClass);
33+
//Object service = resolver.resolve(serviceClass);//this.applicationContext.getBean(serviceClass);
34+
Object service = reflectionResolver.resolve(serviceClass);
3535

3636
try {
3737
Method method = resolveMethodFromClass(service.getClass(), request.getMethod());
Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
package io.kimmking.rpcfx.demo.provider;
22

33
import io.kimmking.rpcfx.api.RpcfxReflectionResolver;
4+
import org.springframework.beans.BeansException;
5+
import org.springframework.context.ApplicationContext;
6+
import org.springframework.context.ApplicationContextAware;
47

58
import java.lang.reflect.InvocationTargetException;
69

7-
public class ReflectionResolver<T> implements RpcfxReflectionResolver<T> {
10+
public class ReflectionResolver<T> implements RpcfxReflectionResolver<T>, ApplicationContextAware {
11+
12+
private ApplicationContext applicationContext;
13+
814
@Override
915
public T resolve(String serviceClass) {
1016
T t = null;
1117
try {
1218
Class<?> klass = Class.forName(serviceClass);
13-
t = (T) klass.getConstructor().newInstance();
19+
t = (T) applicationContext.getBean(klass);
1420
} catch (ClassNotFoundException e) {
1521
e.printStackTrace();
16-
} catch (IllegalAccessException e) {
17-
e.printStackTrace();
18-
} catch (InstantiationException e) {
19-
e.printStackTrace();
20-
} catch (NoSuchMethodException e) {
21-
e.printStackTrace();
22-
} catch (InvocationTargetException e) {
23-
e.printStackTrace();
2422
}
25-
2623
return t;
2724
}
25+
26+
@Override
27+
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
28+
this.applicationContext = applicationContext;
29+
}
2830
}

07rpc/rpc01/rpcfx-demo-provider/src/main/java/io/kimmking/rpcfx/demo/provider/RpcfxServerApplication.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,20 @@ public RpcfxResponse invoke(@RequestBody RpcfxRequest request) {
3232
}
3333

3434
@Bean
35-
public RpcfxInvoker createInvoker(@Autowired RpcfxResolver resolver){
35+
public RpcfxInvoker createInvoker(@Autowired RpcfxReflectionResolver resolver){
3636
return new RpcfxInvoker(resolver);
3737
}
3838

39-
// @Bean
40-
// public RpcfxReflectionResolver createReflectionResolver() {
41-
// return new ReflectionResolver();
42-
// }
43-
4439
@Bean
45-
public RpcfxResolver createResolver(){
46-
return new DemoResolver();
40+
public RpcfxReflectionResolver createReflectionResolver() {
41+
return new ReflectionResolver();
4742
}
4843

44+
// @Bean
45+
// public RpcfxResolver createResolver(){
46+
// return new DemoResolver();
47+
// }
48+
4949
// 能否去掉name
5050
//
5151
@Bean(name = "io.kimmking.rpcfx.demo.api.UserService")

0 commit comments

Comments
 (0)