Skip to content

Commit 0c1ef7b

Browse files
committed
增加最新代码和文档
1 parent e566889 commit 0c1ef7b

File tree

11 files changed

+383
-4
lines changed

11 files changed

+383
-4
lines changed

docs/java.util.ServiceLoader.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,5 @@ include::{sourcedir}/ServiceLoaderTest.java[]
2828
== 参考资料
2929

3030
. http://www.throwable.club/2018/11/30/java-service-loader/[浅析JDK中ServiceLoader的源码 - Throwable's Blog] -- 这里的代码是基于 JDK 8 的,和 JDK 11 的代码已经相差很大。
31+
. https://www.cnblogs.com/vivotech/p/16381937.html[剖析 SPI 在 Spring 中的应用^] -- 讲解了 Java、Spring、Dubbo 等三方面的实现,细致入微。
32+
. https://www.cnblogs.com/better-farther-world2099/articles/17092788.html[SpringBoot的SPI机制^]
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.diguage.truman.jgroups;
2+
3+
import java.util.Scanner;
4+
5+
/**
6+
* 客户端01
7+
*/
8+
public class ChatClient01 {
9+
// private static final String CONFIG_XML = "jgroups-chat-tcp.xml";
10+
//
11+
// public static void main(String[] args) {
12+
// SimpleChat4 chat01 = new SimpleChat4(CONFIG_XML);
13+
// chat01.start();
14+
// scannerChat(chat01);
15+
// }
16+
//
17+
// private static void scannerChat(SimpleChat4 chat01) {
18+
// Scanner scanner = new Scanner(System.in);
19+
// while (true) {
20+
// System.out.print("> ");
21+
// System.out.flush();
22+
// String line = scanner.next();
23+
// if (line.startsWith("quit") || line.startsWith("exit")) {
24+
// System.exit(-1);
25+
// break;
26+
// }
27+
// chat01.sendMessage(null, line);
28+
// }
29+
// }
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.diguage.truman.jgroups;
2+
3+
import java.util.Scanner;
4+
5+
/**
6+
* 客户端02
7+
*/
8+
public class ChatClient02 {
9+
// private static final String CONFIG_XML = "jgroups-chat-tcp.xml";
10+
//
11+
// public static void main(String[] args) {
12+
// SimpleChat4 chat02 = new SimpleChat4(CONFIG_XML);
13+
// chat02.start();
14+
// scannerChat(chat02);
15+
// }
16+
//
17+
// private static void scannerChat(SimpleChat4 chat01) {
18+
// Scanner scanner = new Scanner(System.in);
19+
// while (true) {
20+
// System.out.print("> ");
21+
// System.out.flush();
22+
// String line = scanner.next();
23+
// if (line.startsWith("quit") || line.startsWith("exit")) {
24+
// System.exit(-1);
25+
// break;
26+
// }
27+
// chat01.sendMessage(null, line);
28+
// }
29+
// }
30+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.diguage.truman.jgroups;
2+
3+
import org.jgroups.*;
4+
import org.jgroups.util.Util;
5+
6+
import java.io.*;
7+
import java.time.LocalTime;
8+
import java.util.LinkedList;
9+
import java.util.List;
10+
11+
public class SimpleChat implements Receiver {
12+
JChannel channel;
13+
String user_name = System.getProperty("user.name", LocalTime.now().toString());
14+
final List<String> state = new LinkedList<>();
15+
16+
public void viewAccepted(View new_view) {
17+
System.out.println("** view: " + new_view);
18+
}
19+
20+
public void receive(Message msg) {
21+
String line = msg.getSrc() + ": " + msg.getObject();
22+
System.out.println(line);
23+
synchronized (state) {
24+
state.add(line);
25+
}
26+
}
27+
28+
public void getState(OutputStream output) throws Exception {
29+
synchronized (state) {
30+
Util.objectToStream(state, new DataOutputStream(output));
31+
}
32+
}
33+
34+
public void setState(InputStream input) throws Exception {
35+
List<String> list = Util.objectFromStream(new DataInputStream(input));
36+
synchronized (state) {
37+
state.clear();
38+
state.addAll(list);
39+
}
40+
System.out.println("received state (" + list.size() + " messages in chat history):");
41+
list.forEach(System.out::println);
42+
}
43+
44+
45+
private void start() throws Exception {
46+
// channel = new JChannel(Thread.currentThread().getContextClassLoader().getResourceAsStream("jgroups-chat-udp.xml"));
47+
channel = new JChannel("jgroups-chat-tcp.xml");
48+
channel.setReceiver(this);
49+
channel.connect("ChatCluster");
50+
channel.getState(null, 10000);
51+
eventLoop();
52+
channel.close();
53+
}
54+
55+
private void eventLoop() {
56+
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
57+
while (true) {
58+
try {
59+
System.out.print("> ");
60+
System.out.flush();
61+
String line = in.readLine().toLowerCase();
62+
if (line.startsWith("quit") || line.startsWith("exit")) {
63+
break;
64+
}
65+
line = "[" + user_name + "] " + line;
66+
Message msg = new ObjectMessage(null, line);
67+
channel.send(msg);
68+
} catch (Exception e) {
69+
}
70+
}
71+
}
72+
73+
74+
public static void main(String[] args) throws Exception {
75+
new SimpleChat().start();
76+
}
77+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.diguage.truman.jgroups;
2+
3+
import org.jgroups.*;
4+
import org.jgroups.util.Util;
5+
6+
import java.io.*;
7+
import java.time.LocalTime;
8+
import java.util.LinkedList;
9+
import java.util.List;
10+
11+
public class SimpleChat2 implements Receiver {
12+
JChannel channel;
13+
String user_name = System.getProperty("user.name", LocalTime.now().toString());
14+
final List<String> state = new LinkedList<>();
15+
16+
public void viewAccepted(View new_view) {
17+
System.out.println("** view: " + new_view);
18+
}
19+
20+
public void receive(Message msg) {
21+
String line = msg.getSrc() + ": " + msg.getObject();
22+
System.out.println(line);
23+
synchronized (state) {
24+
state.add(line);
25+
}
26+
}
27+
28+
public void getState(OutputStream output) throws Exception {
29+
synchronized (state) {
30+
Util.objectToStream(state, new DataOutputStream(output));
31+
}
32+
}
33+
34+
public void setState(InputStream input) throws Exception {
35+
List<String> list = Util.objectFromStream(new DataInputStream(input));
36+
synchronized (state) {
37+
state.clear();
38+
state.addAll(list);
39+
}
40+
System.out.println("received state (" + list.size() + " messages in chat history):");
41+
list.forEach(System.out::println);
42+
}
43+
44+
45+
private void start() throws Exception {
46+
// channel = new JChannel(Thread.currentThread().getContextClassLoader().getResourceAsStream("jgroups-chat-udp.xml"));
47+
channel = new JChannel("jgroups-chat-tcp.xml");
48+
channel.setReceiver(this);
49+
channel.connect("ChatCluster");
50+
channel.getState(null, 10000);
51+
eventLoop();
52+
channel.close();
53+
}
54+
55+
private void eventLoop() {
56+
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
57+
while (true) {
58+
try {
59+
System.out.print("> ");
60+
System.out.flush();
61+
String line = in.readLine().toLowerCase();
62+
if (line.startsWith("quit") || line.startsWith("exit")) {
63+
break;
64+
}
65+
line = "[" + user_name + "] " + line;
66+
Message msg = new ObjectMessage(null, line);
67+
channel.send(msg);
68+
} catch (Exception e) {
69+
}
70+
}
71+
}
72+
73+
74+
public static void main(String[] args) throws Exception {
75+
new SimpleChat2().start();
76+
}
77+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.diguage.truman.jgroups;
2+
3+
import java.io.InputStream;
4+
import java.nio.charset.Charset;
5+
import org.jgroups.Address;
6+
import org.jgroups.JChannel;
7+
import org.jgroups.Message;
8+
//import org.jgroups.ReceiverAdapter;
9+
import org.jgroups.View;
10+
11+
/**
12+
* jGroups
13+
*/
14+
public class SimpleChat4 {
15+
// private static final String DEFULT_CONFIG_XML = "jgroups-chat-udp.xml";
16+
// /**
17+
// * 配置文件.
18+
// */
19+
// private String confXml;
20+
//
21+
// /**
22+
// * 集群名称.
23+
// */
24+
// private static final String CLUSTER_NAME = "WTCLOSYN-SIMPLE-CHAT";
25+
// /**
26+
// * 字符编码
27+
// */
28+
// private static final Charset CHARSET = Charset.defaultCharset();
29+
// /**
30+
// * 节点通道.
31+
// */
32+
// private JChannel channel = null;
33+
//
34+
// public SimpleChat4() {
35+
// this.confXml = DEFULT_CONFIG_XML;
36+
// }
37+
//
38+
// public SimpleChat4(String confXml) {
39+
// this.confXml = confXml;
40+
// }
41+
//
42+
// /**
43+
// * 发送消息
44+
// */
45+
// public void start() {
46+
// try {
47+
// InputStream cfg = SimpleChat4.class.getClassLoader().getResourceAsStream(confXml);
48+
// channel = new JChannel(cfg);
49+
// //连接到集群
50+
// channel.connect(CLUSTER_NAME);
51+
// channel.setDiscardOwnMessages(true);
52+
// //指定Receiver用来收消息和得到View改变的通知
53+
// channel.setReceiver(this);
54+
// }catch (Exception e){
55+
// System.out.println("启动Chat失败!");
56+
// }
57+
// }
58+
//
59+
// public void sendMessage(Address dst, String text){
60+
// try {
61+
// //Message构造函数的第一个参数代表目的地地址,这里传null代表要发消息给集群内的所有地址
62+
// //第二个参数表示源地址,传null即可,框架会自动赋值
63+
// //第三个参数line会被序列化成byte[]然后发送,推荐自己序列化而不是用java自带的序列化
64+
// Message msg = new Message(dst, null, text.getBytes(CHARSET));
65+
// //发消息到集群
66+
// channel.send(msg);
67+
// } catch (Exception e) {
68+
// System.out.println("Chat发送消息失败!");
69+
// }
70+
// }
71+
//
72+
// @Override
73+
// public void receive(Message msg) {
74+
// String line = msg.getSrc() + ":" + new String(msg.getBuffer(), CHARSET);
75+
// System.out.println(line);
76+
// }
77+
//
78+
// @Override
79+
// public void viewAccepted(View view) {
80+
// System.out.println("A client has changed!" + view.toString());
81+
// }
82+
}

src/main/java/com/diguage/truman/netty/protobuf/StudentPOJO.java

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/java/com/diguage/truman/netty/protobuf2/MyDataInfo.java

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.diguage.truman.okhttp;
2+
3+
import okhttp3.ConnectionPool;
4+
import okhttp3.OkHttpClient;
5+
import okhttp3.Request;
6+
import okhttp3.Response;
7+
8+
import java.io.IOException;
9+
import java.util.concurrent.TimeUnit;
10+
11+
public class ConnPoolTest {
12+
public static void main(String[] args) {
13+
// 创建自定义的连接池
14+
ConnectionPool connectionPool = new ConnectionPool(10, 5, TimeUnit.MINUTES);
15+
16+
OkHttpClient client = new OkHttpClient.Builder()
17+
.connectionPool(connectionPool)
18+
.build();
19+
20+
// 创建请求
21+
Request request = new Request.Builder()
22+
.url("https://www.baidu.com")
23+
.build();
24+
25+
// 发送请求并获取响应
26+
try (Response response = client.newCall(request).execute()) {
27+
if (response.isSuccessful()) {
28+
System.out.println(response.body().string());
29+
} else {
30+
System.err.println("Request failed: " + response);
31+
}
32+
} catch (IOException e) {
33+
e.printStackTrace();
34+
}
35+
}
36+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
com.diguage.truman.Gua=com.diguage.truman.TestSerializer

0 commit comments

Comments
 (0)