瀏覽代碼

优化代码

zhouhao 8 年之前
父節點
當前提交
4d0fd69407

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

@@ -184,7 +184,7 @@ public class CacheMonitorController {
     protected long getTimes(TimesGetter getter) {
         long times = cacheManagerMap.values().stream()
                 .mapToLong(cacheManager -> getTimes(cacheManager, getter))
-                .reduce((i1, i2) -> i1 + i2).orElseGet(() -> 0);
+                .reduce(Math::addExact).orElse(0);
         return times;
     }
 
@@ -194,7 +194,7 @@ public class CacheMonitorController {
                 .filter(cache -> cache instanceof MonitorCache)
                 .map(cache -> (MonitorCache) cache)
                 .mapToLong(getter::get)
-                .reduce((i1, i2) -> i1 + i2).orElseGet(() -> 0);
+                .reduce(Math::addExact).orElse(0);
         return times;
     }
 

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

@@ -54,6 +54,7 @@ public class DatasourceTests extends AbstractTestCase {
     @Before
     public void setup() throws Exception {
         dataSourceService.delete("test");
+        //创建一个新的数据源配置,放入到数据库中
         DataSource dataSource = new DataSource();
         dataSource.setId("test");
         dataSource.setName("test");
@@ -65,11 +66,14 @@ public class DatasourceTests extends AbstractTestCase {
 
         dataSourceService.insert(dataSource);
 
+        //使用刚刚创建的数据源
         DynamicDataSource.use("test");
-        install.install();//安装新的数据库
+        install.install();//自动创建系统所需的数据库表
 
+        //使用默认数据源进行查询
         DynamicDataSource.useDefault();
         userService.select(QueryParam.build());
+        //使用test数据源进行查询
         DynamicDataSource.use("test");
         userService.select(QueryParam.build());
 

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

@@ -16,11 +16,22 @@
 
 package org.hsweb.web.service.impl.lock;
 
+import org.hsweb.concureent.cache.RedisCacheManagerAutoConfig;
 import org.hsweb.concurrent.lock.LockFactory;
+import org.hsweb.web.bean.po.user.User;
 import org.hsweb.web.service.impl.AbstractTestCase;
 import org.junit.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.dao.DataAccessException;
+import org.springframework.data.redis.connection.RedisConnection;
+import org.springframework.data.redis.core.RedisCallback;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
 
 import javax.annotation.Resource;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectOutputStream;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReadWriteLock;
@@ -30,6 +41,28 @@ public class LockTest extends AbstractTestCase {
     @Resource
     private LockFactory lockFactory;
 
+    @Autowired
+    private RedisTemplate redisTemplate;
+
+    @Test
+    public void testCache() {
+        redisTemplate.execute((RedisCallback<? extends Object>) connection -> {
+            for (int i = 0; i < 50000; i++) {
+                User user = new User();
+                user.setName("test" + i);
+                try {
+                    JdkSerializationRedisSerializer s = new JdkSerializationRedisSerializer();
+                    connection.lPush("test".getBytes(), s.serialize(user));
+                } catch (Exception e) {
+                    e.printStackTrace();
+                }
+            }
+            return null;
+        });
+        long l = System.currentTimeMillis();
+        System.out.println(redisTemplate.opsForList().range("test", 0, -1).size());
+        System.out.println(System.currentTimeMillis() - l);
+    }
 
     @Test
     public void testLock() throws InterruptedException {

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

@@ -20,12 +20,14 @@ import org.hsweb.web.bean.po.quartz.QuartzJob;
 import org.hsweb.web.service.impl.AbstractTestCase;
 import org.hsweb.web.service.quartz.QuartzJobService;
 import org.junit.Test;
+import org.springframework.stereotype.Component;
 
 import javax.annotation.Resource;
 
 /**
  * @author zhouhao
  */
+@Component
 public class QuartzJobServiceImplTest extends AbstractTestCase {
 
     @Resource

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

@@ -55,4 +55,4 @@ mybatis:
     mapper-locations: classpath*:org/hsweb/web/dao/impl/mybatis/mapper/oracle/**/*.xml
     config: classpath:mybatis-config.xml
     typeHandlers-package: org.hsweb.web.mybatis.handler
-#    dynamic-datasource: on
+   # dynamic-datasource: on

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

@@ -3,7 +3,6 @@ package org.hsweb.web.socket.cmd.support;
 import org.hsweb.web.bean.po.user.User;
 import org.hsweb.web.socket.cmd.CMD;
 import org.hsweb.web.socket.message.WebSocketMessage;
-import org.hsweb.web.socket.message.WebSocketMessageManager;
 import org.hyperic.sigar.CpuInfo;
 import org.hyperic.sigar.CpuPerc;
 import org.hyperic.sigar.Sigar;
@@ -12,6 +11,7 @@ import org.springframework.web.socket.WebSocketSession;
 import java.io.IOException;
 import java.util.*;
 import java.util.concurrent.*;
+import java.util.function.Supplier;
 
 /**
  * Created by zhouhao on 16-5-29.
@@ -20,7 +20,7 @@ public class SystemMonitorProcessor extends AbstractCmdProcessor {
 
 
     private Sigar sigar;
-    private ExecutorService exec = Executors.newCachedThreadPool();
+    private ExecutorService      exec       = Executors.newCachedThreadPool();
     private Map<String, Publish> cpuPublish = new ConcurrentHashMap<>();
     private Map<String, Publish> memPublish = new ConcurrentHashMap<>();
 
@@ -40,50 +40,51 @@ public class SystemMonitorProcessor extends AbstractCmdProcessor {
         String type = ((String) cmd.getParams().get("type"));
         if (type == null) return;
         String userId = getUser(cmd).getId();
+
+        Supplier<Publish> supplier = () -> {
+            Publish publish = new Publish();
+            publish.setUserId(userId);
+            publish.setCallback((String) cmd.getParams().get("callback"));
+            cpuPublish.put(userId, publish);
+            return publish;
+        };
+
         switch (type) {
             case "cpu":
                 Publish publish = cpuPublish.get(userId);
-                if (publish == null) {
-                    publish = new Publish();
-                    publish.setUserId(userId);
-                    publish.setCallback((String) cmd.getParams().get("callback"));
-                    cpuPublish.put(userId, publish);
-                }
+                if (publish == null)
+                    publish = supplier.get();
                 publish.addSession(cmd.getSession());
                 if (!cpuMonitorIsStarted) {
                     startPublishCpu();
                     cpuMonitorIsStarted = true;
                 }
-                webSocketMessageManager.subscribe(getName()+"-cpu", userId, cmd.getSession());
+                webSocketMessageManager.subscribe(getName() + "-cpu", userId, cmd.getSession());
                 break;
             case "mem":
                 publish = memPublish.get(userId);
-                if (publish == null) {
-                    publish = new Publish();
-                    publish.setUserId(userId);
-                    publish.setCallback((String) cmd.getParams().get("callback"));
-                    memPublish.put(userId, publish);
-                }
+                if (publish == null)
+                    publish = supplier.get();
                 publish.addSession(cmd.getSession());
                 if (!memMonitorIsStarted) {
                     startPublishMem();
                     memMonitorIsStarted = true;
                 }
-                webSocketMessageManager.subscribe(getName()+"-mem", userId, cmd.getSession());
+                webSocketMessageManager.subscribe(getName() + "-mem", userId, cmd.getSession());
                 break;
             case "mem-cancel":
                 cancelPublish(memPublish, userId, cmd.getSession());
-                webSocketMessageManager.deSubscribe(getName()+"-mem", userId, cmd.getSession());
+                webSocketMessageManager.deSubscribe(getName() + "-mem", userId, cmd.getSession());
                 break;
             case "cpu-cancel":
                 cancelPublish(cpuPublish, userId, cmd.getSession());
-                webSocketMessageManager.deSubscribe(getName()+"-cpu", userId, cmd.getSession());
+                webSocketMessageManager.deSubscribe(getName() + "-cpu", userId, cmd.getSession());
                 break;
             case "cancel":
                 cancelPublish(memPublish, userId, cmd.getSession());
                 cancelPublish(cpuPublish, userId, cmd.getSession());
-                webSocketMessageManager.deSubscribe(getName()+"-mem", userId, cmd.getSession());
-                webSocketMessageManager.deSubscribe(getName()+"-cpu", userId, cmd.getSession());
+                webSocketMessageManager.deSubscribe(getName() + "-mem", userId, cmd.getSession());
+                webSocketMessageManager.deSubscribe(getName() + "-cpu", userId, cmd.getSession());
                 break;
         }
     }
@@ -158,7 +159,7 @@ public class SystemMonitorProcessor extends AbstractCmdProcessor {
                         WebSocketMessage msg = new WebSocketMessage();
                         msg.setTo(publish.getUserId());
                         msg.setContent(infoList);
-                        msg.setType(getName()+"-cpu");
+                        msg.setType(getName() + "-cpu");
                         msg.setCallBack(publish.getCallback());
                         msg.setFrom("system");
                         try {
@@ -187,15 +188,15 @@ public class SystemMonitorProcessor extends AbstractCmdProcessor {
                         }
                     }
                     Map<String, Object> map = sigar.getMem().toMap();
-                    Runtime runtime=  Runtime.getRuntime();
+                    Runtime runtime = Runtime.getRuntime();
                     map.put("jvmTotal", runtime.totalMemory());
-                    map.put("jvmMax",runtime.maxMemory());
-                    map.put("jvmFree",runtime.freeMemory());
+                    map.put("jvmMax", runtime.maxMemory());
+                    map.put("jvmFree", runtime.freeMemory());
                     memPublish.values().forEach(publish -> {
                         WebSocketMessage msg = new WebSocketMessage();
                         msg.setTo(publish.getUserId());
                         msg.setContent(map);
-                        msg.setType(getName()+"-mem");
+                        msg.setType(getName() + "-mem");
                         msg.setCallBack(publish.getCallback());
                         msg.setFrom("system");
                         try {
@@ -227,8 +228,8 @@ public class SystemMonitorProcessor extends AbstractCmdProcessor {
         if (user != null) {
             cancelPublish(cpuPublish, user.getId(), session);
             cancelPublish(memPublish, user.getId(), session);
-            webSocketMessageManager.deSubscribe(getName()+"-cpu", user.getId(),session);
-            webSocketMessageManager.deSubscribe(getName()+"-mem", user.getId(),session);
+            webSocketMessageManager.deSubscribe(getName() + "-cpu", user.getId(), session);
+            webSocketMessageManager.deSubscribe(getName() + "-mem", user.getId(), session);
         }
 
     }