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

Skip to content

Commit 11d2203

Browse files
Use ConcurrentHashMap instead of synchronized HashMap, better performance
Support abbreviation of class name in simple json request
1 parent 9e24c73 commit 11d2203

File tree

3 files changed

+77
-90
lines changed

3 files changed

+77
-90
lines changed

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

Lines changed: 61 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@
1313
import java.util.Collections;
1414
import java.util.Comparator;
1515
import java.util.Date;
16-
import java.util.HashMap;
16+
import java.util.Iterator;
1717
import java.util.List;
1818
import java.util.Map;
1919
import java.util.Vector;
2020
import java.util.concurrent.BlockingQueue;
21+
import java.util.concurrent.ConcurrentHashMap;
2122
import java.util.concurrent.LinkedBlockingQueue;
2223
import java.util.concurrent.TimeUnit;
2324

@@ -51,7 +52,11 @@ public static interface IPipeClosing {
5152
@J2SIgnore
5253
private static long monitoringInterval = 10000; // 10s
5354

54-
static Map<String, SimplePipeRunnable> pipes;
55+
// allPipes is for JavaScript, as pipes is for Java
56+
static Object allPipes = null;
57+
58+
@J2SIgnore
59+
private static Map<String, SimplePipeRunnable> pipes = new ConcurrentHashMap<String, SimplePipeRunnable>(50);
5560

5661
@J2SIgnore
5762
private static BlockingQueue<SimplePipeRunnable> toBeDestroyedPipes = new LinkedBlockingQueue<SimplePipeRunnable>();
@@ -66,16 +71,13 @@ private SimplePipeHelper() {
6671
*/
6772
@J2SNative({
6873
"if (key == null || pipe == null) return;",
69-
"if (net.sf.j2s.ajax.SimplePipeHelper.pipes == null) {",
70-
" net.sf.j2s.ajax.SimplePipeHelper.pipes = new Object ();",
74+
"if (net.sf.j2s.ajax.SimplePipeHelper.allPipes == null) {",
75+
" net.sf.j2s.ajax.SimplePipeHelper.allPipes = new Object ();",
7176
"}",
72-
"net.sf.j2s.ajax.SimplePipeHelper.pipes[key] = pipe;"
77+
"net.sf.j2s.ajax.SimplePipeHelper.allPipes[key] = pipe;"
7378
})
7479
public static void registerPipe(String key, SimplePipeRunnable pipe) {
7580
if (key == null || pipe == null) return;
76-
if (pipes == null) {
77-
pipes = Collections.synchronizedMap(new HashMap<String, SimplePipeRunnable>(50));
78-
}
7981
pipes.put(key, pipe);
8082
}
8183

@@ -88,18 +90,14 @@ synchronized static String registerPipe(SimplePipeRunnable pipe) {
8890
System.out.println("ERROR!!! pipeKey should be null here! " + pipe.pipeKey);
8991
}
9092
// if (pipe == null) return null; // should never register null pipe!
91-
if (pipes == null) {
92-
pipes = Collections.synchronizedMap(new HashMap<String, SimplePipeRunnable>(50));
93-
}
94-
9593
String key = nextPipeKey();
9694
while (pipes.get(key) != null) {
97-
key = nextPipeKey();;
95+
key = nextPipeKey();
9896
}
99-
pipes.put(key, pipe);
97+
pipes.put(key, pipe); // FIXME: In rare case, it will override another pipe
10098

10199
// if (pipeMap == null) {
102-
// pipeMap = Collections.synchronizedMap(new HashMap<String, List<SimpleSerializable>>());
100+
// pipeMap = new ConcurrentHashMap<String, List<SimpleSerializable>>();
103101
// }
104102
// List<SimpleSerializable> list = pipeMap.get(key);
105103
// if (list == null) {
@@ -133,23 +131,20 @@ static String nextPipeKey() {
133131
}
134132

135133
@J2SNative({
136-
"delete net.sf.j2s.ajax.SimplePipeHelper.pipes[key];"
134+
"delete net.sf.j2s.ajax.SimplePipeHelper.allPipes[key];"
137135
})
138136
public static void removePipe(String key) {
139137
if (key == null) {
140138
System.out.println("Removing pipe for null key???");
141139
new RuntimeException("Removing null pipe key").printStackTrace();
142140
return;
143141
}
144-
SimplePipeRunnable pipe = null;
145-
if (pipes != null) {
146-
pipe = pipes.remove(key);
147-
if (pipe != null) {
148-
pipe.pipeAlive = false;
149-
pipe.pipeClearData();
150-
synchronized (pipe) {
151-
pipe.notifyAll();
152-
}
142+
SimplePipeRunnable pipe = pipes.remove(key);
143+
if (pipe != null) {
144+
pipe.pipeAlive = false;
145+
pipe.pipeClearData();
146+
synchronized (pipe) {
147+
pipe.notifyAll();
153148
}
154149
}
155150
// if (pipeMap != null) {
@@ -162,12 +157,12 @@ public static void removePipe(String key) {
162157
}
163158

164159
@J2SNative({
165-
"var ps = net.sf.j2s.ajax.SimplePipeHelper.pipes;",
160+
"var ps = net.sf.j2s.ajax.SimplePipeHelper.allPipes;",
166161
"if (ps == null || key == null) return null;",
167162
"return ps[key];"
168163
})
169164
public static SimplePipeRunnable getPipe(String key) {
170-
if (pipes == null || key == null) return null;
165+
if (key == null) return null;
171166
return pipes.get(key);
172167
}
173168

@@ -297,35 +292,29 @@ static void helpClosing(SimplePipeRunnable pipe) {
297292
public static String printStatistics2() {
298293
StringBuilder builder = new StringBuilder();
299294
builder.append("Pipe monitor<br />\r\n");
300-
if (pipes != null) {
301-
builder.append("Totoal pipe count: " + pipes.size() + "<br />\r\n");
302-
// buffer.append("Totoal pipe map count: " + pipeMap.size() + "<br />\r\n");
303-
Object[] keys = pipes.keySet().toArray();
304-
for (int i = 0; i < keys.length; i++) {
305-
String key = (String) keys[i];
306-
SimplePipeRunnable p = pipes.get(key);
307-
List<SimpleSerializable> list = p != null ? p.pipeData : null; //pipeMap.get(key);
308-
if (p instanceof CompoundPipeRunnable) {
309-
CompoundPipeRunnable cp = (CompoundPipeRunnable) p;
310-
int activeCount = 0;
311-
for (int j = 0; j < cp.pipes.length; j++) {
312-
if (cp.pipes[j] != null) {
313-
activeCount++;
314-
}
315-
}
316-
if (activeCount > 2) {
317-
builder.append(i + " Pipe (active=" + activeCount + ") " + cp.pipeKey + " status=" + cp.status + " pipeAlive=" + cp.isPipeLive() + " created=" + new Date(cp.lastSetup) + "<br />\r\n");
295+
builder.append("Totoal pipe count: " + pipes.size() + "<br />\r\n");
296+
// buffer.append("Total pipe map count: " + pipeMap.size() + "<br />\r\n");
297+
int i = 0;
298+
for (Iterator<SimplePipeRunnable> itr = pipes.values().iterator(); itr.hasNext();) {
299+
SimplePipeRunnable p = (SimplePipeRunnable) itr.next();
300+
i++;
301+
List<SimpleSerializable> list = p.pipeData; //pipeMap.get(key);
302+
if (p instanceof CompoundPipeRunnable) {
303+
CompoundPipeRunnable cp = (CompoundPipeRunnable) p;
304+
int activeCount = 0;
305+
for (int j = 0; j < cp.pipes.length; j++) {
306+
if (cp.pipes[j] != null) {
307+
activeCount++;
318308
}
319309
}
320-
if (list != null) {
321-
int size = list.size();
322-
if (size > 20) {
323-
if (p != null) {
324-
builder.append(i + "::: pipe " + p.pipeKey + " size : " + size + " / " + p.pipeAlive + "<br />\r\n");
325-
} else {
326-
builder.append("Error pipe " + key + " with size : " + size + "<br />\r\n");
327-
}
328-
}
310+
if (activeCount > 2) {
311+
builder.append(i + " Pipe (active=" + activeCount + ") " + cp.pipeKey + " status=" + cp.status + " pipeAlive=" + cp.isPipeLive() + " created=" + new Date(cp.lastSetup) + "<br />\r\n");
312+
}
313+
}
314+
if (list != null) {
315+
int size = list.size();
316+
if (size > 20) {
317+
builder.append(i + "::: pipe " + p.pipeKey + " size : " + size + " / " + p.pipeAlive + "<br />\r\n");
329318
}
330319
}
331320
}
@@ -336,34 +325,26 @@ public static String printStatistics2() {
336325
public static String printStatistics() {
337326
StringBuilder builder = new StringBuilder();
338327
builder.append("Pipe monitor<br />\r\n");
339-
if (pipes != null) {
340-
builder.append("Totoal pipe count: " + pipes.size() + "<br />\r\n");
341-
// buffer.append("Totoal pipe map count: " + pipeMap.size() + "<br />\r\n");
342-
Object[] keys = pipes.keySet().toArray();
343-
for (int i = 0; i < keys.length; i++) {
344-
String key = (String) keys[i];
345-
SimplePipeRunnable p = pipes.get(key);
346-
List<SimpleSerializable> list = p != null ? p.pipeData : null; //pipeMap.get(key);
347-
if (p instanceof CompoundPipeRunnable) {
348-
CompoundPipeRunnable cp = (CompoundPipeRunnable) p;
349-
builder.append(i + "Pipe " + cp.pipeKey + " status=" + cp.status + " pipeAlive=" + cp.isPipeLive() + " created=" + new Date(cp.lastSetup) + "<br />\r\n");
350-
for (int j = 0; j < cp.pipes.length; j++) {
351-
CompoundPipeSession ps = cp.pipes[j];
352-
if (ps != null) {
353-
builder.append(j + " : " + ps.session + " / " + ps.isPipeLive() + " pipeAlive=" + ps.pipeAlive + "<br />\r\n");
354-
}
328+
builder.append("Totoal pipe count: " + pipes.size() + "<br />\r\n");
329+
// buffer.append("Total pipe map count: " + pipeMap.size() + "<br />\r\n");
330+
int i = 0;
331+
for (Iterator<SimplePipeRunnable> itr = pipes.values().iterator(); itr.hasNext();) {
332+
SimplePipeRunnable p = (SimplePipeRunnable) itr.next();
333+
i++;
334+
List<SimpleSerializable> list = p.pipeData; //pipeMap.get(key);
335+
if (p instanceof CompoundPipeRunnable) {
336+
CompoundPipeRunnable cp = (CompoundPipeRunnable) p;
337+
builder.append(i + "Pipe " + cp.pipeKey + " status=" + cp.status + " pipeAlive=" + cp.isPipeLive() + " created=" + new Date(cp.lastSetup) + "<br />\r\n");
338+
for (int j = 0; j < cp.pipes.length; j++) {
339+
CompoundPipeSession ps = cp.pipes[j];
340+
if (ps != null) {
341+
builder.append(j + " : " + ps.session + " / " + ps.isPipeLive() + " pipeAlive=" + ps.pipeAlive + "<br />\r\n");
355342
}
356343
}
357-
if (list != null) {
358-
int size = list.size();
359-
//if (size > 5) {
360-
if (p != null) {
361-
builder.append("::: pipe " + p.pipeKey + " size : " + size + " / " + p.pipeAlive + "<br />\r\n");
362-
} else {
363-
builder.append("Error pipe " + key + " with size : " + size + "<br />\r\n");
364-
}
365-
//}
366-
}
344+
}
345+
if (list != null) {
346+
int size = list.size();
347+
builder.append("::: pipe " + p.pipeKey + " size : " + size + " / " + p.pipeAlive + "<br />\r\n");
367348
}
368349
}
369350
return builder.toString();
@@ -386,10 +367,6 @@ private static void monitoringAllPipes() {
386367
Thread.sleep(monitoringInterval);
387368
} catch (InterruptedException e) {
388369
}
389-
if (pipes == null) {
390-
System.err.println("Pipe sessions are null or empty! Managed pipe session monitor exited!");
391-
break;
392-
}
393370
Object[] allPipes = pipes.values().toArray();
394371
for (int i = 0; i < allPipes.length; i++) {
395372
final SimplePipeRunnable pipe = (SimplePipeRunnable) allPipes[i];
@@ -430,12 +407,7 @@ private static void monitoringAllPipes() {
430407
e.printStackTrace();
431408
}
432409
}
433-
if (pipes == null/* || pipes.isEmpty()*/) {
434-
monitored = false;
435-
break;
436-
}
437410
}
438-
System.err.println("Pipe sessions are null or empty! Pipe session monitor exited!");
439411
}
440412

441413
@J2SIgnore

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@ public interface ISimpleCometable {
3737
* call-back must be called when its job is done. Otherwise, this
3838
* connection will be kept until server restarts.
3939
*/
40-
public boolean cometRun(Runnable asyncDoneCallback);
40+
public boolean cometRun(final Runnable asyncDoneCallback);
4141

4242
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4385,6 +4385,17 @@ public static SimpleSerializable parseInstance(Map<String, Object> properties) {
43854385
if (clazzName == null) {
43864386
return null;
43874387
}
4388+
String longClazzName = classAliasMappings.get(clazzName);
4389+
if (longClazzName != null) {
4390+
clazzName = longClazzName;
4391+
}
4392+
SimpleFactory fb = fallbackFactory;
4393+
if (fb != null && classMissed.contains(clazzName)) {
4394+
SimpleSerializable ssInst = fb.createInstance();
4395+
if (ssInst != null) {
4396+
return ssInst;
4397+
}
4398+
}
43884399
Object inst = SimpleClassLoader.loadSimpleInstance(clazzName);
43894400
if (inst != null && inst instanceof SimpleSerializable) {
43904401
return (SimpleSerializable) inst;
@@ -4395,6 +4406,10 @@ public static SimpleSerializable parseInstance(Map<String, Object> properties) {
43954406
@J2SIgnore
43964407
public void deserialize(Map<String, Object> properties) {
43974408
String clazzName = (String) properties.get("class");
4409+
String longClazzName = classAliasMappings.get(clazzName);
4410+
if (longClazzName != null) {
4411+
clazzName = longClazzName;
4412+
}
43984413
Map<String, Field> fieldMap = getSerializableFields(clazzName, this.getClass());
43994414
String[] fMap = fieldMapping();
44004415
for (Iterator<String> itr = properties.keySet().iterator(); itr.hasNext();) {

0 commit comments

Comments
 (0)