Skip to content

Commit e6ff04e

Browse files
author
zhourenjian
committed
Remove Java 5.0 Syntax warnings
Remove unnecessary "new Date().getTime()" (replaced with System.currentMillisconds()" Fixing bug of SimplePipe/CompoundPipe, improve its stabilities Code refactor
1 parent ab2e0f7 commit e6ff04e

18 files changed

+460
-413
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ protected SimpleRPCRunnable getRunnableByRequest(String request) {
1313
if (pipe instanceof CompoundPipeRunnable) {
1414
CompoundPipeRunnable p = (CompoundPipeRunnable) pipe;
1515
p.weave(session);
16-
//p.status = 3;
1716
}
1817
}
1918
return runnable;

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

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,33 @@
11
package net.sf.j2s.ajax;
22

3-
import java.util.Date;
4-
53
public class CompoundPipeRequest extends SimplePipeRequest {
64

75
static CompoundPipeRunnable[] pipes = new CompoundPipeRunnable[3];
86

97
public static void weave(String id, CompoundPipeSession p) {
108
final CompoundPipeRunnable pipe = retrievePipe(id, true);
119
if (pipe.status == 0 || !pipe.isPipeLive()) {
12-
pipe.weave(p);
10+
pipe.weave(p); // no matter woven or not, continue to start the pipe
1311
pipe.updateStatus(true);
14-
SimplePipeRequest.pipe(pipe);
15-
pipe.status = 1; // pipe
12+
if (pipe.status == 0) { // pipe is not started yet
13+
pipe.status = 1; // pipe request is sent
14+
pipe.pipeKey = null;
15+
SimplePipeRequest.pipe(pipe);
16+
}
1617
} else {
17-
pipe.weave(p);
18-
if (pipe.pipeKey != null) {
19-
p.pipeKey = pipe.pipeKey;
20-
SimpleRPCRequest.request(p);
21-
if (pipe.status < 2) {
22-
pipe.status = 2; // requested
23-
}
18+
if (!pipe.weave(p) && p.isPipeLive()) { // already woven!
19+
return;
20+
}
21+
p.pipeKey = pipe.pipeKey;
22+
SimpleRPCRequest.request(p);
23+
if (pipe.status < 2) {
24+
pipe.status = 2; // requested
2425
}
2526
}
2627
}
2728

2829
static void pipeFailed(CompoundPipeRunnable pipe) {
29-
long now = new Date().getTime();
30+
long now = System.currentTimeMillis();
3031
if (now - pipe.lastSetupRetried > 5 * 60 * 1000) { // five minutes
3132
pipe.setupFailedRetries = 0;
3233
}
@@ -92,6 +93,7 @@ private static CompoundPipeRunnable createPipe(String id) {
9293
public void ajaxOut() {
9394
super.ajaxOut();
9495
if (!pipeAlive) {
96+
CompoundPipeRequest.pipeFailed(this);
9597
return; // Not setup yet!
9698
}
9799
for (int i = 0; i < pipes.length; i++) {

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

Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ private static String nextSessionKey() {
2525
int setupFailedRetries;
2626
long lastSetupRetried;
2727

28+
long lastSetup;
29+
2830
public CompoundPipeRunnable() {
2931
pipes = new CompoundPipeSession[4];
3032
status = 0; // starting
@@ -34,6 +36,7 @@ public CompoundPipeRunnable() {
3436
rpcMethod = "POST";
3537
pipeURL = "simplepipe";
3638
rpcURL = "piperpc";
39+
lastSetup = System.currentTimeMillis();
3740
}
3841

3942
protected CompoundPipeSession getSession(String session) {
@@ -55,6 +58,8 @@ public boolean pipeDestroy() {
5558
pipes[i].pipeDestroy();
5659
}
5760
}
61+
pipeKey = null;
62+
status = 0; // starting
5863
return super.pipeDestroy();
5964
}
6065

@@ -78,6 +83,9 @@ public boolean isPipeLive() {
7883
if (pipeAlive && status < 3) { // connected
7984
return true; // still in starting status
8085
}
86+
if (status == 3 && System.currentTimeMillis() - lastSetup < 30000) {
87+
return true;
88+
}
8189
if (super.isPipeLive()) {
8290
for (int i = 0; i < pipes.length; i++) {
8391
if (pipes[i] != null && pipes[i].isPipeLive()) {
@@ -117,17 +125,29 @@ public void pipeLost() {
117125
@Override
118126
public void keepPipeLive() {
119127
for (int i = 0; i < pipes.length; i++) {
120-
if (pipes[i] != null) {
128+
if (pipes[i] != null && pipes[i].isPipeLive()) {
121129
pipes[i].keepPipeLive();
122130
}
123131
}
124132
}
125133

126-
public void weave(CompoundPipeSession pipe) {
134+
public boolean weave(CompoundPipeSession pipe) {
135+
pipe.pipeReset();
127136
synchronized (pipes) {
128137
for (int i = 0; i < pipes.length; i++) {
129138
if (pipe == pipes[i]) {
130-
return;
139+
return false;
140+
}
141+
}
142+
for (int i = 0; i < pipes.length; i++) {
143+
if (pipe.session != null && pipes[i] != null
144+
&& pipe.session.equals(pipes[i].session)) {
145+
if (pipes[i].isPipeLive()) {
146+
System.out.println("pipe session " + pipes[i].session + " is still live!!");
147+
}
148+
pipes[i] = pipe; // replace it!!!
149+
lastSetup = System.currentTimeMillis();
150+
return true;
131151
}
132152
}
133153
boolean added = false;
@@ -142,6 +162,7 @@ public void weave(CompoundPipeSession pipe) {
142162
CompoundPipeSession[] newPipes = new CompoundPipeSession[pipes.length + 4];
143163
System.arraycopy(pipes, 0, newPipes, 0, pipes.length);
144164
newPipes[pipes.length] = pipe;
165+
lastSetup = System.currentTimeMillis();
145166
}
146167
}
147168
pipe.parent = this;
@@ -159,15 +180,41 @@ public void weave(CompoundPipeSession pipe) {
159180
break;
160181
}
161182
}
183+
return true;
162184
}
163185

164-
public void unweave(CompoundPipeSession pipe) {
186+
public boolean unweave(CompoundPipeSession pipe) {
187+
if (pipeKey == null || !pipeKey.equals(pipe.pipeKey)) {
188+
return false;
189+
}
165190
for (int i = 0; i < pipes.length; i++) {
166-
if (pipe == pipes[i]) {
191+
if (pipe == pipes[i] || (pipe.session != null && pipes[i] != null
192+
&& pipe.session.equals(pipes[i].session))) {
167193
pipes[i] = null;
168-
break;
194+
lastSetup = System.currentTimeMillis();
195+
return true;
196+
}
197+
}
198+
return false;
199+
}
200+
201+
public int getActivePipeSessionCount() {
202+
int count = 0;
203+
for (int i = 0; i < pipes.length; i++) {
204+
if (pipes[i] != null) {
205+
count++;
206+
}
207+
}
208+
return count;
209+
}
210+
211+
public boolean isEmpty() {
212+
for (int i = 0; i < pipes.length; i++) {
213+
if (pipes[i] != null) {
214+
return false;
169215
}
170216
}
217+
return true;
171218
}
172219

173220
@Override
@@ -230,4 +277,26 @@ public String getPipeMethod() {
230277
return pipeMethod;
231278
}
232279

280+
@Override
281+
protected long pipeMonitoringInterval() {
282+
long monitorInterval = super.pipeMonitoringInterval();
283+
for (int i = 0; i < pipes.length; i++) {
284+
if (pipes[i] != null) {
285+
monitorInterval = Math.min(monitorInterval, pipes[i].pipeWaitClosingInterval());
286+
}
287+
}
288+
return monitorInterval;
289+
}
290+
291+
@Override
292+
protected long pipeWaitClosingInterval() {
293+
long closingInterval = super.pipeWaitClosingInterval();
294+
for (int i = 0; i < pipes.length; i++) {
295+
if (pipes[i] != null) {
296+
closingInterval = Math.max(closingInterval, pipes[i].pipeWaitClosingInterval());
297+
}
298+
}
299+
return closingInterval;
300+
}
301+
233302
}

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

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
public abstract class CompoundPipeSession extends SimplePipeRunnable {
44

5-
private static class PipeSessionClosedEvent extends CompoundSerializable {
5+
public static final class PipeSessionClosedEvent extends CompoundSerializable {
66

77
}
88

@@ -21,6 +21,11 @@ public void ajaxRun() {
2121
}
2222
}
2323

24+
@Override
25+
public boolean isPipeLive() {
26+
return super.isPipeLive() && session != null;
27+
}
28+
2429
@Override
2530
public void pipeCreated() {
2631
super.pipeCreated();
@@ -51,24 +56,15 @@ public boolean pipeDestroy() {
5156
SimplePipeRunnable pipe = SimplePipeHelper.getPipe(pipeKey);
5257
if (pipe instanceof CompoundPipeRunnable) {
5358
CompoundPipeRunnable cp = (CompoundPipeRunnable) pipe;
59+
if (cp.status < 3) {
60+
cp.status = 3; // no matter what happens, mark status as completed
61+
}
5462
cp.unweave(this);
5563
}
64+
session = null;
5665
return true;
5766
}
5867

59-
@Override
60-
public void pipeFailed() {
61-
super.pipeFailed();
62-
SimplePipeRunnable pipe = SimplePipeHelper.getPipe(pipeKey);
63-
if (pipe instanceof CompoundPipeRunnable) {
64-
CompoundPipeRunnable cp = (CompoundPipeRunnable) pipe;
65-
if (cp.status < 3) {
66-
cp.status = 3; // connected but failed to create child pipe
67-
}
68-
updateStatus(false);
69-
}
70-
}
71-
7268
@Override
7369
final public SimpleSerializable[] through(Object... args) {
7470
CompoundSerializable[] cs = convert(args);
@@ -85,7 +81,7 @@ final public SimpleSerializable[] through(Object... args) {
8581
public abstract CompoundSerializable[] convert(Object... args);
8682

8783
@Override
88-
protected void pipeThrough(Object... args) {
84+
public void pipeThrough(Object... args) {
8985
SimplePipeRunnable pipe = SimplePipeHelper.getPipe(pipeKey);
9086
if (!(pipe instanceof CompoundPipeRunnable)) return;
9187
CompoundPipeRunnable cp = (CompoundPipeRunnable) pipe;
@@ -140,8 +136,9 @@ public boolean deal(PipeSessionClosedEvent evt) {
140136
}
141137

142138
if (!p.isPipeLive()) {
139+
String pipeKey = this.pipeKey;
143140
p.pipeDestroy();
144-
SimplePipeHelper.removePipe(this.pipeKey);
141+
SimplePipeHelper.removePipe(pipeKey);
145142
}
146143
}
147144
return true;

0 commit comments

Comments
 (0)