作者 crossoverJie

:recycle: 重构代码

... ... @@ -26,8 +26,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
/**
* Function:
*
... ... @@ -67,7 +65,7 @@ public class CIMClient {
*/
private int errorCount;
@PostConstruct
//@PostConstruct
public void start() throws Exception {
//登录 + 获取可以使用的服务器 ip+port
... ...
package com.crossoverjie.cim.client.service;
/**
* Function:
*
* @author crossoverJie
* Date: 2019-01-27 19:26
* @since JDK 1.8
*/
public interface InnerCommand {
/**
* 执行
* @param msg
*/
void process(String msg) ;
}
... ...
... ... @@ -3,6 +3,7 @@ package com.crossoverjie.cim.client.service.impl;
import com.alibaba.fastjson.JSON;
import com.crossoverjie.cim.client.client.CIMClient;
import com.crossoverjie.cim.client.config.AppConfiguration;
import com.crossoverjie.cim.client.service.InnerCommandContext;
import com.crossoverjie.cim.client.service.MsgHandle;
import com.crossoverjie.cim.client.service.MsgLogger;
import com.crossoverjie.cim.client.service.RouteRequest;
... ... @@ -49,7 +50,10 @@ public class MsgHandler implements MsgHandle {
private MsgLogger msgLogger;
@Autowired
private ClientInfo clientInfo ;
private ClientInfo clientInfo;
@Autowired
private InnerCommandContext innerCommandContext ;
private boolean aiModel = false;
... ... @@ -134,6 +138,8 @@ public class MsgHandler implements MsgHandle {
if (msg.startsWith(":")) {
Map<String, String> allStatusCode = SystemCommandEnumType.getAllStatusCode();
innerCommandContext.execute(msg);
if (SystemCommandEnumType.QUIT.getCommandType().trim().equals(msg)) {
//关闭系统
shutdown();
... ...
package com.crossoverjie.cim.client.service.impl.command;
import com.crossoverjie.cim.client.client.CIMClient;
import com.crossoverjie.cim.client.service.InnerCommand;
import com.crossoverjie.cim.client.service.MsgLogger;
import com.crossoverjie.cim.client.service.RouteRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* Function:
*
* @author crossoverJie
* Date: 2019-01-27 19:28
* @since JDK 1.8
*/
@Service
public class ShutDownCommand implements InnerCommand {
private final static Logger LOGGER = LoggerFactory.getLogger(ShutDownCommand.class);
@Autowired
private RouteRequest routeRequest ;
@Autowired
private CIMClient cimClient;
@Autowired
private MsgLogger msgLogger;
@Resource(name = "callBackThreadPool")
private ThreadPoolExecutor executor;
@Override
public void process(String msg) {
LOGGER.info("系统关闭中。。。。");
routeRequest.offLine();
msgLogger.stop();
executor.shutdown();
try {
while (!executor.awaitTermination(1, TimeUnit.SECONDS)) {
LOGGER.info("线程池关闭中。。。。");
}
cimClient.close();
} catch (InterruptedException e) {
LOGGER.error("InterruptedException", e);
}
System.exit(0);
}
}
... ...
package com.crossoverjie.cim.client.service;
import com.crossoverjie.cim.client.CIMClientApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest(classes = CIMClientApplication.class)
@RunWith(SpringRunner.class)
public class InnerCommandContextTest {
@Autowired
private InnerCommandContext context;
@Test
public void execute() {
String msg = ":all";
InnerCommand execute = context.execute(msg);
execute.process(msg) ;
}
}
\ No newline at end of file
... ...
package com.crossoverjie.cim.common.enums;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
... ... @@ -14,14 +12,14 @@ import java.util.Map;
*/
public enum SystemCommandEnumType {
ALL(":all ","获取所有命令"),
ONLINE_USER(":olu ","获取所有在线用户"),
QUIT(":q! ","退出程序"),
QUERY(":q ","【:q 关键字】查询聊天记录"),
AI(":ai ","开启 AI 模式"),
QAI(":qai ","关闭 AI 模式"),
PREFIX(":pu ","模糊匹配用户"),
INFO(":info ","获取客户端信息")
ALL(":all ","获取所有命令","PrintAllCommand"),
ONLINE_USER(":olu ","获取所有在线用户",""),
QUIT(":q! ","退出程序","ShutDownCommand"),
QUERY(":q ","【:q 关键字】查询聊天记录",""),
AI(":ai ","开启 AI 模式",""),
QAI(":qai ","关闭 AI 模式",""),
PREFIX(":pu ","模糊匹配用户",""),
INFO(":info ","获取客户端信息","")
;
... ... @@ -31,15 +29,18 @@ public enum SystemCommandEnumType {
/** 枚举描述 */
private final String desc;
private final String clazz ;
/**
* 构建一个 。
* @param commandType 枚举值码。
* @param desc 枚举描述。
*/
private SystemCommandEnumType(String commandType, String desc) {
private SystemCommandEnumType(String commandType, String desc,String clazz) {
this.commandType = commandType;
this.desc = desc;
this.clazz = clazz ;
}
/**
... ... @@ -49,6 +50,13 @@ public enum SystemCommandEnumType {
public String getCommandType() {
return commandType;
}
/**
* 获取 class。
* @return class。
*/
public String getClazz() {
return clazz;
}
/**
* 得到枚举描述。
... ... @@ -80,13 +88,21 @@ public enum SystemCommandEnumType {
* @return 全部枚举值码。
*/
public static Map<String,String> getAllStatusCode() {
List<String> list = new ArrayList<String>();
Map<String,String> map = new HashMap<String, String>(16) ;
for (SystemCommandEnumType status : values()) {
list.add(status.code());
map.put(status.getCommandType(),status.getDesc()) ;
}
return map;
}
public static Map<String,String> getAllClazz() {
Map<String,String> map = new HashMap<String, String>(16) ;
for (SystemCommandEnumType status : values()) {
map.put(status.getCommandType().trim(),"com.crossoverjie.cim.client.service.impl.command." + status.getClazz()) ;
}
return map;
}
}
\ No newline at end of file
... ...