Skip to content

Commit dfa61e3

Browse files
committed
Merge branch 'main' of https://github.com/JavaCourse00/JavaCourseCodes into main
2 parents ed46ea5 + d360a51 commit dfa61e3

17 files changed

Lines changed: 517 additions & 0 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package java0.conc0302.threadpool;
2+
3+
import java.util.concurrent.ExecutorService;
4+
import java.util.concurrent.Executors;
5+
import java.util.concurrent.Future;
6+
7+
public class ExceptionDemo {
8+
9+
public static void main(String[] args) {
10+
ExecutorService executorService = Executors.newFixedThreadPool(1);
11+
12+
try {
13+
Future<Double> future = executorService.submit(() -> {
14+
int a = 1;
15+
return 10.0/(a-1);
16+
});
17+
18+
double b = future.get();
19+
System.out.println(b);
20+
21+
} catch (Exception ex) {
22+
System.out.println("catch execute");
23+
ex.printStackTrace();
24+
}
25+
26+
try {
27+
executorService.execute(() -> {
28+
int a = 1;
29+
float b = 10/(a-1);
30+
});
31+
} catch (Exception ex) {
32+
System.out.println("catch execute");
33+
ex.printStackTrace();
34+
}
35+
36+
executorService.shutdown();
37+
System.out.println("Main Thread End!");
38+
}
39+
40+
}

07rpc/rpc01/.DS_Store

6 KB
Binary file not shown.

07rpc/rpc01/pom.xml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.0.9.RELEASE</version>
9+
<!-- <relativePath/> &lt;!&ndash; lookup parent from repository &ndash;&gt;-->
10+
</parent>
11+
<groupId>io.kimmking</groupId>
12+
<artifactId>rpcfx</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>rpcfx</name>
15+
<packaging>pom</packaging>
16+
<description>RPC demo project for Spring Boot</description>
17+
18+
<modules>
19+
<module>rpcfx-api</module>
20+
<module>rpcfx-server</module>
21+
<module>rpcfx-client</module>
22+
</modules>
23+
24+
<properties>
25+
<java.version>1.8</java.version>
26+
</properties>
27+
28+
<dependencies>
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter</artifactId>
32+
</dependency>
33+
34+
<dependency>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-starter-web</artifactId>
37+
</dependency>
38+
39+
<dependency>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-starter-test</artifactId>
42+
<scope>test</scope>
43+
<exclusions>
44+
<exclusion>
45+
<groupId>org.junit.vintage</groupId>
46+
<artifactId>junit-vintage-engine</artifactId>
47+
</exclusion>
48+
</exclusions>
49+
</dependency>
50+
</dependencies>
51+
52+
</project>

07rpc/rpc01/rpcfx-api/pom.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>io.kimmking</groupId>
7+
<artifactId>rpcfx</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<!-- <relativePath/> &lt;!&ndash; lookup parent from repository &ndash;&gt;-->
10+
</parent>
11+
<groupId>io.kimmking</groupId>
12+
<artifactId>rpcfx-api</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>rpcfx-api</name>
15+
16+
<properties>
17+
<java.version>1.8</java.version>
18+
</properties>
19+
20+
</project>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package io.kimmking.rpcfx.api;
2+
3+
public class RpcfxRequest {
4+
5+
private String serviceClass;
6+
7+
private String method;
8+
9+
private Object[] params;
10+
11+
public String getServiceClass() {
12+
return serviceClass;
13+
}
14+
15+
public void setServiceClass(String serviceClass) {
16+
this.serviceClass = serviceClass;
17+
}
18+
19+
public String getMethod() {
20+
return method;
21+
}
22+
23+
public void setMethod(String method) {
24+
this.method = method;
25+
}
26+
27+
public Object[] getParams() {
28+
return params;
29+
}
30+
31+
public void setParams(Object[] params) {
32+
this.params = params;
33+
}
34+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package io.kimmking.rpcfx.api;
2+
3+
public class RpcfxResponse {
4+
5+
private Object result;
6+
private boolean status;
7+
private Exception exception;
8+
9+
public Object getResult() {
10+
return result;
11+
}
12+
13+
public void setResult(Object result) {
14+
this.result = result;
15+
}
16+
17+
public boolean isStatus() {
18+
return status;
19+
}
20+
21+
public void setStatus(boolean status) {
22+
this.status = status;
23+
}
24+
25+
public Exception getException() {
26+
return exception;
27+
}
28+
29+
public void setException(Exception exception) {
30+
this.exception = exception;
31+
}
32+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package io.kimmking.rpcfx.api;
2+
3+
public class User {
4+
5+
public User(){}
6+
7+
public User(int id, String name) {
8+
this.id = id;
9+
this.name = name;
10+
}
11+
12+
private int id;
13+
private String name;
14+
15+
public int getId() {
16+
return id;
17+
}
18+
19+
public void setId(int id) {
20+
this.id = id;
21+
}
22+
23+
public String getName() {
24+
return name;
25+
}
26+
27+
public void setName(String name) {
28+
this.name = name;
29+
}
30+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package io.kimmking.rpcfx.api;
2+
3+
public interface UserService {
4+
5+
User findById(int id);
6+
7+
}

07rpc/rpc01/rpcfx-client/pom.xml

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>io.kimmking</groupId>
7+
<artifactId>rpcfx</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<!-- <relativePath/> &lt;!&ndash; lookup parent from repository &ndash;&gt;-->
10+
</parent>
11+
<groupId>io.kimmking</groupId>
12+
<artifactId>rpcfx-client</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>rpcfx-client</name>
15+
16+
<properties>
17+
<java.version>1.8</java.version>
18+
</properties>
19+
20+
<dependencies>
21+
<dependency>
22+
<groupId>io.kimmking</groupId>
23+
<artifactId>rpcfx-api</artifactId>
24+
<version>${project.version}</version>
25+
</dependency>
26+
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter</artifactId>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-web</artifactId>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>com.alibaba</groupId>
39+
<artifactId>fastjson</artifactId>
40+
<version>1.2.70</version>
41+
</dependency>
42+
43+
<dependency>
44+
<groupId>com.squareup.okhttp3</groupId>
45+
<artifactId>okhttp</artifactId>
46+
<version>3.12.2</version>
47+
</dependency>
48+
49+
<dependency>
50+
<groupId>org.springframework.boot</groupId>
51+
<artifactId>spring-boot-starter-test</artifactId>
52+
<scope>test</scope>
53+
<exclusions>
54+
<exclusion>
55+
<groupId>org.junit.vintage</groupId>
56+
<artifactId>junit-vintage-engine</artifactId>
57+
</exclusion>
58+
</exclusions>
59+
</dependency>
60+
</dependencies>
61+
62+
<build>
63+
<plugins>
64+
<plugin>
65+
<groupId>org.springframework.boot</groupId>
66+
<artifactId>spring-boot-maven-plugin</artifactId>
67+
</plugin>
68+
</plugins>
69+
</build>
70+
71+
</project>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package io.kimmking.rpcfx.client;
2+
3+
4+
import com.alibaba.fastjson.JSON;
5+
import io.kimmking.rpcfx.api.RpcfxRequest;
6+
import io.kimmking.rpcfx.api.RpcfxResponse;
7+
import okhttp3.MediaType;
8+
import okhttp3.OkHttpClient;
9+
import okhttp3.Request;
10+
import okhttp3.RequestBody;
11+
12+
import java.io.IOException;
13+
import java.lang.reflect.InvocationHandler;
14+
import java.lang.reflect.Method;
15+
import java.lang.reflect.Proxy;
16+
17+
public class Rpcfx {
18+
public static <T> T create(final Class<T> serviceClass, final String url) {
19+
20+
return (T) Proxy.newProxyInstance(Rpcfx.class.getClassLoader(), new Class[]{serviceClass}, new RpcfxInvocationHandler(serviceClass, url));
21+
22+
}
23+
24+
public static class RpcfxInvocationHandler implements InvocationHandler {
25+
26+
public static final MediaType JSONTYPE = MediaType.get("application/json; charset=utf-8");
27+
28+
29+
private final Class<?> serviceClass;
30+
private final String url;
31+
public <T> RpcfxInvocationHandler(Class<T> serviceClass, String url) {
32+
this.serviceClass = serviceClass;
33+
this.url = url;
34+
}
35+
36+
@Override
37+
public Object invoke(Object proxy, Method method, Object[] params) throws Throwable {
38+
RpcfxRequest request = new RpcfxRequest();
39+
request.setServiceClass(this.serviceClass.getName());
40+
request.setMethod(method.getName());
41+
request.setParams(params);
42+
43+
RpcfxResponse response = post(request, url);
44+
return JSON.parse(response.getResult().toString());
45+
}
46+
47+
private RpcfxResponse post(RpcfxRequest req, String url) throws IOException {
48+
String reqJson = JSON.toJSONString(req);
49+
OkHttpClient client = new OkHttpClient();
50+
final Request request = new Request.Builder()
51+
.url(url)
52+
.post(RequestBody.create(JSONTYPE, reqJson))
53+
.build();
54+
String respJson = client.newCall(request).execute().body().string();
55+
return JSON.parseObject(respJson, RpcfxResponse.class);
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)