24282 vor 2 Jahren
Ursprung
Commit
8dccf4c6cd

+ 4 - 0
hx-admin/src/main/resources/application-dev.yml

@@ -89,3 +89,7 @@ seata:
     # seata 事务组编号 用于TC集群名
     tx-service-group: springboot-seata-group
 
+mail:
+    # 调用邮箱服务url前缀
+    urlPrefix: http://localhost:8088/mailService/
+

+ 4 - 0
hx-admin/src/main/resources/application-test.yml

@@ -91,3 +91,7 @@ seata:
             springboot-seata-group: default
     # seata 事务组编号 用于TC集群名
     tx-service-group: springboot-seata-group
+
+mail:
+    # 调用邮箱服务url前缀
+    urlPrefix: http://localhost:8088/mailService/

+ 20 - 0
hx-mail/src/main/java/com/fjhx/mail/config/MailServiceConfig.java

@@ -0,0 +1,20 @@
+package com.fjhx.mail.config;
+
+
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Configuration;
+
+@Configuration
+public class MailServiceConfig {
+
+    /**
+     * 调用主服务url前缀
+     */
+    public static String urlPrefix;
+
+    @Value("${mail.urlPrefix}")
+    public void setUrlPrefix(String urlPrefix) {
+        MailServiceConfig.urlPrefix = urlPrefix;
+    }
+
+}

+ 42 - 0
hx-mail/src/main/java/com/fjhx/mail/listener/LoginEventListeners.java

@@ -0,0 +1,42 @@
+package com.fjhx.mail.listener;
+
+import com.fjhx.mail.util.MailHttpUtil;
+import com.ruoyi.framework.config.ThreadPoolConfig;
+import com.ruoyi.framework.event.LoginEvent;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.context.event.EventListener;
+import org.springframework.scheduling.annotation.Async;
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
+import org.springframework.stereotype.Component;
+
+@Slf4j
+@Component
+public class LoginEventListeners {
+
+    @Autowired
+    @Qualifier(ThreadPoolConfig.threadPoolTaskExecutor)
+    private ThreadPoolTaskExecutor threadPoolTaskExecutor;
+
+    @Async
+    @EventListener
+    public void loginEvent(LoginEvent loginEvent) {
+        threadPoolTaskExecutor.execute(() -> {
+            Long userId = (Long) loginEvent.getSource();
+
+            int num = 0;
+            while (num < 3) {
+                try {
+                    MailHttpUtil.userLogin(userId);
+                    num = 3;
+                } catch (Exception e) {
+                    log.error("同步邮件服务用户登录失败", e);
+                    num++;
+                }
+            }
+
+        });
+    }
+
+}

+ 12 - 0
hx-mail/src/main/java/com/fjhx/mail/util/MailHttpUtil.java

@@ -0,0 +1,12 @@
+package com.fjhx.mail.util;
+
+import cn.hutool.http.HttpUtil;
+import com.fjhx.mail.config.MailServiceConfig;
+
+public class MailHttpUtil {
+
+    public static void userLogin(Long userId) {
+        HttpUtil.get(MailServiceConfig.urlPrefix + "userLogin/" + userId);
+    }
+
+}