作者 crossoverJie

:recycle: 重构代码

@@ -26,8 +26,6 @@ import org.springframework.beans.factory.annotation.Autowired; @@ -26,8 +26,6 @@ import org.springframework.beans.factory.annotation.Autowired;
26 import org.springframework.beans.factory.annotation.Value; 26 import org.springframework.beans.factory.annotation.Value;
27 import org.springframework.stereotype.Component; 27 import org.springframework.stereotype.Component;
28 28
29 -import javax.annotation.PostConstruct;  
30 -  
31 /** 29 /**
32 * Function: 30 * Function:
33 * 31 *
@@ -67,7 +65,7 @@ public class CIMClient { @@ -67,7 +65,7 @@ public class CIMClient {
67 */ 65 */
68 private int errorCount; 66 private int errorCount;
69 67
70 - @PostConstruct 68 + //@PostConstruct
71 public void start() throws Exception { 69 public void start() throws Exception {
72 70
73 //登录 + 获取可以使用的服务器 ip+port 71 //登录 + 获取可以使用的服务器 ip+port
  1 +package com.crossoverjie.cim.client.service;
  2 +
  3 +/**
  4 + * Function:
  5 + *
  6 + * @author crossoverJie
  7 + * Date: 2019-01-27 19:26
  8 + * @since JDK 1.8
  9 + */
  10 +public interface InnerCommand {
  11 +
  12 + /**
  13 + * 执行
  14 + * @param msg
  15 + */
  16 + void process(String msg) ;
  17 +}
@@ -3,6 +3,7 @@ package com.crossoverjie.cim.client.service.impl; @@ -3,6 +3,7 @@ package com.crossoverjie.cim.client.service.impl;
3 import com.alibaba.fastjson.JSON; 3 import com.alibaba.fastjson.JSON;
4 import com.crossoverjie.cim.client.client.CIMClient; 4 import com.crossoverjie.cim.client.client.CIMClient;
5 import com.crossoverjie.cim.client.config.AppConfiguration; 5 import com.crossoverjie.cim.client.config.AppConfiguration;
  6 +import com.crossoverjie.cim.client.service.InnerCommandContext;
6 import com.crossoverjie.cim.client.service.MsgHandle; 7 import com.crossoverjie.cim.client.service.MsgHandle;
7 import com.crossoverjie.cim.client.service.MsgLogger; 8 import com.crossoverjie.cim.client.service.MsgLogger;
8 import com.crossoverjie.cim.client.service.RouteRequest; 9 import com.crossoverjie.cim.client.service.RouteRequest;
@@ -49,7 +50,10 @@ public class MsgHandler implements MsgHandle { @@ -49,7 +50,10 @@ public class MsgHandler implements MsgHandle {
49 private MsgLogger msgLogger; 50 private MsgLogger msgLogger;
50 51
51 @Autowired 52 @Autowired
52 - private ClientInfo clientInfo ; 53 + private ClientInfo clientInfo;
  54 +
  55 + @Autowired
  56 + private InnerCommandContext innerCommandContext ;
53 57
54 private boolean aiModel = false; 58 private boolean aiModel = false;
55 59
@@ -134,6 +138,8 @@ public class MsgHandler implements MsgHandle { @@ -134,6 +138,8 @@ public class MsgHandler implements MsgHandle {
134 if (msg.startsWith(":")) { 138 if (msg.startsWith(":")) {
135 Map<String, String> allStatusCode = SystemCommandEnumType.getAllStatusCode(); 139 Map<String, String> allStatusCode = SystemCommandEnumType.getAllStatusCode();
136 140
  141 + innerCommandContext.execute(msg);
  142 +
137 if (SystemCommandEnumType.QUIT.getCommandType().trim().equals(msg)) { 143 if (SystemCommandEnumType.QUIT.getCommandType().trim().equals(msg)) {
138 //关闭系统 144 //关闭系统
139 shutdown(); 145 shutdown();
@@ -158,7 +164,7 @@ public class MsgHandler implements MsgHandle { @@ -158,7 +164,7 @@ public class MsgHandler implements MsgHandle {
158 } else if (msg.startsWith(SystemCommandEnumType.PREFIX.getCommandType().trim() + " ")) { 164 } else if (msg.startsWith(SystemCommandEnumType.PREFIX.getCommandType().trim() + " ")) {
159 //模糊匹配 165 //模糊匹配
160 prefixSearch(msg); 166 prefixSearch(msg);
161 - } else if (SystemCommandEnumType.INFO.getCommandType().trim().equals(msg.toLowerCase())) { 167 + } else if (SystemCommandEnumType.INFO.getCommandType().trim().equals(msg.toLowerCase())) {
162 LOGGER.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); 168 LOGGER.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
163 LOGGER.info("client info=[{}]", JSON.toJSONString(clientInfo.get())); 169 LOGGER.info("client info=[{}]", JSON.toJSONString(clientInfo.get()));
164 LOGGER.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); 170 LOGGER.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  1 +package com.crossoverjie.cim.client.service.impl.command;
  2 +
  3 +import com.crossoverjie.cim.client.client.CIMClient;
  4 +import com.crossoverjie.cim.client.service.InnerCommand;
  5 +import com.crossoverjie.cim.client.service.MsgLogger;
  6 +import com.crossoverjie.cim.client.service.RouteRequest;
  7 +import org.slf4j.Logger;
  8 +import org.slf4j.LoggerFactory;
  9 +import org.springframework.beans.factory.annotation.Autowired;
  10 +import org.springframework.stereotype.Service;
  11 +
  12 +import javax.annotation.Resource;
  13 +import java.util.concurrent.ThreadPoolExecutor;
  14 +import java.util.concurrent.TimeUnit;
  15 +
  16 +/**
  17 + * Function:
  18 + *
  19 + * @author crossoverJie
  20 + * Date: 2019-01-27 19:28
  21 + * @since JDK 1.8
  22 + */
  23 +@Service
  24 +public class ShutDownCommand implements InnerCommand {
  25 + private final static Logger LOGGER = LoggerFactory.getLogger(ShutDownCommand.class);
  26 +
  27 + @Autowired
  28 + private RouteRequest routeRequest ;
  29 +
  30 + @Autowired
  31 + private CIMClient cimClient;
  32 +
  33 + @Autowired
  34 + private MsgLogger msgLogger;
  35 +
  36 + @Resource(name = "callBackThreadPool")
  37 + private ThreadPoolExecutor executor;
  38 +
  39 + @Override
  40 + public void process(String msg) {
  41 + LOGGER.info("系统关闭中。。。。");
  42 + routeRequest.offLine();
  43 + msgLogger.stop();
  44 + executor.shutdown();
  45 + try {
  46 + while (!executor.awaitTermination(1, TimeUnit.SECONDS)) {
  47 + LOGGER.info("线程池关闭中。。。。");
  48 + }
  49 + cimClient.close();
  50 + } catch (InterruptedException e) {
  51 + LOGGER.error("InterruptedException", e);
  52 + }
  53 + System.exit(0);
  54 + }
  55 +}
  1 +package com.crossoverjie.cim.client.service;
  2 +
  3 +import com.crossoverjie.cim.client.CIMClientApplication;
  4 +import org.junit.Test;
  5 +import org.junit.runner.RunWith;
  6 +import org.springframework.beans.factory.annotation.Autowired;
  7 +import org.springframework.boot.test.context.SpringBootTest;
  8 +import org.springframework.test.context.junit4.SpringRunner;
  9 +
  10 +@SpringBootTest(classes = CIMClientApplication.class)
  11 +@RunWith(SpringRunner.class)
  12 +public class InnerCommandContextTest {
  13 +
  14 + @Autowired
  15 + private InnerCommandContext context;
  16 +
  17 + @Test
  18 + public void execute() {
  19 + String msg = ":all";
  20 + InnerCommand execute = context.execute(msg);
  21 + execute.process(msg) ;
  22 + }
  23 +}
1 package com.crossoverjie.cim.common.enums; 1 package com.crossoverjie.cim.common.enums;
2 2
3 -import java.util.ArrayList;  
4 import java.util.HashMap; 3 import java.util.HashMap;
5 -import java.util.List;  
6 import java.util.Map; 4 import java.util.Map;
7 5
8 /** 6 /**
@@ -14,14 +12,14 @@ import java.util.Map; @@ -14,14 +12,14 @@ import java.util.Map;
14 */ 12 */
15 public enum SystemCommandEnumType { 13 public enum SystemCommandEnumType {
16 14
17 - ALL(":all ","获取所有命令"),  
18 - ONLINE_USER(":olu ","获取所有在线用户"),  
19 - QUIT(":q! ","退出程序"),  
20 - QUERY(":q ","【:q 关键字】查询聊天记录"),  
21 - AI(":ai ","开启 AI 模式"),  
22 - QAI(":qai ","关闭 AI 模式"),  
23 - PREFIX(":pu ","模糊匹配用户"),  
24 - INFO(":info ","获取客户端信息") 15 + ALL(":all ","获取所有命令","PrintAllCommand"),
  16 + ONLINE_USER(":olu ","获取所有在线用户",""),
  17 + QUIT(":q! ","退出程序","ShutDownCommand"),
  18 + QUERY(":q ","【:q 关键字】查询聊天记录",""),
  19 + AI(":ai ","开启 AI 模式",""),
  20 + QAI(":qai ","关闭 AI 模式",""),
  21 + PREFIX(":pu ","模糊匹配用户",""),
  22 + INFO(":info ","获取客户端信息","")
25 23
26 ; 24 ;
27 25
@@ -31,15 +29,18 @@ public enum SystemCommandEnumType { @@ -31,15 +29,18 @@ public enum SystemCommandEnumType {
31 /** 枚举描述 */ 29 /** 枚举描述 */
32 private final String desc; 30 private final String desc;
33 31
  32 + private final String clazz ;
  33 +
34 34
35 /** 35 /**
36 * 构建一个 。 36 * 构建一个 。
37 * @param commandType 枚举值码。 37 * @param commandType 枚举值码。
38 * @param desc 枚举描述。 38 * @param desc 枚举描述。
39 */ 39 */
40 - private SystemCommandEnumType(String commandType, String desc) { 40 + private SystemCommandEnumType(String commandType, String desc,String clazz) {
41 this.commandType = commandType; 41 this.commandType = commandType;
42 this.desc = desc; 42 this.desc = desc;
  43 + this.clazz = clazz ;
43 } 44 }
44 45
45 /** 46 /**
@@ -49,6 +50,13 @@ public enum SystemCommandEnumType { @@ -49,6 +50,13 @@ public enum SystemCommandEnumType {
49 public String getCommandType() { 50 public String getCommandType() {
50 return commandType; 51 return commandType;
51 } 52 }
  53 + /**
  54 + * 获取 class。
  55 + * @return class。
  56 + */
  57 + public String getClazz() {
  58 + return clazz;
  59 + }
52 60
53 /** 61 /**
54 * 得到枚举描述。 62 * 得到枚举描述。
@@ -80,13 +88,21 @@ public enum SystemCommandEnumType { @@ -80,13 +88,21 @@ public enum SystemCommandEnumType {
80 * @return 全部枚举值码。 88 * @return 全部枚举值码。
81 */ 89 */
82 public static Map<String,String> getAllStatusCode() { 90 public static Map<String,String> getAllStatusCode() {
83 - List<String> list = new ArrayList<String>();  
84 Map<String,String> map = new HashMap<String, String>(16) ; 91 Map<String,String> map = new HashMap<String, String>(16) ;
85 for (SystemCommandEnumType status : values()) { 92 for (SystemCommandEnumType status : values()) {
86 - list.add(status.code());  
87 map.put(status.getCommandType(),status.getDesc()) ; 93 map.put(status.getCommandType(),status.getDesc()) ;
88 } 94 }
89 return map; 95 return map;
90 } 96 }
91 97
  98 + public static Map<String,String> getAllClazz() {
  99 + Map<String,String> map = new HashMap<String, String>(16) ;
  100 + for (SystemCommandEnumType status : values()) {
  101 + map.put(status.getCommandType().trim(),"com.crossoverjie.cim.client.service.impl.command." + status.getClazz()) ;
  102 + }
  103 + return map;
  104 + }
  105 +
  106 +
  107 +
92 } 108 }