Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a58a20d

Browse files
author
zhourenjian
committed
Add a managable attribute to SimplePipe so SimplePipe's pipe monitor thread can be a static thread for all pipes. In a result, thread number is decreased.
Using a different way to avoid using Thread.sleep inside pipe monitor
1 parent b38c709 commit a58a20d

File tree

7 files changed

+156
-37
lines changed

7 files changed

+156
-37
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package net.sf.j2s.ajax;
2+
3+
import java.util.Collections;
4+
import java.util.HashMap;
5+
import java.util.HashSet;
6+
import java.util.Iterator;
7+
import java.util.Map;
8+
import java.util.Set;
9+
10+
import net.sf.j2s.annotation.J2SIgnore;
11+
12+
public class ManagedPipeHelper {
13+
14+
@J2SIgnore
15+
private static Map<String, SimplePipeRunnable> pipeSessions = null;
16+
@J2SIgnore
17+
private static boolean monitored = false;
18+
19+
@J2SIgnore
20+
private static boolean monitorRunning = false;
21+
22+
@J2SIgnore
23+
private static long monitoringInterval = 10000; // 10s
24+
25+
26+
@J2SIgnore
27+
public static long getMonitoringInterval() {
28+
return monitoringInterval;
29+
}
30+
31+
@J2SIgnore
32+
public static void setMonitoringInterval(long monitoringInterval) {
33+
ManagedPipeHelper.monitoringInterval = monitoringInterval;
34+
}
35+
36+
@J2SIgnore
37+
public static void stopMonitor() {
38+
monitorRunning = false;
39+
}
40+
41+
static synchronized void init() {
42+
if (pipeSessions == null) {
43+
pipeSessions = Collections.synchronizedMap(new HashMap<String, SimplePipeRunnable>(20));
44+
}
45+
}
46+
47+
@J2SIgnore
48+
static void startMonitor() {
49+
init();
50+
monitorRunning = true;
51+
new Thread(new Runnable() {
52+
53+
public void run() {
54+
while (monitorRunning) {
55+
try {
56+
Thread.sleep(getMonitoringInterval());
57+
} catch (InterruptedException e) {
58+
}
59+
if (pipeSessions == null) {
60+
System.err.println("Pipe sessions are null or empty! Managed pipe session monitor exited!");
61+
break;
62+
}
63+
Set<SimplePipeRunnable> pipes = new HashSet<SimplePipeRunnable>(pipeSessions.values());
64+
for (Iterator<SimplePipeRunnable> itr = pipes.iterator(); itr.hasNext();) {
65+
SimplePipeRunnable pipe = itr.next();
66+
try {
67+
if (!pipe.isPipeLive()) {
68+
if (System.currentTimeMillis() - pipe.lastLiveDetected > pipe.pipeWaitClosingInterval()) {
69+
try {
70+
pipe.pipeDestroy();
71+
} catch (Throwable e) {
72+
e.printStackTrace();
73+
}
74+
try {
75+
if (pipe.closer != null) {
76+
pipe.closer.helpClosing(pipe);
77+
} else {
78+
pipe.pipeClosed();
79+
}
80+
} catch (Throwable e) {
81+
e.printStackTrace();
82+
}
83+
pipeSessions.remove(pipe.pipeKey);
84+
}
85+
} else {
86+
pipe.lastLiveDetected = System.currentTimeMillis();
87+
}
88+
} catch (Throwable e) {
89+
e.printStackTrace();
90+
}
91+
}
92+
}
93+
}
94+
95+
}, "Managed Pipe Session Monitor").start();
96+
}
97+
98+
@J2SIgnore
99+
public static void monitoringPipe(SimplePipeRunnable pipe) {
100+
init();
101+
pipeSessions.put(pipe.pipeKey, pipe);
102+
pipe.lastLiveDetected = System.currentTimeMillis();
103+
if (monitored) {
104+
return;
105+
}
106+
monitored = true;
107+
startMonitor();
108+
}
109+
110+
}

sources/net.sf.j2s.ajax/ajaxpipe/net/sf/j2s/ajax/SimplePipeHelper.java

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -198,33 +198,6 @@ static boolean notifyPipeStatus(String key, boolean live) {
198198
return false;
199199
}
200200

201-
/**
202-
* Wait some more seconds to check whether the pipe is to be closed or not.
203-
* @param runnable
204-
* @return whether the pipe is to be closed or not.
205-
*/
206-
@J2SIgnore
207-
static boolean waitAMomentForClosing(final SimplePipeRunnable runnable) {
208-
if (runnable == null) {
209-
return true;
210-
}
211-
long extra = runnable.pipeWaitClosingInterval();
212-
long interval = runnable.pipeMonitoringInterval();
213-
if (interval <= 0) {
214-
interval = 1000; // default as 1s
215-
}
216-
217-
while (!runnable.isPipeLive() && extra > 0) {
218-
try {
219-
Thread.sleep(interval);
220-
extra -= interval;
221-
} catch (InterruptedException e) {
222-
//e.printStackTrace();
223-
}
224-
}
225-
return !runnable.isPipeLive();
226-
}
227-
228201
@J2SIgnore
229202
public static void helpClosing(SimplePipeRunnable pipe) {
230203
if (pipe.closer != null) {

sources/net.sf.j2s.ajax/ajaxpipe/net/sf/j2s/ajax/SimplePipeHttpServlet.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,14 +189,28 @@ protected void doPipe(final HttpServletResponse resp, String key, String type, S
189189
long beforeLoop = new Date().getTime();
190190
Vector<SimpleSerializable> vector = null;
191191
int priority = 0;
192+
long lastLiveDetected = System.currentTimeMillis();
193+
SimplePipeRunnable pipe = SimplePipeHelper.getPipe(key);
194+
long waitClosingInterval = 5000;
195+
if (pipe != null) {
196+
waitClosingInterval = pipe.pipeWaitClosingInterval();
197+
}
192198
while ((vector = SimplePipeHelper.getPipeVector(key)) != null
193199
/* && SimplePipeHelper.isPipeLive(key) */ // check it!
194200
&& !writer.checkError()) {
195201
if (!SimplePipeHelper.isPipeLive(key)) {
196-
boolean okToClose = SimplePipeHelper.waitAMomentForClosing(SimplePipeHelper.getPipe(key));
197-
if (okToClose) {
202+
if (System.currentTimeMillis() - lastLiveDetected > waitClosingInterval) {
203+
// break out while loop so pipe connection will be closed
198204
break;
205+
} else { // sleep 1s and continue to check pipe status again
206+
try {
207+
Thread.sleep(1000);
208+
} catch (InterruptedException e) {
209+
}
210+
continue;
199211
}
212+
} else {
213+
lastLiveDetected = System.currentTimeMillis();
200214
}
201215
int size = vector.size();
202216
if (size > 0) {

sources/net.sf.j2s.ajax/ajaxpipe/net/sf/j2s/ajax/SimplePipeRequest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ static void keepPipeLive(final SimplePipeRunnable runnable) {
207207
new Thread(new Runnable() {
208208

209209
public void run() {
210+
long lastLiveDetected = System.currentTimeMillis();
210211
do {
211212
long interval = pipeLiveNotifyInterval;
212213

@@ -233,9 +234,9 @@ public void run() {
233234
boolean pipeLive = runnable.isPipeLive();
234235
if (pipeLive) {
235236
runnable.keepPipeLive();
237+
lastLiveDetected = System.currentTimeMillis();
236238
} else {
237-
boolean okToClose = SimplePipeHelper.waitAMomentForClosing(runnable);
238-
if (okToClose) {
239+
if (System.currentTimeMillis() - lastLiveDetected > runnable.pipeWaitClosingInterval()) {
239240
runnable.pipeDestroy(); // Pipe's server side destroying
240241
runnable.pipeClosed(); // Pipe's client side closing
241242
break;

sources/net.sf.j2s.ajax/ajaxpipe/net/sf/j2s/ajax/SimplePipeRunnable.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ public abstract class SimplePipeRunnable extends SimpleRPCRunnable {
4343

4444
long lastPipeDataReceived;
4545

46+
@J2SIgnore
47+
boolean pipeManaged; // For Java server side's monitoring thread
48+
49+
@J2SIgnore
50+
long lastLiveDetected;
51+
4652
@J2SIgnore
4753
void setPipeHelper(SimplePipeHelper.IPipeThrough helper) {
4854
this.helper = helper;
@@ -182,9 +188,14 @@ public void keepPipeLive() {
182188
* This method is run on server side
183189
*/
184190
protected void pipeMonitoring() {
191+
if (pipeManaged) {
192+
ManagedPipeHelper.monitoringPipe(this);
193+
return;
194+
}
185195
new Thread(new Runnable() {
186196

187197
public void run() {
198+
lastLiveDetected = System.currentTimeMillis();
188199
long interval = pipeMonitoringInterval();
189200
if (interval <= 0) {
190201
interval = 1000;
@@ -196,8 +207,7 @@ public void run() {
196207
//e.printStackTrace();
197208
}
198209
if (!isPipeLive()) {
199-
boolean okToClose = SimplePipeHelper.waitAMomentForClosing(SimplePipeRunnable.this);
200-
if (okToClose) {
210+
if (System.currentTimeMillis() - lastLiveDetected > pipeWaitClosingInterval()) {
201211
pipeDestroy();
202212
if (closer != null) {
203213
closer.helpClosing(SimplePipeRunnable.this);
@@ -206,6 +216,8 @@ public void run() {
206216
}
207217
break;
208218
}
219+
} else {
220+
lastLiveDetected = System.currentTimeMillis();
209221
}
210222
}
211223
}

sources/net.sf.j2s.ajax/ajaxrpc/net/sf/j2s/ajax/SimpleRPCHttpServlet.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ public class SimpleRPCHttpServlet extends HttpServlet {
5454

5555
protected long xssLatency = 60 * 1000; // 60s to send a request?
5656

57+
protected boolean managingPipe = false;
58+
5759
protected long maxPostLimit() {
5860
return postLimit;
5961
}
@@ -140,6 +142,10 @@ public boolean ignoreDefaultFields() {
140142
<param-name>simple.rpc.xss.max.parts</param-name>
141143
<param-value>10</param-value>
142144
</init-param>
145+
<init-param>
146+
<param-name>simple.pipe.managable</param-name>
147+
<param-value>true</param-value>
148+
</init-param>
143149
</servlet>
144150
<servlet-mapping>
145151
<servlet-name>simplerpc</servlet-name>
@@ -188,6 +194,10 @@ public void init() throws ServletException {
188194
e.printStackTrace();
189195
}
190196
}
197+
String managablePipeStr = getInitParameter("simple.pipe.managable");
198+
if (managablePipeStr != null) {
199+
managingPipe = "true".equals(managablePipeStr);
200+
}
191201
super.init();
192202
}
193203

sources/net.sf.j2s.ajax/ajaxswt/net/sf/j2s/ajax/SimplePipeSWTRequest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ static void swtKeepPipeLive(final SimplePipeRunnable runnable, final Display dis
9191
new Thread(new Runnable() {
9292

9393
public void run() {
94+
long lastLiveDetected = System.currentTimeMillis();
9495
do {
9596
long interval = pipeLiveNotifyInterval;
9697

@@ -116,9 +117,9 @@ public void run() {
116117
boolean pipeLive = runnable.isPipeLive();
117118
if (pipeLive) {
118119
runnable.keepPipeLive();
120+
lastLiveDetected = System.currentTimeMillis();
119121
} else {
120-
boolean okToClose = SimplePipeHelper.waitAMomentForClosing(runnable);
121-
if (okToClose) {
122+
if (System.currentTimeMillis() - lastLiveDetected > runnable.pipeWaitClosingInterval()) {
122123
runnable.pipeDestroy();
123124
SWTHelper.syncExec(disp, new Runnable() {
124125
public void run() {
@@ -127,8 +128,6 @@ public void run() {
127128
});
128129
break;
129130
}
130-
131-
break;
132131
}
133132
} else {
134133
SimplePipeRunnable r = SimplePipeHelper.getPipe(runnable.pipeKey);

0 commit comments

Comments
 (0)