3
3
import org .hsweb .web .bean .po .user .User ;
4
4
import org .hsweb .web .socket .cmd .CMD ;
5
5
import org .hsweb .web .socket .message .WebSocketMessage ;
6
- import org .hsweb .web .socket .message .WebSocketMessageManager ;
7
6
import org .hyperic .sigar .CpuInfo ;
8
7
import org .hyperic .sigar .CpuPerc ;
9
8
import org .hyperic .sigar .Sigar ;
12
11
import java .io .IOException ;
13
12
import java .util .*;
14
13
import java .util .concurrent .*;
14
+ import java .util .function .Supplier ;
15
15
16
16
/**
17
17
* Created by zhouhao on 16-5-29.
@@ -20,7 +20,7 @@ public class SystemMonitorProcessor extends AbstractCmdProcessor {
20
20
21
21
22
22
private Sigar sigar ;
23
- private ExecutorService exec = Executors .newCachedThreadPool ();
23
+ private ExecutorService exec = Executors .newCachedThreadPool ();
24
24
private Map <String , Publish > cpuPublish = new ConcurrentHashMap <>();
25
25
private Map <String , Publish > memPublish = new ConcurrentHashMap <>();
26
26
@@ -40,50 +40,51 @@ public void exec(CMD cmd) throws Exception {
40
40
String type = ((String ) cmd .getParams ().get ("type" ));
41
41
if (type == null ) return ;
42
42
String userId = getUser (cmd ).getId ();
43
+
44
+ Supplier <Publish > supplier = () -> {
45
+ Publish publish = new Publish ();
46
+ publish .setUserId (userId );
47
+ publish .setCallback ((String ) cmd .getParams ().get ("callback" ));
48
+ cpuPublish .put (userId , publish );
49
+ return publish ;
50
+ };
51
+
43
52
switch (type ) {
44
53
case "cpu" :
45
54
Publish publish = cpuPublish .get (userId );
46
- if (publish == null ) {
47
- publish = new Publish ();
48
- publish .setUserId (userId );
49
- publish .setCallback ((String ) cmd .getParams ().get ("callback" ));
50
- cpuPublish .put (userId , publish );
51
- }
55
+ if (publish == null )
56
+ publish = supplier .get ();
52
57
publish .addSession (cmd .getSession ());
53
58
if (!cpuMonitorIsStarted ) {
54
59
startPublishCpu ();
55
60
cpuMonitorIsStarted = true ;
56
61
}
57
- webSocketMessageManager .subscribe (getName ()+ "-cpu" , userId , cmd .getSession ());
62
+ webSocketMessageManager .subscribe (getName () + "-cpu" , userId , cmd .getSession ());
58
63
break ;
59
64
case "mem" :
60
65
publish = memPublish .get (userId );
61
- if (publish == null ) {
62
- publish = new Publish ();
63
- publish .setUserId (userId );
64
- publish .setCallback ((String ) cmd .getParams ().get ("callback" ));
65
- memPublish .put (userId , publish );
66
- }
66
+ if (publish == null )
67
+ publish = supplier .get ();
67
68
publish .addSession (cmd .getSession ());
68
69
if (!memMonitorIsStarted ) {
69
70
startPublishMem ();
70
71
memMonitorIsStarted = true ;
71
72
}
72
- webSocketMessageManager .subscribe (getName ()+ "-mem" , userId , cmd .getSession ());
73
+ webSocketMessageManager .subscribe (getName () + "-mem" , userId , cmd .getSession ());
73
74
break ;
74
75
case "mem-cancel" :
75
76
cancelPublish (memPublish , userId , cmd .getSession ());
76
- webSocketMessageManager .deSubscribe (getName ()+ "-mem" , userId , cmd .getSession ());
77
+ webSocketMessageManager .deSubscribe (getName () + "-mem" , userId , cmd .getSession ());
77
78
break ;
78
79
case "cpu-cancel" :
79
80
cancelPublish (cpuPublish , userId , cmd .getSession ());
80
- webSocketMessageManager .deSubscribe (getName ()+ "-cpu" , userId , cmd .getSession ());
81
+ webSocketMessageManager .deSubscribe (getName () + "-cpu" , userId , cmd .getSession ());
81
82
break ;
82
83
case "cancel" :
83
84
cancelPublish (memPublish , userId , cmd .getSession ());
84
85
cancelPublish (cpuPublish , userId , cmd .getSession ());
85
- webSocketMessageManager .deSubscribe (getName ()+ "-mem" , userId , cmd .getSession ());
86
- webSocketMessageManager .deSubscribe (getName ()+ "-cpu" , userId , cmd .getSession ());
86
+ webSocketMessageManager .deSubscribe (getName () + "-mem" , userId , cmd .getSession ());
87
+ webSocketMessageManager .deSubscribe (getName () + "-cpu" , userId , cmd .getSession ());
87
88
break ;
88
89
}
89
90
}
@@ -158,7 +159,7 @@ public Future startPublishCpu() throws Exception {
158
159
WebSocketMessage msg = new WebSocketMessage ();
159
160
msg .setTo (publish .getUserId ());
160
161
msg .setContent (infoList );
161
- msg .setType (getName ()+ "-cpu" );
162
+ msg .setType (getName () + "-cpu" );
162
163
msg .setCallBack (publish .getCallback ());
163
164
msg .setFrom ("system" );
164
165
try {
@@ -187,15 +188,15 @@ public Future startPublishMem() throws Exception {
187
188
}
188
189
}
189
190
Map <String , Object > map = sigar .getMem ().toMap ();
190
- Runtime runtime = Runtime .getRuntime ();
191
+ Runtime runtime = Runtime .getRuntime ();
191
192
map .put ("jvmTotal" , runtime .totalMemory ());
192
- map .put ("jvmMax" ,runtime .maxMemory ());
193
- map .put ("jvmFree" ,runtime .freeMemory ());
193
+ map .put ("jvmMax" , runtime .maxMemory ());
194
+ map .put ("jvmFree" , runtime .freeMemory ());
194
195
memPublish .values ().forEach (publish -> {
195
196
WebSocketMessage msg = new WebSocketMessage ();
196
197
msg .setTo (publish .getUserId ());
197
198
msg .setContent (map );
198
- msg .setType (getName ()+ "-mem" );
199
+ msg .setType (getName () + "-mem" );
199
200
msg .setCallBack (publish .getCallback ());
200
201
msg .setFrom ("system" );
201
202
try {
@@ -227,8 +228,8 @@ public void onSessionClose(WebSocketSession session) throws Exception {
227
228
if (user != null ) {
228
229
cancelPublish (cpuPublish , user .getId (), session );
229
230
cancelPublish (memPublish , user .getId (), session );
230
- webSocketMessageManager .deSubscribe (getName ()+ "-cpu" , user .getId (),session );
231
- webSocketMessageManager .deSubscribe (getName ()+ "-mem" , user .getId (),session );
231
+ webSocketMessageManager .deSubscribe (getName () + "-cpu" , user .getId (), session );
232
+ webSocketMessageManager .deSubscribe (getName () + "-mem" , user .getId (), session );
232
233
}
233
234
234
235
}
0 commit comments