Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit11d2203

Browse files
author
zhourenjian@gmail.com
committed
Use ConcurrentHashMap instead of synchronized HashMap, better performance
Support abbreviation of class name in simple json request
1 parent9e24c73 commit11d2203

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
importjava.util.Collections;
1414
importjava.util.Comparator;
1515
importjava.util.Date;
16-
importjava.util.HashMap;
16+
importjava.util.Iterator;
1717
importjava.util.List;
1818
importjava.util.Map;
1919
importjava.util.Vector;
2020
importjava.util.concurrent.BlockingQueue;
21+
importjava.util.concurrent.ConcurrentHashMap;
2122
importjava.util.concurrent.LinkedBlockingQueue;
2223
importjava.util.concurrent.TimeUnit;
2324

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

54-
staticMap<String,SimplePipeRunnable>pipes;
55+
// allPipes is for JavaScript, as pipes is for Java
56+
staticObjectallPipes =null;
57+
58+
@J2SIgnore
59+
privatestaticMap<String,SimplePipeRunnable>pipes =newConcurrentHashMap<String,SimplePipeRunnable>(50);
5560

5661
@J2SIgnore
5762
privatestaticBlockingQueue<SimplePipeRunnable>toBeDestroyedPipes =newLinkedBlockingQueue<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
publicstaticvoidregisterPipe(Stringkey,SimplePipeRunnablepipe) {
7580
if (key ==null ||pipe ==null)return;
76-
if (pipes ==null) {
77-
pipes =Collections.synchronizedMap(newHashMap<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(newHashMap<String,SimplePipeRunnable>(50));
93-
}
94-
9593
Stringkey =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(newHashMap<String, List<SimpleSerializable>>());
100+
//pipeMap = newConcurrentHashMap<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
publicstaticvoidremovePipe(Stringkey) {
139137
if (key ==null) {
140138
System.out.println("Removing pipe for null key???");
141139
newRuntimeException("Removing null pipe key").printStackTrace();
142140
return;
143141
}
144-
SimplePipeRunnablepipe =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+
SimplePipeRunnablepipe =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
publicstaticSimplePipeRunnablegetPipe(Stringkey) {
170-
if (pipes ==null ||key ==null)returnnull;
165+
if (key ==null)returnnull;
171166
returnpipes.get(key);
172167
}
173168

@@ -297,35 +292,29 @@ static void helpClosing(SimplePipeRunnable pipe) {
297292
publicstaticStringprintStatistics2() {
298293
StringBuilderbuilder =newStringBuilder();
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 (inti =0;i <keys.length;i++) {
305-
Stringkey = (String)keys[i];
306-
SimplePipeRunnablep =pipes.get(key);
307-
List<SimpleSerializable>list =p !=null ?p.pipeData :null;//pipeMap.get(key);
308-
if (pinstanceofCompoundPipeRunnable) {
309-
CompoundPipeRunnablecp = (CompoundPipeRunnable)p;
310-
intactiveCount =0;
311-
for (intj =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=" +newDate(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+
inti =0;
298+
for (Iterator<SimplePipeRunnable>itr =pipes.values().iterator();itr.hasNext();) {
299+
SimplePipeRunnablep = (SimplePipeRunnable)itr.next();
300+
i++;
301+
List<SimpleSerializable>list =p.pipeData;//pipeMap.get(key);
302+
if (pinstanceofCompoundPipeRunnable) {
303+
CompoundPipeRunnablecp = (CompoundPipeRunnable)p;
304+
intactiveCount =0;
305+
for (intj =0;j <cp.pipes.length;j++) {
306+
if (cp.pipes[j] !=null) {
307+
activeCount++;
318308
}
319309
}
320-
if (list !=null) {
321-
intsize =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=" +newDate(cp.lastSetup) +"<br />\r\n");
312+
}
313+
}
314+
if (list !=null) {
315+
intsize =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
publicstaticStringprintStatistics() {
337326
StringBuilderbuilder =newStringBuilder();
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 (inti =0;i <keys.length;i++) {
344-
Stringkey = (String)keys[i];
345-
SimplePipeRunnablep =pipes.get(key);
346-
List<SimpleSerializable>list =p !=null ?p.pipeData :null;//pipeMap.get(key);
347-
if (pinstanceofCompoundPipeRunnable) {
348-
CompoundPipeRunnablecp = (CompoundPipeRunnable)p;
349-
builder.append(i +"Pipe " +cp.pipeKey +" status=" +cp.status +" pipeAlive=" +cp.isPipeLive() +" created=" +newDate(cp.lastSetup) +"<br />\r\n");
350-
for (intj =0;j <cp.pipes.length;j++) {
351-
CompoundPipeSessionps =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+
inti =0;
331+
for (Iterator<SimplePipeRunnable>itr =pipes.values().iterator();itr.hasNext();) {
332+
SimplePipeRunnablep = (SimplePipeRunnable)itr.next();
333+
i++;
334+
List<SimpleSerializable>list =p.pipeData;//pipeMap.get(key);
335+
if (pinstanceofCompoundPipeRunnable) {
336+
CompoundPipeRunnablecp = (CompoundPipeRunnable)p;
337+
builder.append(i +"Pipe " +cp.pipeKey +" status=" +cp.status +" pipeAlive=" +cp.isPipeLive() +" created=" +newDate(cp.lastSetup) +"<br />\r\n");
338+
for (intj =0;j <cp.pipes.length;j++) {
339+
CompoundPipeSessionps =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-
intsize =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+
intsize =list.size();
347+
builder.append("::: pipe " +p.pipeKey +" size : " +size +" / " +p.pipeAlive +"<br />\r\n");
367348
}
368349
}
369350
returnbuilder.toString();
@@ -386,10 +367,6 @@ private static void monitoringAllPipes() {
386367
Thread.sleep(monitoringInterval);
387368
}catch (InterruptedExceptione) {
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 (inti =0;i <allPipes.length;i++) {
395372
finalSimplePipeRunnablepipe = (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-
publicbooleancometRun(RunnableasyncDoneCallback);
40+
publicbooleancometRun(finalRunnableasyncDoneCallback);
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
returnnull;
43874387
}
4388+
StringlongClazzName =classAliasMappings.get(clazzName);
4389+
if (longClazzName !=null) {
4390+
clazzName =longClazzName;
4391+
}
4392+
SimpleFactoryfb =fallbackFactory;
4393+
if (fb !=null &&classMissed.contains(clazzName)) {
4394+
SimpleSerializablessInst =fb.createInstance();
4395+
if (ssInst !=null) {
4396+
returnssInst;
4397+
}
4398+
}
43884399
Objectinst =SimpleClassLoader.loadSimpleInstance(clazzName);
43894400
if (inst !=null &&instinstanceofSimpleSerializable) {
43904401
return (SimpleSerializable)inst;
@@ -4395,6 +4406,10 @@ public static SimpleSerializable parseInstance(Map<String, Object> properties) {
43954406
@J2SIgnore
43964407
publicvoiddeserialize(Map<String,Object>properties) {
43974408
StringclazzName = (String)properties.get("class");
4409+
StringlongClazzName =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)

[8]ページ先頭

©2009-2025 Movatter.jp