JAVA-SWING监控Tomcat小工具

由于线上的一个抓取节点Tomcat经常莫名其妙的被关掉,所以准备写个小工具来监控。在Tomcat服务异常时,重新启动。

界面使用的intellij idea设计。



设计完毕后,点右键->Jump to source

注意:idea不会为你生成main方法,需要自己写。

完整代码:
TomcatMonitor

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/**
* Tomcat监控助手。
* 用来实现在Tomcat服务异常时,自动重启Tomcat服务。
* Created by j.tommy on 2017/9/17.
*/
public class TomcatMonitor {
/**
* Tomcat启动脚本位置
*/
private JTextField textField1;
/**
* 监控地址URL
*/
private JTextField textField2;
/**
* 用来显示监控日志的Panel
*/
private JTextPane logPanel;
/**
* 使用帮助按钮
*/
private JButton helpButton;
JPanel mainPanel;
/**
* 开始监控按钮
*/
private JButton jkButton;
/**
* 监控间隔(秒)
*/
private JTextField textField3;
/**
* 是否监控的标志位
*/
private boolean listenFlag = true;
private final DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private String tomcatHome;
private String listenUrl;
private String interval;
/**
* 监控线程
*/
private ListenThread lt = null;
public TomcatMonitor() {
loadConf();
helpButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "这是一个tomcat监控小工具,通过定时扫描指定的页面来确认服务是否正常。\n在服务异常时,自动重新启动tomcat服务器。\n启动是通过命令行启动的tomcat的startup.bat脚本。\n", "使用说明", JOptionPane.INFORMATION_MESSAGE);
}
});
logPanel.setContentType("text/html");
logPanel.setEditable(false);
jkButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String text = "监控中...";
if (jkButton.getText().equals(text)) {
int r = JOptionPane.showConfirmDialog(null,"停止监控?","系统提示",JOptionPane.YES_NO_OPTION);
if (r == JOptionPane.YES_OPTION) {
TomcatMonitor.this.lt = null;
TomcatMonitor.this.listenFlag = false;
jkButton.setText("开始监控");
}
return;
}
boolean validateFlag = validateForm();
if (validateFlag) {
TomcatMonitor.this.listenFlag = true;
if (lt == null ) {
lt = new ListenThread();
lt.start();
}
jkButton.setText(text);
}
}
});
insertLog("tomcat监控助手启动成功!");
}
private boolean validateForm() {
String tomcatHome = textField1.getText();
String listenUrl = textField2.getText();
String interval = textField3.getText();
if (null == tomcatHome || "".equals(tomcatHome)) {
JOptionPane.showMessageDialog(null,"请输入tomcat启动脚本位置!","系统提示",JOptionPane.WARNING_MESSAGE);
return false;
}
if (null == listenUrl || "".equals(listenUrl)) {
JOptionPane.showMessageDialog(null,"请输入监控地址!","系统提示",JOptionPane.WARNING_MESSAGE);
return false;
}
if (!listenUrl.startsWith("http://")) {
JOptionPane.showMessageDialog(null,"请输入正确的监控地址!","系统提示",JOptionPane.WARNING_MESSAGE);
return false;
}
if (!interval.matches("\\d+")) {
JOptionPane.showMessageDialog(null,"请输入正确的监控间隔(秒)!","系统提示",JOptionPane.WARNING_MESSAGE);
return false;
}
int intervalTime = Integer.parseInt(interval);
if (intervalTime < 10) {
JOptionPane.showMessageDialog(null,"监控间隔不能小于10秒!","系统提示",JOptionPane.WARNING_MESSAGE);
return false;
}
if (!tomcatHome.equals(this.tomcatHome)) {
this.tomcatHome = tomcatHome;
PropertiesUtils.setString(PropertiesUtils.KEY_TOMCAT_HOME,tomcatHome);
}
if (!listenUrl.equals(this.listenUrl)) {
this.listenUrl = listenUrl;
PropertiesUtils.setString(PropertiesUtils.KEY_LISTEN_URL,listenUrl);
}
if (!interval.equals(this.interval)) {
this.interval = interval;
PropertiesUtils.setString(PropertiesUtils.KEY_LISTEN_INTERVAL,interval);
}
return true;
}
private void loadConf() {
String tomcatHome = PropertiesUtils.getString(PropertiesUtils.KEY_TOMCAT_HOME);
String listenUrl = PropertiesUtils.getString(PropertiesUtils.KEY_LISTEN_URL);
String listenInterval = PropertiesUtils.getString(PropertiesUtils.KEY_LISTEN_INTERVAL);
if (null != tomcatHome && !"".equals(tomcatHome)) {
textField1.setText(tomcatHome);
this.tomcatHome = tomcatHome;
}
if (null != listenUrl && !"".equals(listenUrl)) {
textField2.setText(listenUrl);
this.listenUrl = listenUrl;
}
if (null != listenInterval && !"".equals(listenInterval)) {
textField3.setText(listenInterval);
this.interval = listenInterval;
}
}
private void startTomcat() {
Runtime runtime = Runtime.getRuntime();
try {
runtime.exec(tomcatHome);
} catch (IOException e) {
insertLog("启动tomcat失败!error=" + e.getMessage());
}
}
private void insertLog(String msg) {
StyledDocument sd = logPanel.getStyledDocument();
try {
String current = df.format(new Date());
msg = current + "," + msg +"\n";
int docLength = sd.getLength();
if (docLength > 100000) {
sd.remove(0,docLength);
sd.insertString(sd.getLength(),"日志过多,之前的日志已被清除!", logPanel.getCharacterAttributes());
}
sd.insertString(sd.getLength(),msg,logPanel.getCharacterAttributes());
} catch (BadLocationException e) {
e.printStackTrace();
}
}
class ListenThread extends Thread {
public void run() {
while (listenFlag) {
HttpURLConnection connection = null;
try {
URL url = new URL(listenUrl);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
int responseCode = connection.getResponseCode();
String msg = "请求URL:" + listenUrl + ",结果:"+ (responseCode > 200 ? "失败":"成功") + ",responseCode=" + responseCode;
insertLog(msg);
} catch (Exception e) {
String msg = "监控异常!error=" + e.getMessage();
insertLog(msg);
insertLog("正在重新启动Tomcat...");
startTomcat();
} finally {
try {
if (null != connection) {
connection.disconnect();
}
Thread.sleep(1000*Long.parseLong(interval));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Tomcat监控小工具");
final TomcatMonitor tm = new TomcatMonitor();
JScrollPane jsp = new JScrollPane(tm.mainPanel);
frame.setContentPane(jsp);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.pack();
frame.setSize(800,600);
frame.setLocationRelativeTo(jsp);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);

tm.listenFlag = false;
}
});
}
}

一个属性文件读写工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* Created by j.tommy on 2017/9/17.
*/
public final class PropertiesUtils {
private final static Properties properties = new Properties();
private final static String CONF_FILE = "/TomcatMonitor.properties";
public final static String KEY_TOMCAT_HOME = "tomcat.home";
public final static String KEY_LISTEN_URL = "listen.url";
public final static String KEY_LISTEN_INTERVAL = "listen.interval";
static {
InputStream in = PropertiesUtils.class.getResourceAsStream(CONF_FILE);
try {
properties.load(in);
} catch (IOException e) {
e.printStackTrace();
}
}
public static String getString(String key) {
return properties.getProperty(key);
}
public static void setString(String key,String value) {
properties.setProperty(key,value);
try {
properties.store(new FileOutputStream(PropertiesUtils.class.getResource(CONF_FILE).getPath()),"update " + key);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {

}
}

属性文件TomcatMonitor.properties:

1
2
3
tomcat.home=D:/soft/tomcat7.0.1/bin/startup.bat
listen.url=http://localhost:8080/abc/test.jsp
listen.interval=10

最后打成一个JAR包,方便执行。
注意:idea设计的GUI窗体代码在com/intellij下面,这个也要打进去。

将相关的classes复制到一个单独的目录。如图:

注意包括配置文件。

然后用java命令打包

1
2
cd E:
jar -cvf aa.jar -C test .

这样打包后jar中的MANIFEST.MF文件中没有Main-Class配置,增加:

然后双击或者在命令行执行java -jar TomcatMonitor.jar就可以了。

Donny wechat
欢迎关注我的个人公众号
打赏,是超越赞的一种表达。
Show comments from Gitment