Skip to content

Commit 872f892

Browse files
committed
sms 增加redis端口检测
1 parent d0e1942 commit 872f892

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

springboot-sms/src/main/java/cn/tellsea/SpringbootSmsApplication.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
package cn.tellsea;
22

3+
import cn.tellsea.utils.PortUtils;
34
import org.springframework.boot.SpringApplication;
45
import org.springframework.boot.autoconfigure.SpringBootApplication;
56

67
@SpringBootApplication
78
public class SpringbootSmsApplication {
89

10+
static {
11+
PortUtils.checkPort(6379, "Redis 服务端", true);
12+
}
13+
914
public static void main(String[] args) {
1015
SpringApplication.run(SpringbootSmsApplication.class, args);
1116
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* 模仿天猫整站 springboot 教程 为 how2j.cn 版权所有
3+
* 本教程仅用于学习使用,切勿用于非法用途,由此引起一切后果与本站无关
4+
* 供购买者学习,请勿私自传播,否则自行承担相关法律责任
5+
*/
6+
7+
package cn.tellsea.utils;
8+
9+
import javax.swing.*;
10+
import java.io.IOException;
11+
import java.net.ServerSocket;
12+
13+
public class PortUtils {
14+
15+
public static boolean testPort(int port) {
16+
try {
17+
ServerSocket ss = new ServerSocket(port);
18+
ss.close();
19+
return false;
20+
} catch (java.net.BindException e) {
21+
return true;
22+
} catch (IOException e) {
23+
return true;
24+
}
25+
}
26+
27+
28+
public static void checkPort(int port, String server, boolean shutdown) {
29+
if (!testPort(port)) {
30+
if (shutdown) {
31+
String message = String.format("在端口 %d 未检查得到 %s 启动%n", port, server);
32+
JOptionPane.showMessageDialog(null, message);
33+
System.exit(1);
34+
} else {
35+
String message = String.format("在端口 %d 未检查得到 %s 启动%n,是否继续?", port, server);
36+
if (JOptionPane.OK_OPTION != JOptionPane.showConfirmDialog(null, message))
37+
System.exit(1);
38+
}
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)