Browse Source

rocketMQ_1

wyfilcn 6 months ago
parent
commit
b7d0ec9308
56 changed files with 640 additions and 600 deletions
  1. 21 15
      logback_logs/app/app.log
  2. 527 403
      logback_logs/root/app.log
  3. BIN
      logs/0
  4. BIN
      logs/1
  5. BIN
      logs/10
  6. BIN
      logs/11
  7. BIN
      logs/12
  8. BIN
      logs/13
  9. BIN
      logs/14
  10. BIN
      logs/15
  11. BIN
      logs/16
  12. BIN
      logs/17
  13. BIN
      logs/18
  14. BIN
      logs/19
  15. BIN
      logs/2
  16. BIN
      logs/20
  17. BIN
      logs/21
  18. BIN
      logs/22
  19. BIN
      logs/23
  20. BIN
      logs/24
  21. BIN
      logs/25
  22. BIN
      logs/26
  23. BIN
      logs/27
  24. BIN
      logs/28
  25. BIN
      logs/29
  26. BIN
      logs/3
  27. BIN
      logs/30
  28. BIN
      logs/31
  29. BIN
      logs/32
  30. BIN
      logs/33
  31. BIN
      logs/34
  32. BIN
      logs/35
  33. BIN
      logs/36
  34. BIN
      logs/37
  35. BIN
      logs/38
  36. BIN
      logs/39
  37. BIN
      logs/4
  38. BIN
      logs/40
  39. BIN
      logs/41
  40. BIN
      logs/42
  41. BIN
      logs/43
  42. BIN
      logs/44
  43. BIN
      logs/45
  44. BIN
      logs/46
  45. BIN
      logs/47
  46. BIN
      logs/48
  47. BIN
      logs/49
  48. BIN
      logs/5
  49. BIN
      logs/6
  50. BIN
      logs/7
  51. BIN
      logs/7-journal
  52. BIN
      logs/8
  53. BIN
      logs/9
  54. 5 0
      pom.xml
  55. 86 181
      src/main/java/com/scbfkj/uni/process/RocketMQClient.java
  56. 1 1
      src/main/java/com/scbfkj/uni/services/SecurityService.java

+ 21 - 15
logback_logs/app/app.log

@@ -1,15 +1,21 @@
-10:23:19.136 [restartedMain] INFO  com.scbfkj.uni.Application - Starting Application using Java 17.0.8 with PID 18208 (D:\work\code\uni206\uni-factory\target\classes started by WYF in D:\work\code\uni206\uni-factory)
-10:23:19.139 [restartedMain] INFO  com.scbfkj.uni.Application - No active profile set, falling back to 1 default profile: "default"
-10:23:50.475 [restartedMain] INFO  com.scbfkj.uni.Application - Started Application in 32.067 seconds (process running for 33.936)
-10:54:22.414 [restartedMain] INFO  com.scbfkj.uni.Application - Starting Application using Java 17.0.8 with PID 20652 (D:\work\code\uni206\uni-factory\target\classes started by WYF in D:\work\code\uni206\uni-factory)
-10:54:22.415 [restartedMain] INFO  com.scbfkj.uni.Application - No active profile set, falling back to 1 default profile: "default"
-10:54:51.522 [restartedMain] INFO  com.scbfkj.uni.Application - Started Application in 29.634 seconds (process running for 30.511)
-16:16:22.139 [restartedMain] INFO  com.scbfkj.uni.Application - Starting Application using Java 17.0.8 with PID 7368 (D:\work\code\uni206\uni-factory\target\classes started by WYF in D:\work\code\uni206\uni-factory)
-16:16:22.151 [restartedMain] INFO  com.scbfkj.uni.Application - No active profile set, falling back to 1 default profile: "default"
-16:16:53.650 [restartedMain] INFO  com.scbfkj.uni.Application - Started Application in 32.38 seconds (process running for 34.371)
-16:58:15.395 [restartedMain] INFO  com.scbfkj.uni.Application - Starting Application using Java 17.0.8 with PID 2028 (D:\work\code\uni206\uni-factory\target\classes started by WYF in D:\work\code\uni206\uni-factory)
-16:58:15.396 [restartedMain] INFO  com.scbfkj.uni.Application - No active profile set, falling back to 1 default profile: "default"
-16:58:44.024 [restartedMain] INFO  com.scbfkj.uni.Application - Started Application in 29.13 seconds (process running for 29.882)
-17:05:48.998 [restartedMain] INFO  com.scbfkj.uni.Application - Starting Application using Java 17.0.8 with PID 15232 (D:\work\code\uni206\uni-factory\target\classes started by WYF in D:\work\code\uni206\uni-factory)
-17:05:48.999 [restartedMain] INFO  com.scbfkj.uni.Application - No active profile set, falling back to 1 default profile: "default"
-17:06:17.048 [restartedMain] INFO  com.scbfkj.uni.Application - Started Application in 28.599 seconds (process running for 29.705)
+09:33:46.722 [restartedMain] INFO  com.scbfkj.uni.Application - Starting Application using Java 17.0.8 with PID 17744 (D:\work\code\uni_206_w\target\classes started by WYF in D:\work\code\uni_206_w)
+09:33:46.737 [restartedMain] INFO  com.scbfkj.uni.Application - No active profile set, falling back to 1 default profile: "default"
+09:34:19.261 [restartedMain] INFO  com.scbfkj.uni.Application - Started Application in 33.23 seconds (process running for 34.417)
+09:37:39.293 [restartedMain] INFO  com.scbfkj.uni.Application - Starting Application using Java 17.0.8 with PID 7440 (D:\work\code\uni_206_w\target\classes started by WYF in D:\work\code\uni_206_w)
+09:37:39.295 [restartedMain] INFO  com.scbfkj.uni.Application - No active profile set, falling back to 1 default profile: "default"
+09:38:13.124 [restartedMain] INFO  com.scbfkj.uni.Application - Started Application in 34.41 seconds (process running for 35.504)
+09:57:57.479 [restartedMain] INFO  com.scbfkj.uni.Application - Starting Application using Java 17.0.8 with PID 16860 (D:\work\code\uni_206_w\target\classes started by WYF in D:\work\code\uni_206_w)
+09:57:57.480 [restartedMain] INFO  com.scbfkj.uni.Application - No active profile set, falling back to 1 default profile: "default"
+09:58:30.377 [restartedMain] INFO  com.scbfkj.uni.Application - Started Application in 33.414 seconds (process running for 34.344)
+10:23:32.005 [restartedMain] INFO  com.scbfkj.uni.Application - Starting Application using Java 17.0.8 with PID 16572 (D:\work\code\uni_206_w\target\classes started by WYF in D:\work\code\uni_206_w)
+10:23:32.019 [restartedMain] INFO  com.scbfkj.uni.Application - No active profile set, falling back to 1 default profile: "default"
+10:24:09.266 [restartedMain] INFO  com.scbfkj.uni.Application - Started Application in 38.184 seconds (process running for 40.907)
+10:26:12.026 [restartedMain] INFO  com.scbfkj.uni.Application - Starting Application using Java 17.0.8 with PID 16400 (D:\work\code\uni_206_w\target\classes started by WYF in D:\work\code\uni_206_w)
+10:26:12.028 [restartedMain] INFO  com.scbfkj.uni.Application - No active profile set, falling back to 1 default profile: "default"
+10:26:46.157 [restartedMain] INFO  com.scbfkj.uni.Application - Started Application in 34.84 seconds (process running for 36.318)
+10:29:05.847 [restartedMain] INFO  com.scbfkj.uni.Application - Starting Application using Java 17.0.8 with PID 9440 (D:\work\code\uni_206_w\target\classes started by WYF in D:\work\code\uni_206_w)
+10:29:05.849 [restartedMain] INFO  com.scbfkj.uni.Application - No active profile set, falling back to 1 default profile: "default"
+10:29:39.453 [restartedMain] INFO  com.scbfkj.uni.Application - Started Application in 34.312 seconds (process running for 35.516)
+10:30:28.586 [restartedMain] INFO  com.scbfkj.uni.Application - Starting Application using Java 17.0.8 with PID 18076 (D:\work\code\uni_206_w\target\classes started by WYF in D:\work\code\uni_206_w)
+10:30:28.587 [restartedMain] INFO  com.scbfkj.uni.Application - No active profile set, falling back to 1 default profile: "default"
+10:31:02.415 [restartedMain] INFO  com.scbfkj.uni.Application - Started Application in 34.452 seconds (process running for 35.736)

File diff suppressed because it is too large
+ 527 - 403
logback_logs/root/app.log


BIN
logs/0


BIN
logs/1


BIN
logs/10


BIN
logs/11


BIN
logs/12


BIN
logs/13


BIN
logs/14


BIN
logs/15


BIN
logs/16


BIN
logs/17


BIN
logs/18


BIN
logs/19


BIN
logs/2


BIN
logs/20


BIN
logs/21


BIN
logs/22


BIN
logs/23


BIN
logs/24


BIN
logs/25


BIN
logs/26


BIN
logs/27


BIN
logs/28


BIN
logs/29


BIN
logs/3


BIN
logs/30


BIN
logs/31


BIN
logs/32


BIN
logs/33


BIN
logs/34


BIN
logs/35


BIN
logs/36


BIN
logs/37


BIN
logs/38


BIN
logs/39


BIN
logs/4


BIN
logs/40


BIN
logs/41


BIN
logs/42


BIN
logs/43


BIN
logs/44


BIN
logs/45


BIN
logs/46


BIN
logs/47


BIN
logs/48


BIN
logs/49


BIN
logs/5


BIN
logs/6


BIN
logs/7


BIN
logs/7-journal


BIN
logs/8


BIN
logs/9


+ 5 - 0
pom.xml

@@ -76,6 +76,11 @@
             <groupId>org.apache.activemq</groupId>
             <artifactId>artemis-jakarta-client</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.apache.rocketmq</groupId>
+            <artifactId>rocketmq-client</artifactId>
+            <version>5.2.0</version>
+        </dependency>
         <dependency>
             <groupId>org.elasticsearch.client</groupId>
             <artifactId>elasticsearch-rest-client</artifactId>

+ 86 - 181
src/main/java/com/scbfkj/uni/process/RocketMQClient.java

@@ -1,25 +1,27 @@
 package com.scbfkj.uni.process;
 
 import cn.hutool.extra.spring.SpringUtil;
+import com.alibaba.fastjson.JSONObject;
 import com.fasterxml.jackson.core.JsonProcessingException;
 import com.scbfkj.uni.exceptions.UniException;
 import com.scbfkj.uni.library.DataFormatUtil;
 import com.scbfkj.uni.logs.Log;
 import com.scbfkj.uni.utils.SystemUtil;
 import jakarta.annotation.Resource;
-import jakarta.jms.ConnectionFactory;
-import jakarta.jms.JMSException;
-import jakarta.jms.Message;
-import jakarta.jms.TextMessage;
 import lombok.AllArgsConstructor;
 import lombok.Data;
 import lombok.NoArgsConstructor;
-import org.apache.activemq.artemis.jms.client.ActiveMQJMSConnectionFactory;
-import org.apache.commons.pool2.BaseKeyedPooledObjectFactory;
-import org.apache.commons.pool2.KeyedObjectPool;
-import org.apache.commons.pool2.PooledObject;
-import org.apache.commons.pool2.impl.DefaultPooledObject;
-import org.apache.commons.pool2.impl.GenericKeyedObjectPool;
+import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
+import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
+import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
+import org.apache.rocketmq.client.exception.MQBrokerException;
+import org.apache.rocketmq.client.exception.MQClientException;
+import org.apache.rocketmq.client.producer.DefaultMQProducer;
+import org.apache.rocketmq.client.producer.SendResult;
+import org.apache.rocketmq.common.message.Message;
+import org.apache.rocketmq.common.message.MessageExt;
+import org.apache.rocketmq.remoting.common.RemotingHelper;
+import org.apache.rocketmq.remoting.exception.RemotingException;
 import org.springframework.jms.core.JmsTemplate;
 import org.springframework.stereotype.Component;
 
@@ -34,43 +36,25 @@ import java.util.List;
  */
 @Component
 public class RocketMQClient {
-    /**
-     * 消费者是非线程安全的所以需要一个对象池
-     */
-    private static final KeyedObjectPool<String, JmsTemplate> JMS_TEMPLATE_POOL =
-            new GenericKeyedObjectPool<>(new BaseKeyedPooledObjectFactory<>() {
-                @Override
-                public JmsTemplate create(String key) throws JsonProcessingException {
-                    RocketConnection rocketConnection = SystemUtil.OBJECT_MAPPER.readValue(key, RocketConnection.class);
-                    return createJmsTemplate(new ActiveMQJMSConnectionFactory(rocketConnection.getUsername(),
-                            rocketConnection.getPassword(),
-                            rocketConnection.getHost()));
-                }
-                
-                @Override
-                public PooledObject<JmsTemplate> wrap(JmsTemplate value) {
-                    return new DefaultPooledObject<>(value);
-                }
-            });
     
     @Resource
     private DataFormatUtil dataFormatUtil;
-    
+
     /**
-     * 创建JmsTemplate实例。 这个方法用于初始化一个新的JmsTemplate对象,并将其与给定的ConnectionFactory对象关联。
-     *
-     * @param factory ConnectionFactory对象,用于创建与消息服务的连接。
-     *
-     * @return 返回创建的JmsTemplate实例,这个实例可以用于发送消息。
+     * 读取配置文件中设置的rocketmq相关属性,创建消息生产者
      */
-    private static JmsTemplate createJmsTemplate(ConnectionFactory factory) {
-        JmsTemplate template = new JmsTemplate();
-        // 设置ConnectionFactory到JmsTemplate
-        template.setConnectionFactory(factory);
-        // 返回配置好的JmsTemplate实例
-        return template;
+    private DefaultMQProducer getRocketMqProducer(RocketMqConfig rocketMqConfig){
+        // 1 创建消息生产者,指定生成组名
+        DefaultMQProducer defaultMQProducer = new DefaultMQProducer(rocketMqConfig.getGroupId());
+        // 2 指定NameServer的地址
+        defaultMQProducer.setNamesrvAddr(rocketMqConfig.getAddr());
+        // 3 设置消息超时时间
+        defaultMQProducer.setSendMsgTimeout(Integer.parseInt("10000"));
+        // 4 同步发送消息,如果SendMsgTimeout时间内没有发送成功,则重试retryWhenSendFailed次
+        defaultMQProducer.setRetryTimesWhenSendFailed(Integer.parseInt("5"));
+        return defaultMQProducer;
     }
-    
+
     /**
      * 向指定队列发送消息。
      * 此方法封装了向指定RocketMQ队列发送消息的逻辑,支持发送多种格式的数据。
@@ -84,57 +68,39 @@ public class RocketMQClient {
      * @param queueName 需要发送消息的队列名称
      * @param data 要发送的消息内容,支持发送Object数组或实现Iterable接口的对象
      */
-    public void sendMessage(String host, String username, String password, String queueName, Object data)
+    public void sendMessage(RocketMqConfig rocketMqConfig,String topic,String tag, Object data)
             throws UniException, JsonProcessingException {
-        RocketConnection rocketConnection = new RocketConnection(host, username, password);
-        String key = SystemUtil.OBJECT_MAPPER.writeValueAsString(rocketConnection);
-        JmsTemplate template;
+        DefaultMQProducer rocketMqProducer = getRocketMqProducer(rocketMqConfig);
+
         try {
-            template = JMS_TEMPLATE_POOL.borrowObject(key);
+            rocketMqProducer.start();
+            Message message = new Message(topic, tag, data.toString().getBytes());
+
+            SendResult result = null;
+            result = rocketMqProducer.send(message);
+
         } catch (Exception e) {
-            throw new UniException(e);
-        }
-        try {
-            // 判断数据类型,支持发送Object数组或实现Iterable接口的对象
-            if (data instanceof Object[] datas) {
-                // 如果是对象数组,则循环发送每一个对象
-                for (Object it : datas) {
-                    execSend(template, it, queueName);
-                }
-            } else if (data instanceof Iterable<?> datas) {
-                // 如果是实现了Iterable接口的对象,则同样循环发送每一个对象
-                for (Object it : datas) {
-                    execSend(template, it, queueName);
-                }
-            } else {
-                // 如果是单个对象或不支持的类型,则直接发送
-                execSend(template, data, queueName);
-            }
+            throw new RuntimeException(e);
         } finally {
-            if (template != null) {
-                try {
-                    JMS_TEMPLATE_POOL.returnObject(key, template);
-                } catch (Exception e) {
-                    SpringUtil.getBean(Log.class).error("JmsTemplate 归还失败", e);
-                }
-            }
+            rocketMqProducer.shutdown();
         }
     }
-    
-    /**
-     * 执行发送消息操作。此方法通过JmsTemplate发送消息到指定的队列。
-     *
-     * @param template JmsTemplate实例,用于发送消息。这是一个Spring提供的用于发送JMS消息的模板类,简化了消息发送的操作。
-     * @param data 要发送的数据。此参数将被转换为JSON字符串后发送。
-     * @param queueName 消息发送的目的地队列名称。指定消息要发送到的队列。
-     */
-    private void execSend(JmsTemplate template, Object data, String queueName) {
-        // 将数据转换为JSON字符串
-        String message = dataFormatUtil.toString(data);
-        // 使用JmsTemplate发送消息到指定队列
-        template.send(queueName, session -> session.createTextMessage(message));
+
+    private DefaultMQPushConsumer getRocketMqConsumer(RocketMqConfig rocketMqConfig,String topic,String tag)
+            throws UniException {
+        // 1 创建消费者,指定所属的消费者组名
+        DefaultMQPushConsumer defaultMQPushConsumer = new DefaultMQPushConsumer(rocketMqConfig.getGroupId() );
+        // 2 指定NameServer的地址
+        defaultMQPushConsumer.setNamesrvAddr(rocketMqConfig.getAddr());
+        // 3 指定消费者订阅的主题和标签
+        try {
+            defaultMQPushConsumer.subscribe(topic, tag);
+        } catch (MQClientException e) {
+            throw new UniException(e);
+        }
+        return defaultMQPushConsumer;
     }
-    
+
     /**
      * 从指定队列接收消息。
      *
@@ -148,111 +114,50 @@ public class RocketMQClient {
      *
      * @return 返回接收到的消息列表
      */
-    public List<String> receptionMessage(String host, String username, String password, String queueName,
-                                         Long receiveTimeout, Long pollSize, Long retry)
+    public List<String> receptionMessage(RocketMqConfig rocketMqConfig,String topic,String tag)
             throws UniException, JsonProcessingException {
-        RocketConnection rocketConnection = new RocketConnection(host, username, password);
-        String key = SystemUtil.OBJECT_MAPPER.writeValueAsString(rocketConnection);
-        JmsTemplate jmsTemplate;
-        try {
-            jmsTemplate = JMS_TEMPLATE_POOL.borrowObject(key);
-        } catch (Exception e) {
-            throw new UniException(e);
-        }
-        try {
-            jmsTemplate.setReceiveTimeout(receiveTimeout);
-            // 默认最大接收消息数量
-            long maxSize = pollSize > 0 ? pollSize : 100;
-            // 记录消息接收失败的重试次数
-            int maxRetry = 0;
-            // 用于存储接收到的消息
-            List<String> result = new ArrayList<>();
-            long lastMessageDateTime = System.currentTimeMillis();
-            // 循环接收消息,直到满足退出条件
-            while (true) {
-                try {
-                    // 尝试接收消息
-                    Message message = jmsTemplate.receive(queueName);
-                    boolean isBreak = getMessage(message, result, maxSize);
-                    if (! isBreak && message == null &&
-                            System.currentTimeMillis() - lastMessageDateTime > receiveTimeout) {
-                        isBreak = true;
-                    }
-                    if (isBreak) {
-                        break;
-                    }
-                } catch (Exception e) { // 捕获接收消息过程中可能抛出的异常
-                    maxRetry++;
-                    boolean isReturn = reTry(retry, maxRetry);
-                    if (isReturn) {
-                        return result;
-                    }
-                }
-            }
-            // 返回接收到的消息列表
-            return result;
-        } finally {
+        DefaultMQPushConsumer rocketMqConsumer = getRocketMqConsumer(rocketMqConfig, topic,tag);
+
+        rocketMqConsumer.registerMessageListener((List<MessageExt> list, ConsumeConcurrentlyContext context) -> {
+
             try {
-                JMS_TEMPLATE_POOL.returnObject(key, jmsTemplate);
+                System.out.println("收到消息--》" + list);
+                for (MessageExt messageExt : list) {
+                    String message=new String(messageExt.getBody(), RemotingHelper.DEFAULT_CHARSET);
+                    JSONObject object= JSONObject.parseObject(message);
+                    String fileId = (String) object.get("fileId");
+                    String fileCreateDate = (String) object.get("fileCreateDate");
+                }
+
             } catch (Exception e) {
-                SpringUtil.getBean(Log.class).error("JmsTemplate 归还失败", e);
-            }
-        }
-    }
-    
-    /**
-     * 尝试从给定的消息对象中获取消息文本,并将其添加到结果列表中。 当达到最大消息数量或未找到消息时,停止获取。
-     *
-     * @param message 待处理的消息对象,如果为null表示没有消息可处理。
-     * @param result 用于收集消息文本的结果列表。
-     * @param maxSize 最大消息数量,当接收的消息数量达到此值时,将停止接收。
-     *
-     * @return 如果找到并处理了消息,返回true;如果未找到消息或已处理达到最大数量,返回false。
-     *
-     * @throws JMSException 如果处理消息时发生错误。
-     */
-    private static boolean getMessage(Message message, List<String> result, long maxSize) throws JMSException {
-        // 标记是否找到(接收到)消息
-        boolean found = message == null;
-        if (found) {
-            // 如果没有消息,则退出循环
-            return false;
-        }
-        // 处理文本消息
-        if (message instanceof TextMessage msg) {
-            // 获取消息文本
-            String text = msg.getText();
-            // 将消息添加到结果列表
-            result.add(text);
-            // 如果接收的消息数量达到最大值,退出循环
-            if (result.size() >= maxSize) {
-                found = true;
+                throw new UniException(e);
             }
+
+            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
+        });
+
+        // 5 启动消费者
+        try {
+            rocketMqConsumer.start();
+        } catch (MQClientException e) {
+            throw new UniException(e);
         }
-        return found;
-    }
-    
-    /**
-     * 尝试重新接收消息的函数。 对于给定的主机、用户名、密码和队列名称,如果接收消息失败,将根据设定的最大重试次数进行重试。
-     *
-     * @param retry 当前的重试次数。
-     * @param maxRetry 允许的最大重试次数。
-     *
-     * @return 如果重试次数超过最大重试次数,返回已接收的消息列表;否则返回空列表。
-     */
-    private boolean reTry(Long retry, int maxRetry) {
-        return maxRetry > retry;
+
+        return null;
     }
-    
+
+
     @Data
     @AllArgsConstructor
     @NoArgsConstructor
-    private static class RocketConnection {
-        private String host;
-        
-        private String username;
-        
-        private String password;
+    static class RocketMqConfig {
+        private String addr;
+
+        private String accessKey;
+
+        private String secretKey;
+
+        private String groupId;
     }
 }
 

+ 1 - 1
src/main/java/com/scbfkj/uni/services/SecurityService.java

@@ -158,7 +158,7 @@ public class SecurityService {
         String appId = body.getAppId();
         String appSecret = body.getAppSecret();
         // 删除过期的连接日志
-        appConnectLogRepository.deleteAllByExpireTimeBefore(LocalDateTime.now());
+//        appConnectLogRepository.deleteAllByExpireTimeBefore(LocalDateTime.now());
         // 查询应用信息
         Optional<Application> applicationOptional = applicationRepository.findByAppIdAndAppSecret(appId, appSecret);
         // 如果查询结果为空,抛出运行时异常

Some files were not shown because too many files changed in this diff