作者 crossoverJie

:sparkles: Introducing new features.异步写入聊天记录

@@ -2,6 +2,7 @@ package com.crossoverjie.cim.client.scanner; @@ -2,6 +2,7 @@ package com.crossoverjie.cim.client.scanner;
2 2
3 import com.crossoverjie.cim.client.config.AppConfiguration; 3 import com.crossoverjie.cim.client.config.AppConfiguration;
4 import com.crossoverjie.cim.client.service.MsgHandle; 4 import com.crossoverjie.cim.client.service.MsgHandle;
  5 +import com.crossoverjie.cim.client.service.MsgLogger;
5 import com.crossoverjie.cim.client.util.SpringBeanFactory; 6 import com.crossoverjie.cim.client.util.SpringBeanFactory;
6 import org.slf4j.Logger; 7 import org.slf4j.Logger;
7 import org.slf4j.LoggerFactory; 8 import org.slf4j.LoggerFactory;
@@ -26,9 +27,12 @@ public class Scan implements Runnable { @@ -26,9 +27,12 @@ public class Scan implements Runnable {
26 27
27 private MsgHandle msgHandle ; 28 private MsgHandle msgHandle ;
28 29
  30 + private MsgLogger msgLogger ;
  31 +
29 public Scan() { 32 public Scan() {
30 this.configuration = SpringBeanFactory.getBean(AppConfiguration.class); 33 this.configuration = SpringBeanFactory.getBean(AppConfiguration.class);
31 this.msgHandle = SpringBeanFactory.getBean(MsgHandle.class) ; 34 this.msgHandle = SpringBeanFactory.getBean(MsgHandle.class) ;
  35 + this.msgLogger = SpringBeanFactory.getBean(MsgLogger.class) ;
32 } 36 }
33 37
34 @Override 38 @Override
@@ -50,6 +54,8 @@ public class Scan implements Runnable { @@ -50,6 +54,8 @@ public class Scan implements Runnable {
50 //真正的发送消息 54 //真正的发送消息
51 msgHandle.sendMsg(msg) ; 55 msgHandle.sendMsg(msg) ;
52 56
  57 + //写入聊天记录
  58 + msgLogger.log(msg) ;
53 59
54 LOGGER.info("{}:【{}】", configuration.getUserName(), msg); 60 LOGGER.info("{}:【{}】", configuration.getUserName(), msg);
55 } 61 }
  1 +package com.crossoverjie.cim.client.service;
  2 +
  3 +/**
  4 + * Function:
  5 + *
  6 + * @author crossoverJie
  7 + * Date: 2019/1/6 15:23
  8 + * @since JDK 1.8
  9 + */
  10 +public interface MsgLogger {
  11 +
  12 + /**
  13 + * 异步写入消息
  14 + * @param msg
  15 + */
  16 + void log(String msg) ;
  17 +
  18 +
  19 + /**
  20 + * 停止写入
  21 + */
  22 + void stop() ;
  23 +}
  1 +package com.crossoverjie.cim.client.service.impl;
  2 +
  3 +import com.crossoverjie.cim.client.service.MsgLogger;
  4 +import org.slf4j.Logger;
  5 +import org.slf4j.LoggerFactory;
  6 +import org.springframework.stereotype.Service;
  7 +
  8 +import java.util.concurrent.ArrayBlockingQueue;
  9 +import java.util.concurrent.BlockingQueue;
  10 +
  11 +/**
  12 + * Function:
  13 + *
  14 + * @author crossoverJie
  15 + * Date: 2019/1/6 15:26
  16 + * @since JDK 1.8
  17 + */
  18 +@Service
  19 +public class AsyncMsgLogger implements MsgLogger {
  20 +
  21 + private final static Logger LOGGER = LoggerFactory.getLogger(AsyncMsgLogger.class);
  22 +
  23 + /**
  24 + * The default buffer size.
  25 + */
  26 + private static final int DEFAULT_QUEUE_SIZE = 16;
  27 + private BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<String>(DEFAULT_QUEUE_SIZE);
  28 +
  29 + private volatile boolean started = false ;
  30 + private Worker worker = new Worker() ;
  31 +
  32 +
  33 + @Override
  34 + public void log(String msg) {
  35 + //开始消费
  36 + startMsgLogger();
  37 + try {
  38 + blockingQueue.put(msg);
  39 + } catch (InterruptedException e) {
  40 + LOGGER.error("InterruptedException", e);
  41 + }
  42 + }
  43 +
  44 + private class Worker extends Thread {
  45 +
  46 +
  47 + @Override
  48 + public void run() {
  49 + while (started) {
  50 + try {
  51 + String msg = blockingQueue.take();
  52 + LOGGER.info("写入聊天记录={}", msg);
  53 + } catch (InterruptedException e) {
  54 + break;
  55 + }
  56 + }
  57 + }
  58 +
  59 + }
  60 +
  61 + /**
  62 + * 开始工作
  63 + */
  64 + private void startMsgLogger(){
  65 + if (started){
  66 + return ;
  67 + }
  68 +
  69 + worker.setDaemon(true);
  70 + worker.setName("AsyncMsgLogger-Worker");
  71 + started = true ;
  72 + worker.start();
  73 + }
  74 +
  75 +
  76 + @Override
  77 + public void stop() {
  78 + started = false ;
  79 + worker.interrupt();
  80 + }
  81 +}
@@ -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.crossoverjie.cim.client.client.CIMClient; 3 import com.crossoverjie.cim.client.client.CIMClient;
4 import com.crossoverjie.cim.client.config.AppConfiguration; 4 import com.crossoverjie.cim.client.config.AppConfiguration;
5 import com.crossoverjie.cim.client.service.MsgHandle; 5 import com.crossoverjie.cim.client.service.MsgHandle;
  6 +import com.crossoverjie.cim.client.service.MsgLogger;
6 import com.crossoverjie.cim.client.service.RouteRequest; 7 import com.crossoverjie.cim.client.service.RouteRequest;
7 import com.crossoverjie.cim.client.vo.req.GroupReqVO; 8 import com.crossoverjie.cim.client.vo.req.GroupReqVO;
8 import com.crossoverjie.cim.client.vo.req.P2PReqVO; 9 import com.crossoverjie.cim.client.vo.req.P2PReqVO;
@@ -41,6 +42,9 @@ public class MsgHandler implements MsgHandle { @@ -41,6 +42,9 @@ public class MsgHandler implements MsgHandle {
41 @Autowired 42 @Autowired
42 private CIMClient cimClient ; 43 private CIMClient cimClient ;
43 44
  45 + @Autowired
  46 + private MsgLogger msgLogger ;
  47 +
44 @Override 48 @Override
45 public void sendMsg(String msg) { 49 public void sendMsg(String msg) {
46 String[] totalMsg = msg.split(";;"); 50 String[] totalMsg = msg.split(";;");
@@ -140,6 +144,7 @@ public class MsgHandler implements MsgHandle { @@ -140,6 +144,7 @@ public class MsgHandler implements MsgHandle {
140 */ 144 */
141 private void shutdown() { 145 private void shutdown() {
142 LOGGER.info("系统关闭中。。。。"); 146 LOGGER.info("系统关闭中。。。。");
  147 + msgLogger.stop();
143 executor.shutdown(); 148 executor.shutdown();
144 try { 149 try {
145 while (!executor.awaitTermination(1, TimeUnit.SECONDS)) { 150 while (!executor.awaitTermination(1, TimeUnit.SECONDS)) {
@@ -60,4 +60,92 @@ public class CommonTest { @@ -60,4 +60,92 @@ public class CommonTest {
60 } 60 }
61 LOGGER.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); 61 LOGGER.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
62 } 62 }
  63 +
  64 +
  65 + @Test
  66 + public void searchMsg(){
  67 + StringBuilder sb = new StringBuilder() ;
  68 + String allMsg = "于是在之前的基础上我完善了一些内容,先来看看这个项目的介绍吧:\n" +
  69 + "\n" +
  70 + "CIM(CROSS-IM) 一款面向开发者的 IM(即时通讯)系统;同时提供了一些组件帮助开发者构建一款属于自己可水平扩展的 IM 。\n" +
  71 + "\n" +
  72 + "借助 CIM 你可以实现以下需求:" ;
  73 +
  74 + String key = "IM" ;
  75 +
  76 + String[] split = allMsg.split("\n");
  77 + for (String msg : split) {
  78 + if (msg.trim().contains(key)){
  79 + sb.append(msg).append("\n") ;
  80 + }
  81 + }
  82 + int pos = 0;
  83 +
  84 + String result = sb.toString();
  85 +
  86 + int count = 1 ;
  87 + int multiple = 2 ;
  88 + while((pos = result.indexOf(key, pos)) >= 0) {
  89 +
  90 + LOGGER.info("{},{}",pos, pos + key.length());
  91 +
  92 + if (count == 1){
  93 + sb.insert(pos,"**");
  94 + }else {
  95 + Double pow = Math.pow(multiple, count);
  96 + sb.insert(pos +pow.intValue(),"**");
  97 + }
  98 +
  99 + pos += key.length();
  100 +
  101 + if (count == 1){
  102 + sb.insert(pos +2,"**");
  103 + }else {
  104 + Double pow = Math.pow(multiple, count);
  105 + sb.insert((pos +2) + pow.intValue(),"**");
  106 +
  107 + }
  108 +
  109 +
  110 + count ++ ;
  111 + }
  112 +
  113 + System.out.println(sb);
  114 + }
  115 + @Test
  116 + public void searchMsg2(){
  117 + StringBuilder sb = new StringBuilder() ;
  118 + String allMsg = "于是在之前的基础上我完善了一些内容,先来看看这个项目的介绍吧:\n" +
  119 + "\n" +
  120 + "CIM(CROSS-IM) 一款面向开发者的 IM(即时通讯)系统;同时提供了一些组件帮助开发者构建一款属于自己可水平扩展的 IM 。\n" +
  121 + "\n" +
  122 + "借助 CIM 你可以实现以下需求:" ;
  123 +
  124 + String key = "IM" ;
  125 +
  126 + String[] split = allMsg.split("\n");
  127 + for (String msg : split) {
  128 + if (msg.trim().contains(key)){
  129 + sb.append(msg).append("\n") ;
  130 + }
  131 + }
  132 + int pos = 0;
  133 +
  134 + String result = sb.toString();
  135 +
  136 + int count = 1 ;
  137 + int multiple = 2 ;
  138 + while((pos = result.indexOf(key, pos)) >= 0) {
  139 +
  140 + LOGGER.info("{},{}",pos, pos + key.length());
  141 +
  142 + pos += key.length();
  143 +
  144 +
  145 + count ++ ;
  146 + }
  147 +
  148 +
  149 + System.out.println(sb.toString().replace(key,"**" + key+"**"));
  150 + }
63 } 151 }