9
9
import java .net .InetAddress ;
10
10
import java .net .ServerSocket ;
11
11
import java .net .Socket ;
12
- import java .util .concurrent .CompletableFuture ;
13
12
import java .util .concurrent .ExecutorService ;
14
13
import java .util .concurrent .Executors ;
14
+ import java .util .concurrent .ForkJoinPool ;
15
15
16
16
public class TCPReverseProxy {
17
17
@@ -29,24 +29,35 @@ public class TCPReverseProxy {
29
29
30
30
private ServerSocket serverSocket ;
31
31
32
- private final ExecutorService executor ;
32
+ private final ExecutorService bossExecutor = Executors .newFixedThreadPool (1 );
33
+
34
+ private final ExecutorService workerExecutor ;
35
+
36
+
37
+ public TCPReverseProxy (String sourceHost , int sourcePort , String targetHost , int targetPort , ExecutorService workerExecutor ) {
38
+ this .sourceHost = sourceHost ;
39
+ this .sourcePort = sourcePort ;
40
+ this .targetHost = targetHost ;
41
+ this .targetPort = targetPort ;
42
+ this .workerExecutor = workerExecutor ;
43
+ }
33
44
34
45
public TCPReverseProxy (String sourceHost , int sourcePort , String targetHost , int targetPort ) {
35
46
this .sourceHost = sourceHost ;
36
47
this .sourcePort = sourcePort ;
37
48
this .targetHost = targetHost ;
38
49
this .targetPort = targetPort ;
39
- executor = Executors . newFixedThreadPool ( 1 );
50
+ workerExecutor = ForkJoinPool . commonPool ( );
40
51
}
41
52
42
53
public void start () {
43
- executor .submit (() -> {
54
+ bossExecutor .submit (() -> {
44
55
try {
45
56
serverSocket = new ServerSocket (sourcePort , 0 , InetAddress .getByName (sourceHost ));
46
57
log .info ("proxy server started {}:{} <--> {}:{}" , sourceHost , sourcePort , targetHost , targetPort );
47
58
while (!serverSocket .isClosed ()) {
48
59
Socket sourceSocket = serverSocket .accept ();
49
- CompletableFuture . runAsync (() -> {
60
+ workerExecutor . execute (() -> {
50
61
try {
51
62
/**
52
63
* 将sourceSocket获取的内容,写入到targetSocket
@@ -55,7 +66,7 @@ public void start() {
55
66
Socket targetSocket = new Socket (targetHost , targetPort );
56
67
log .info ("{} connected, proxy to {}" , sourceSocket .getRemoteSocketAddress ().toString (), targetSocket .getRemoteSocketAddress ().toString ());
57
68
// 监听源端主动传入的消息写给目标端
58
- CompletableFuture . runAsync (() -> {
69
+ workerExecutor . execute (() -> {
59
70
// isClosed只能监听本地连接状态。若远端关闭或者网络问题,无法监听到。
60
71
if (sourceSocket .isClosed () || targetSocket .isClosed ()) {
61
72
return ;
@@ -80,7 +91,7 @@ public void start() {
80
91
}
81
92
});
82
93
// 监听目标端主动写回的消息写回源端
83
- CompletableFuture . runAsync (() -> {
94
+ workerExecutor . execute (() -> {
84
95
if (sourceSocket .isClosed () || targetSocket .isClosed ()) {
85
96
return ;
86
97
}
@@ -122,7 +133,7 @@ public void stop() {
122
133
} catch (Exception ignore ) {
123
134
}
124
135
}
125
- executor .shutdown ();
136
+ bossExecutor .shutdown ();
126
137
log .info ("proxy server stoped {}:{} <--> {}:{}" , sourceHost , sourcePort , targetHost , targetPort );
127
138
}
128
139
0 commit comments