功能说明
该小工具使用swing实现,实现监控某个服务地址,在异常时(连续3次访问不通)自动重启tomcat,并启动配置好的抓取项。
先看下效果图:
代码说明
下面是代码:
配置文件TomcatMonitor.properties
1234567891011121314151617181920 |
#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#开启抓取项的URLsnatch.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
12345678910111213141516171819202122232425262728293031323334353637 |
* 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:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
* 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:
12345678910111213141516171819202122232425262728293031323334353637383940 |
* 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:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
** * 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:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
* 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:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232大专栏 Tomcat监控助手-自动重启相关服务">233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365 |
* 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就可以了。
属性文件的读取时使用读写文件,如下:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
/** * 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包执行没问题。
原文地址:https://www.cnblogs.com/dajunjun/p/11710969.html