码迷,mamicode.com
首页 > 其他好文 > 详细

Tomcat监控助手-自动重启相关服务

时间:2019-10-21 00:11:33      阅读:460      评论:0      收藏:0      [点我收藏+]

标签:错误信息   用户名   功能   col   chcon   layout   工具类   check   ade   

功能说明

该小工具使用swing实现,实现监控某个服务地址,在异常时(连续3次访问不通)自动重启tomcat,并启动配置好的抓取项。

先看下效果图:
技术图片
技术图片

代码说明

下面是代码:
配置文件TomcatMonitor.properties

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#tomcat的启动脚本位置
tomcat.home=D:/luckystar88/soft/apache-tomcat-8.5.6/bin/startup.bat
#tomcat服务监控地址
listen.url=http://localhost:8080/nodeManage/index.jsp
#tomcat监控间隔(秒)
listen.interval=10

#抓取节点
snatch.node=test-99-YY
#抓取节点下的抓取项
snatch.items=皇冠#足球.滚球#1,皇冠#足球.单式#1,皇冠#足球.早餐#0,皇冠#足球.单式.上半场波胆#0,皇冠#足球.单式.全场波胆#0,皇冠#足球.单式.总进球#0,皇冠#足球.单式.半场全场#0,利记#足球.滚球#0,利记#足球.单式#0,利记#足球.早餐#0,浩博#足球.早餐#0,浩博#足球.单式#0,浩博#足球.滚球#0,500w#500w必发指数#0,球探网#足球.赛果(API)#0,球探网#足球.比分.API#0,竞彩网#足球.受注赛程#0
#开启抓取项的URL
snatch.url=http://localhost:8080/nodeManage/nodeSnatchManage/startOrStopSnatchItem

#管理中心的登录地址
snatch.login.url=http://localhost:8080/nodeManage/user/login
#管理中心登录用户名
snatch.login.username=admin
#管理中心登录密码
snatch.login.pwd=test123456

读写配置文件的工具类PropertiesUtils

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

* Created by j.tommy on 2017/9/17.
*/
public final class {
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";
public final static String KEY_SNATCH_NODE = "snatch.node";
public final static String KEY_SNATCH_ITEMS = "snatch.items";
public final static String KEY_SNATCH_URL = "snatch.url";
public final static String KEY_LOGIN_URL = "snatch.login.url";
public final static String KEY_LOGIN_USERNAME = "snatch.login.username";
public final static String KEY_LOGIN_PWD = "snatch.login.pwd";
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();
}
}
}

Http请求的工具类HttpClientUtils:

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

* Created by j.tommy on 2017/9/20.
*/
public final class HttpClientUtils {
private final static String USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36";
private final static ThreadLocal<CloseableHttpClient> HTTP_CLIENT_THREAD_LOCAL = new ThreadLocal<CloseableHttpClient>(){

protected CloseableHttpClient initialValue() {
return HttpClients.custom().setUserAgent(USER_AGENT).build();
}
};
private static CloseableHttpClient getClosableHttpClient() {
return HTTP_CLIENT_THREAD_LOCAL.get();
}
public static String get(String url,Map<String,Object> params,String encoding) throws IOException {
url = makeUrlParams(url,params);
HttpGet hg = new HttpGet(url);
CloseableHttpClient chc = getClosableHttpClient();
try {
CloseableHttpResponse chr = chc.execute(hg);
if (chr.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
return EntityUtils.toString(chr.getEntity(),StringUtils.isEmpty(encoding) ? "utf-8" : encoding);
}
} finally {
hg.releaseConnection();
}
return null;
}
public static String post(String url,Map<String,Object> params,String encoding) throws IOException {
HttpPost hp = new HttpPost(url);

if (null != params && !params.isEmpty()) {
List<NameValuePair> urlParams = new ArrayList<NameValuePair>();
for (Iterator<Map.Entry<String,Object>> it = params.entrySet().iterator();it.hasNext();) {
Map.Entry<String,Object> entry = it.next();
urlParams.add(new BasicNameValuePair(entry.getKey(),entry.getValue()+""));
}
hp.setEntity(new UrlEncodedFormEntity(urlParams));
}
CloseableHttpClient chc = getClosableHttpClient();
try {
CloseableHttpResponse chr = chc.execute(hp);
if (chr.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
return EntityUtils.toString(chr.getEntity(), StringUtils.isEmpty(encoding) ? "utf-8" : encoding);
}
} finally {
hp.releaseConnection();
}
return null;
}

* 组装URL参数
* @param url
* @param params
* @return
*/
private static String makeUrlParams(String url,Map<String,Object> params) {
StringBuffer urlBuf = new StringBuffer(url);
boolean isEmpty = true;
if (null != params && !params.isEmpty()) {
isEmpty = false;
urlBuf.append("?");
}
if (!isEmpty) {
Iterator<Map.Entry<String,Object>> it = params.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String,Object> entry = it.next();
urlBuf.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
}
}
return isEmpty ? url : urlBuf.substring(0,urlBuf.length()-1);
}
}

表格用到的数据对象SnatchConfig:

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

* Created by j.tommy on 2017/9/21.
*/
public class SnatchConfig {
private int index;
private String thirdSystem;
private String snatchItem;
private boolean startFlag;
public SnatchConfig(){}
public SnatchConfig(int index,String thirdSystem,String snatchItem,boolean startFlag) {
this.index = index;
this.thirdSystem = thirdSystem;
this.snatchItem = snatchItem;
this.startFlag = startFlag;
}
public String getSnatchItem() {
return snatchItem;
}
public void setSnatchItem(String snatchItem) {
this.snatchItem = snatchItem;
}
public boolean isStartFlag() {
return startFlag;
}
public void setStartFlag(boolean startFlag) {
this.startFlag = startFlag;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public String getThirdSystem() {
return thirdSystem;
}
public void setThirdSystem(String thirdSystem) {
this.thirdSystem = thirdSystem;
}
}

用于显示表格数据的SnatchInfoTableModel:

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
**
* Created by j.tommy on 2017/9/21.
*/
public class SnatchInfoTableModel implements TableModel {
private String[] headers = {"编号","第三方系统","抓取项","是否开启抓取"};
private java.util.List<SnatchConfig> configs = new ArrayList<SnatchConfig>();
public SnatchInfoTableModel(List<SnatchConfig> configs ) {
this.configs = configs;
}

public int getRowCount() {
return configs.size();
}

public int getColumnCount() {
return headers.length;
}

public String getColumnName(int columnIndex) {
return headers[columnIndex];
}

public Class<?> getColumnClass(int columnIndex) {
if (columnIndex == 0) return Integer.class;
if (columnIndex == 1) return String.class;
if (columnIndex == 2) return String.class;
if (columnIndex == 3) return Boolean.class; // 返回bool类型,swing就会显示CheckBox了。
return null;
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return columnIndex == 0 ? false : true;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
SnatchConfig sc = configs.get(rowIndex);
if (columnIndex == 0) return sc.getIndex();
if (columnIndex == 1) return sc.getThirdSystem();
if (columnIndex == 2) return sc.getSnatchItem();
if (columnIndex == 3) return sc.isStartFlag();
return null;
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
SnatchConfig sc = configs.get(rowIndex);
if (null == sc) sc = new SnatchConfig();
if (columnIndex == 0) System.out.println("暂不提供修改功能。");
else if (columnIndex == 1) sc.setThirdSystem((String) aValue);
else if (columnIndex == 2) sc.setSnatchItem((String) aValue);
else if (columnIndex == 3) sc.setStartFlag((Boolean) aValue);
}
@Override
public void addTableModelListener(TableModelListener l) {

}
@Override
public void removeTableModelListener(TableModelListener l) {
}
}

用于开启抓取项的StartSnatchItemManager:

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

* Created by j.tommy on 2017/9/21.
*/
public final class StartSnatchItemManager {
private static boolean login(TomcatMonitor tm) throws IOException {
String username = PropertiesUtils.getString(PropertiesUtils.KEY_LOGIN_USERNAME);
String pwd = PropertiesUtils.getString(PropertiesUtils.KEY_LOGIN_PWD);
String loginUrl = PropertiesUtils.getString(PropertiesUtils.KEY_LOGIN_URL);
Map<String,Object> params = new HashMap<String, Object>(2);
params.put("username",username);
params.put("pwd",pwd);
String content = HttpClientUtils.post(loginUrl, params, null);
System.out.println(content);
boolean loginCheck = false;
if (null != content && !"".equals(content)) {
String returnCode = (String) JsonUtil.getValue(content,"returncode");
if (null != returnCode && "0".equals(returnCode)) {
loginCheck = true;
}
else {
String errmsg = (String) JsonUtil.getValue(content,"errmsg");
String msg = "登录管理中心失败!无法开启抓取项。错误信息:" + errmsg;
tm.insertLog(msg);
}
}
return loginCheck;
}

* 开启抓取项
* @param tm
* @param snatchUrl 开启抓取项的URL
* @param snatchNode 抓取节点
* @param snatchItems 抓取项(见属性文件配置)
*/
public static void startSnatchItems(TomcatMonitor tm,String snatchUrl,String snatchNode,String snatchItems) {
boolean loginResult = false;
try {
loginResult = login(tm);
} catch (IOException e) {
tm.insertLog("登录失败!err=" + e.getMessage());
e.printStackTrace();
}
if (!loginResult) {
return;
}
Map<String,Object> params = new HashMap<String, Object>(4);
if (null != snatchItems && !"".equals(snatchItems)) {
String[] items = snatchItems.split(",");
params.clear();
for (String item : items) {
String startFlag = item.split("#")[2];
if (!"1".equals(startFlag)) {
continue;
}
params.put("onlyCode",snatchNode);
params.put("operateStatus","1");
params.put("thirdSystem",item.split("#")[0]);
params.put("snatchItem",item.split("#")[1]);
// 开启抓取项
String result = null;
try {
result = HttpClientUtils.get(snatchUrl, params, "utf-8");
} catch (IOException e) {
tm.insertLog("开启抓取项失败!err=" + e.getMessage());
e.printStackTrace();
}
if (null != result) {
Double returncode = (Double) JsonUtil.getValue(result,"returncode");
if (null != result && returncode == 0) {
String msg = "抓取项:[" + item.split("#")[0] + "." + item.split("#")[1] + "]开启成功!";
System.out.println(msg);
tm.insertLog(msg);
}
else {
String errmsg = (String) JsonUtil.getValue(result,"errmsg");
String errMsg = "抓取项:[" + item.split("#")[0] + "." + item.split("#")[1] + "]开启失败!msg=" + errmsg;
tm.insertLog(errMsg);
}
}
}
}
}
}

用来展示GUI和事件处理的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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
大专栏  Tomcat监控助手-自动重启相关服务">233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365

* 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 JTabbedPane tp;
private JTextField snatchNodeFd = new JTextField(60);
private JTextField snatchUrlField = new JTextField(60);
/**
* 是否监控的标志位
*/
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 String snatchNode;
private String snatchItems;
private String snatchUrl;
/**
* 监控线程
*/
private ListenThread lt = null;
public TomcatMonitor() {
initSecondPane();
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监控助手启动成功!");
}
/**
* 初始化抓取项配置TAB组件
*/
private void initSecondPane() {
JPanel secondPane = new JPanel();
GridBagLayout gbl = new GridBagLayout();
secondPane.setLayout(gbl);
List<SnatchConfig> snatchConfigs = getSnatchConfigs();
SnatchInfoTableModel sitm = new SnatchInfoTableModel(snatchConfigs);
final JTable jt = new JTable(sitm);
Font font = new Font("宋体",Font.BOLD,13);
jt.getTableHeader().setFont(font);
jt.setFont(font);
JScrollPane jp = new JScrollPane(jt);
// 设置表格的宽度和高度
for (int i=0;i<jt.getColumnCount();i++) {
if (i == 0)
jt.getColumnModel().getColumn(i).setPreferredWidth(30);
if (i == 1)
jt.getColumnModel().getColumn(i).setPreferredWidth(100);
if (i == 2)
jt.getColumnModel().getColumn(i).setPreferredWidth(200);
}
jt.setRowHeight(40);
jt.setPreferredScrollableViewportSize(new Dimension(820,500));
JLabel snatchNodeLbl = new JLabel("抓取项所属节点:");
snatchNodeFd.setPreferredSize(new Dimension(200,30));
JLabel snatchUrlLabel = new JLabel("开启抓取项URL:");
JButton saveSnatchConfigBtn = new JButton("保存配置");
snatchUrlField.setToolTipText("请输入抓取项URL");
snatchUrlField.setPreferredSize(new Dimension(200,30));
secondPane.add(jp,new GBC(0,0,2,1).setAnchor(GridBagConstraints.WEST).setInsets(-40,10,0,10));
secondPane.add(snatchNodeLbl,new GBC(0,1,1,1).setAnchor(GridBagConstraints.WEST).setInsets(10,10,10,10));
secondPane.add(snatchNodeFd,new GBC(1,1,2,1).setAnchor(GridBagConstraints.WEST).setInsets(10,10,10,10));
secondPane.add(snatchUrlLabel,new GBC(0,3,1,1).setAnchor(GridBagConstraints.WEST).setInsets(10,10,10,10));
secondPane.add(snatchUrlField,new GBC(1,3,2,1).setAnchor(GridBagConstraints.WEST).setInsets(10,10,10,10));
secondPane.add(saveSnatchConfigBtn,new GBC(1,4,1,1).setAnchor(GridBagConstraints.CENTER));
tp.add("抓取项配置",secondPane);
/**
* 保存抓取配置
*/
saveSnatchConfigBtn.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
boolean dataChangeFlag = false;
// 抓取节点
String snatchNode = snatchNodeFd.getText();
if (!snatchNode.equals(TomcatMonitor.this.snatchNode) && !"".equals(snatchNode)) {
dataChangeFlag = true;
PropertiesUtils.setString(PropertiesUtils.KEY_SNATCH_NODE,snatchNode);
TomcatMonitor.this.snatchNode = snatchNode;
}
// 抓取URL
String snatchURL = snatchUrlField.getText();
if (!snatchURL.equals(snatchUrl) && !"".equals(snatchURL)) {
dataChangeFlag = true;
PropertiesUtils.setString(PropertiesUtils.KEY_SNATCH_URL,snatchURL);
TomcatMonitor.this.snatchUrl = snatchURL;
}
int rows = jt.getRowCount();
if (rows > 0) {
StringBuffer snatchConfigBuff = new StringBuffer();
for (int i=0;i<rows;i++) {
String thirdSystem = (String) jt.getModel().getValueAt(i,1);
String snatchItem = (String) jt.getModel().getValueAt(i,2);
boolean startFlag = (Boolean)jt.getModel().getValueAt(i,3);
snatchConfigBuff.append(thirdSystem).append("#").append(snatchItem).append("#").append(startFlag?1:0).append(",");
}
String snatchItems = snatchConfigBuff.substring(0,snatchConfigBuff.length()-1);
if (!snatchItems.equals(TomcatMonitor.this.snatchItems)) {
// 保存到配置文件
PropertiesUtils.setString(PropertiesUtils.KEY_SNATCH_ITEMS,snatchItems);
dataChangeFlag = true;
TomcatMonitor.this.snatchItems = snatchItems;
}
}
String tipMsg = "配置已保存";
if (!dataChangeFlag) {
tipMsg = "配置无修改,无需保存!";
}
JOptionPane.showMessageDialog(null,tipMsg,"提示",JOptionPane.INFORMATION_MESSAGE);
}
});
}
/**
* 输入框验证
* @return
*/
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;
}
/**
* 从配置文件加载tomcat启动脚本位置、监控url、监控间隔、抓取节点、抓取url
*/
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);
String snatchNode = PropertiesUtils.getString(PropertiesUtils.KEY_SNATCH_NODE);
String snatchUrl = PropertiesUtils.getString(PropertiesUtils.KEY_SNATCH_URL);
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;
}
if (null != snatchNode && !"".equals(snatchNode)) {
this.snatchNode = snatchNode;
snatchNodeFd.setText(snatchNode);
}
if (null != snatchUrl && !"".equals(snatchUrl)) {
snatchUrlField.setText(snatchUrl);
this.snatchUrl = snatchUrl;
}
}
/**
* 从配置文件加载抓取项
* @return
*/
private List<SnatchConfig> getSnatchConfigs() {
List<SnatchConfig> snatchConfigs = new ArrayList<SnatchConfig>();
String snatchItems = PropertiesUtils.getString(PropertiesUtils.KEY_SNATCH_ITEMS);
this.snatchItems = snatchItems;
if (null != snatchItems && !"".equals(snatchItems)) {
String[] items = snatchItems.split(",");
int index = 1;
for (String item : items) {
String[] itemConfig = item.split("#");
boolean startFlag = "0".equals(itemConfig[2]) ? false : true;
snatchConfigs.add(new SnatchConfig(index,itemConfig[0],itemConfig[1],startFlag));
index++;
}
}
return snatchConfigs;
}
/**
* 开启tomcat,然后等待120S,待tomcat启动完毕后,开启指定的抓取项。
*/
private void startTomcat() {
Runtime runtime = Runtime.getRuntime();
try {
runtime.exec(tomcatHome);
insertLog("120秒后检测是否启动成功...");
// 休眠120秒后启动抓取项
Thread.sleep(120000);
insertLog("检测是否启动成功...");
// 检查是否启动成功
String content = HttpClientUtils.get(listenUrl,null,"utf-8");
if (null != content && !"".equals(content)) {
// 开启抓取项
insertLog("Tomcat启动成功!正在开启抓取项...");
StartSnatchItemManager.startSnatchItems(this,snatchUrl,snatchNode,snatchItems);
}
else {
insertLog("启动tomcat失败!");
}
} catch (IOException e) {
insertLog("启动tomcat失败!error=" + e.getMessage());
} catch (InterruptedException e) {
insertLog("启动tomcat失败!error=" + e.getMessage());
}
}
/**
* 向logPanel插入日志
* @param msg
*/
public 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();
}
}
/**
* tomcat监控线程,定时请求给定的URL,检测服务是否正常。在tomcat服务异常时自动重启tomcat。
*/
class ListenThread extends Thread {
private int count = 0;
public void run() {
while (listenFlag) {
try {
String content = HttpClientUtils.get(listenUrl,null,"utf-8");
String f = "成功";
if (null == content) {
f = "失败";
count++;
}
String msg = "请求URL:" + listenUrl + ",结果:"+ f;
insertLog(msg);

if (count >= 3) {
insertLog("tomcat服务监控连续3次异常,重新启动...");
startTomcat();
count = 0;
}
} catch (Exception e) {
String msg = "监控异常!error=" + e.getMessage();
insertLog(msg);
insertLog("正在重新启动Tomcat...");
startTomcat();
} finally {
try {
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();
frame.setContentPane(tm.tp);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.pack();
frame.setSize(900,750);
frame.setLocationRelativeTo(tm.tp);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
tm.listenFlag = false;
}
});
}
}

这里特别说明:GUI使用idea来创建的。
技术图片
TAB导航也是可以通过idea来添加的。
技术图片
但是这里我第2个TAB是自己用代码写的,费时费力,完全是不必要的。

打包说明

最后是打包成Jar运行的。
将要打包的文件(.class和.properties)复制到E:/test
技术图片

注意:要将idea生成的class也复制过来
技术图片

打包命令:E:>jar -cvf aa.jar -C test .
上面的命令即将test目录下的所有文件和目录打成一个jar包。

将依赖的jar放到一个lib目录中,然后将打包后的文件和lib放到一个目录中。比如:
技术图片

用解压缩软件打开aa.jar,修改MANIFEST.MF文件,增加Main-Class和Class-Path,如下:
技术图片
Main-Class用来指定程序启动入口,Class-Path指定依赖的jar包。
然后双击aa.jar或者在命令行执行java -jar aa.jar就可以执行了。

jar包中读写配置文件

后来发现上面的方式,读取jar中的配置文件是OK的,但是写入报错。
后采用了下面的方式处理:
这里打包的目录结构如图:
技术图片
compile是class文件,lib是依赖的jar包,res是配置文件。
将程序的class文件拷贝到compile目录,依赖的jar拷贝到lib,依赖的配置文件拷贝到res目录,然后命令行切换到E:/test目录。
执行:E:test>jar -cvf /test/aa.jar -C compile/ .
这样就会在/test目录生成aa.jar。修改aa.jar中的MANIFEST.MF,修改Main-Class和Class-Path,这个是跟上面一样的。然后执行java -jar aa.jar就可以了。

属性文件的读取时使用读写文件,如下:

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
/**
* Created by j.tommy on 2017/9/17.
*/
public final class {
private final static Properties properties = new Properties();
private final static String CONF_FILE = "/res/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";
public final static String KEY_SNATCH_NODE = "snatch.node";
public final static String KEY_SNATCH_ITEMS = "snatch.items";
public final static String KEY_SNATCH_URL = "snatch.url";
public final static String KEY_LOGIN_URL = "snatch.login.url";
public final static String KEY_LOGIN_USERNAME = "snatch.login.username";
public final static String KEY_LOGIN_PWD = "snatch.login.pwd";
private static String fullConfigPath = null;
static {
String rootPath = getRootPath();
fullConfigPath = rootPath + CONF_FILE;
System.out.println(fullConfigPath);
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(fullConfigPath))));
properties.load(br);
} catch (IOException e) {
e.printStackTrace();
}
}
private static String getRootPath() {
String rootPath = System.getProperty("user.dir").replace("\", "/");
return rootPath;
}
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(fullConfigPath),"update " + key);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
}
}

这样执行程序是会报错的,但打jar包执行没问题。

Tomcat监控助手-自动重启相关服务

标签:错误信息   用户名   功能   col   chcon   layout   工具类   check   ade   

原文地址:https://www.cnblogs.com/dajunjun/p/11710969.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!