Skip to content

Commit 4d0fd69

Browse files
committed
优化代码
1 parent 3fbef7c commit 4d0fd69

File tree

6 files changed

+71
-31
lines changed

6 files changed

+71
-31
lines changed

hsweb-web-controller/src/main/java/org/hsweb/web/controller/monitor/CacheMonitorController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public ResponseMessage getSize() {
184184
protected long getTimes(TimesGetter getter) {
185185
long times = cacheManagerMap.values().stream()
186186
.mapToLong(cacheManager -> getTimes(cacheManager, getter))
187-
.reduce((i1, i2) -> i1 + i2).orElseGet(() -> 0);
187+
.reduce(Math::addExact).orElse(0);
188188
return times;
189189
}
190190

@@ -194,7 +194,7 @@ protected long getTimes(CacheManager cacheManager, TimesGetter getter) {
194194
.filter(cache -> cache instanceof MonitorCache)
195195
.map(cache -> (MonitorCache) cache)
196196
.mapToLong(getter::get)
197-
.reduce((i1, i2) -> i1 + i2).orElseGet(() -> 0);
197+
.reduce(Math::addExact).orElse(0);
198198
return times;
199199
}
200200

hsweb-web-service/hsweb-web-service-simple/src/test/java/org/hsweb/web/service/impl/datasource/DatasourceTests.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public void init() {
5454
@Before
5555
public void setup() throws Exception {
5656
dataSourceService.delete("test");
57+
//创建一个新的数据源配置,放入到数据库中
5758
DataSource dataSource = new DataSource();
5859
dataSource.setId("test");
5960
dataSource.setName("test");
@@ -65,11 +66,14 @@ public void setup() throws Exception {
6566

6667
dataSourceService.insert(dataSource);
6768

69+
//使用刚刚创建的数据源
6870
DynamicDataSource.use("test");
69-
install.install();//安装新的数据库
71+
install.install();//自动创建系统所需的数据库表
7072

73+
//使用默认数据源进行查询
7174
DynamicDataSource.useDefault();
7275
userService.select(QueryParam.build());
76+
//使用test数据源进行查询
7377
DynamicDataSource.use("test");
7478
userService.select(QueryParam.build());
7579

hsweb-web-service/hsweb-web-service-simple/src/test/java/org/hsweb/web/service/impl/lock/LockTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,22 @@
1616

1717
package org.hsweb.web.service.impl.lock;
1818

19+
import org.hsweb.concureent.cache.RedisCacheManagerAutoConfig;
1920
import org.hsweb.concurrent.lock.LockFactory;
21+
import org.hsweb.web.bean.po.user.User;
2022
import org.hsweb.web.service.impl.AbstractTestCase;
2123
import org.junit.Test;
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.dao.DataAccessException;
26+
import org.springframework.data.redis.connection.RedisConnection;
27+
import org.springframework.data.redis.core.RedisCallback;
28+
import org.springframework.data.redis.core.RedisTemplate;
29+
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
2230

2331
import javax.annotation.Resource;
32+
import java.io.ByteArrayOutputStream;
33+
import java.io.IOException;
34+
import java.io.ObjectOutputStream;
2435
import java.util.concurrent.TimeUnit;
2536
import java.util.concurrent.locks.Lock;
2637
import java.util.concurrent.locks.ReadWriteLock;
@@ -30,6 +41,28 @@ public class LockTest extends AbstractTestCase {
3041
@Resource
3142
private LockFactory lockFactory;
3243

44+
@Autowired
45+
private RedisTemplate redisTemplate;
46+
47+
@Test
48+
public void testCache() {
49+
redisTemplate.execute((RedisCallback<? extends Object>) connection -> {
50+
for (int i = 0; i < 50000; i++) {
51+
User user = new User();
52+
user.setName("test" + i);
53+
try {
54+
JdkSerializationRedisSerializer s = new JdkSerializationRedisSerializer();
55+
connection.lPush("test".getBytes(), s.serialize(user));
56+
} catch (Exception e) {
57+
e.printStackTrace();
58+
}
59+
}
60+
return null;
61+
});
62+
long l = System.currentTimeMillis();
63+
System.out.println(redisTemplate.opsForList().range("test", 0, -1).size());
64+
System.out.println(System.currentTimeMillis() - l);
65+
}
3366

3467
@Test
3568
public void testLock() throws InterruptedException {

hsweb-web-service/hsweb-web-service-simple/src/test/java/org/hsweb/web/service/impl/quartz/QuartzJobServiceImplTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@
2020
import org.hsweb.web.service.impl.AbstractTestCase;
2121
import org.hsweb.web.service.quartz.QuartzJobService;
2222
import org.junit.Test;
23+
import org.springframework.stereotype.Component;
2324

2425
import javax.annotation.Resource;
2526

2627
/**
2728
* @author zhouhao
2829
*/
30+
@Component
2931
public class QuartzJobServiceImplTest extends AbstractTestCase {
3032

3133
@Resource

hsweb-web-service/hsweb-web-service-simple/src/test/resources/application.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,4 @@ mybatis:
5555
mapper-locations: classpath*:org/hsweb/web/dao/impl/mybatis/mapper/oracle/**/*.xml
5656
config: classpath:mybatis-config.xml
5757
typeHandlers-package: org.hsweb.web.mybatis.handler
58-
# dynamic-datasource: on
58+
# dynamic-datasource: on

hsweb-web-websocket/src/main/java/org/hsweb/web/socket/cmd/support/SystemMonitorProcessor.java

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import org.hsweb.web.bean.po.user.User;
44
import org.hsweb.web.socket.cmd.CMD;
55
import org.hsweb.web.socket.message.WebSocketMessage;
6-
import org.hsweb.web.socket.message.WebSocketMessageManager;
76
import org.hyperic.sigar.CpuInfo;
87
import org.hyperic.sigar.CpuPerc;
98
import org.hyperic.sigar.Sigar;
@@ -12,6 +11,7 @@
1211
import java.io.IOException;
1312
import java.util.*;
1413
import java.util.concurrent.*;
14+
import java.util.function.Supplier;
1515

1616
/**
1717
* Created by zhouhao on 16-5-29.
@@ -20,7 +20,7 @@ public class SystemMonitorProcessor extends AbstractCmdProcessor {
2020

2121

2222
private Sigar sigar;
23-
private ExecutorService exec = Executors.newCachedThreadPool();
23+
private ExecutorService exec = Executors.newCachedThreadPool();
2424
private Map<String, Publish> cpuPublish = new ConcurrentHashMap<>();
2525
private Map<String, Publish> memPublish = new ConcurrentHashMap<>();
2626

@@ -40,50 +40,51 @@ public void exec(CMD cmd) throws Exception {
4040
String type = ((String) cmd.getParams().get("type"));
4141
if (type == null) return;
4242
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+
4352
switch (type) {
4453
case "cpu":
4554
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();
5257
publish.addSession(cmd.getSession());
5358
if (!cpuMonitorIsStarted) {
5459
startPublishCpu();
5560
cpuMonitorIsStarted = true;
5661
}
57-
webSocketMessageManager.subscribe(getName()+"-cpu", userId, cmd.getSession());
62+
webSocketMessageManager.subscribe(getName() + "-cpu", userId, cmd.getSession());
5863
break;
5964
case "mem":
6065
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();
6768
publish.addSession(cmd.getSession());
6869
if (!memMonitorIsStarted) {
6970
startPublishMem();
7071
memMonitorIsStarted = true;
7172
}
72-
webSocketMessageManager.subscribe(getName()+"-mem", userId, cmd.getSession());
73+
webSocketMessageManager.subscribe(getName() + "-mem", userId, cmd.getSession());
7374
break;
7475
case "mem-cancel":
7576
cancelPublish(memPublish, userId, cmd.getSession());
76-
webSocketMessageManager.deSubscribe(getName()+"-mem", userId, cmd.getSession());
77+
webSocketMessageManager.deSubscribe(getName() + "-mem", userId, cmd.getSession());
7778
break;
7879
case "cpu-cancel":
7980
cancelPublish(cpuPublish, userId, cmd.getSession());
80-
webSocketMessageManager.deSubscribe(getName()+"-cpu", userId, cmd.getSession());
81+
webSocketMessageManager.deSubscribe(getName() + "-cpu", userId, cmd.getSession());
8182
break;
8283
case "cancel":
8384
cancelPublish(memPublish, userId, cmd.getSession());
8485
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());
8788
break;
8889
}
8990
}
@@ -158,7 +159,7 @@ public Future startPublishCpu() throws Exception {
158159
WebSocketMessage msg = new WebSocketMessage();
159160
msg.setTo(publish.getUserId());
160161
msg.setContent(infoList);
161-
msg.setType(getName()+"-cpu");
162+
msg.setType(getName() + "-cpu");
162163
msg.setCallBack(publish.getCallback());
163164
msg.setFrom("system");
164165
try {
@@ -187,15 +188,15 @@ public Future startPublishMem() throws Exception {
187188
}
188189
}
189190
Map<String, Object> map = sigar.getMem().toMap();
190-
Runtime runtime= Runtime.getRuntime();
191+
Runtime runtime = Runtime.getRuntime();
191192
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());
194195
memPublish.values().forEach(publish -> {
195196
WebSocketMessage msg = new WebSocketMessage();
196197
msg.setTo(publish.getUserId());
197198
msg.setContent(map);
198-
msg.setType(getName()+"-mem");
199+
msg.setType(getName() + "-mem");
199200
msg.setCallBack(publish.getCallback());
200201
msg.setFrom("system");
201202
try {
@@ -227,8 +228,8 @@ public void onSessionClose(WebSocketSession session) throws Exception {
227228
if (user != null) {
228229
cancelPublish(cpuPublish, user.getId(), session);
229230
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);
232233
}
233234

234235
}

0 commit comments

Comments
 (0)