From e334acd508356f8b65d3bde317e4eee29121bab2 Mon Sep 17 00:00:00 2001 From: zhaojun1998 Date: Tue, 24 Dec 2019 22:11:45 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E5=A2=9E=E5=8A=A0=E5=90=AF=E5=8A=A8?= =?UTF-8?q?=E6=97=B6=E6=98=BE=E7=A4=BA=E9=A1=B9=E7=9B=AE=E8=AE=BF=E9=97=AE?= =?UTF-8?q?=E5=9C=B0=E5=9D=80=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zhaojun/common/util/StartupListener.java | 35 ++++++++++++++++--- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/src/main/java/im/zhaojun/common/util/StartupListener.java b/src/main/java/im/zhaojun/common/util/StartupListener.java index a095452..92e0e0d 100644 --- a/src/main/java/im/zhaojun/common/util/StartupListener.java +++ b/src/main/java/im/zhaojun/common/util/StartupListener.java @@ -1,31 +1,58 @@ package im.zhaojun.common.util; +import cn.hutool.core.net.NetUtil; import im.zhaojun.common.exception.InitializeException; import im.zhaojun.common.service.FileAsyncCacheService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.context.event.ApplicationStartedEvent; import org.springframework.context.ApplicationListener; -import org.springframework.context.event.ContextRefreshedEvent; +import org.springframework.core.env.Environment; import org.springframework.lang.NonNull; import org.springframework.stereotype.Component; import javax.annotation.Resource; +import java.util.LinkedHashSet; /** * 项目启动监听器, 当项目启动时, 遍历当前对象存储的所有内容, 添加到缓存中. * @author zhaojun */ @Component -public class StartupListener implements ApplicationListener { +@Slf4j +public class StartupListener implements ApplicationListener { @Resource private FileAsyncCacheService fileAsyncCacheService; + @Resource + private Environment environment; + @Override - public void onApplicationEvent(@NonNull ContextRefreshedEvent event) { + public void onApplicationEvent(@NonNull ApplicationStartedEvent event) { + printStartInfo(); + cacheAllFile(); + } + + private void printStartInfo() { + String serverPort = environment.getProperty("server.port", "8080"); + + LinkedHashSet localIps = NetUtil.localIps(); + StringBuilder indexAddr = new StringBuilder(); + StringBuilder indexAdminAddr = new StringBuilder(); + for (String localIp : localIps) { + String addr = String.format("http://%s:%s", localIp, serverPort); + indexAddr.append(addr).append("\t"); + indexAdminAddr.append(addr).append("/#/admin").append("\t"); + } + log.info("ZFile started at " + indexAddr); + log.info("ZFile Admin started at " + indexAdminAddr); + } + + private void cacheAllFile() { try { fileAsyncCacheService.cacheGlobalFile(); } catch (Exception e) { throw new InitializeException("初始化缓存异常.", e); } } - } \ No newline at end of file