ActiveMq队列数据监控器----1小时1个Swing小程序

需求:

压力测试,无人监守情况下,记录ActiveMq队列数据情况,监视队列是否存在积压。

思路:

ActiveMq的Queue Views 提供Xml形式查看方式,URL:http://172.16.9.38:8161/admin/xml/queues.jsp

利用Java解析该地址,获取到队列数据,写入日志。

实现:

涉及Jar:

jdom-1.1.3.jar

logback-classic-1.0.0.jar

logback-core-1.0.0.jar

slf4j-api-1.6.1.jar

URL解析类

package com.read;

import org.jdom.*;
import org.jdom.input.SAXBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: wangq
 * Date: 12-12-11
 * Time: 下午4:31
 * To change this template use File | Settings | File Templates.
 */
public class UrlStyle {
    private static Document document;
    private final static Logger logger = LoggerFactory.getLogger(UrlStyle.class);
    private boolean flag = true;
    private String xmlUrl;
    private int warnNum;
    private int errorNum;

    public void init() throws IOException, JDOMException {
        URL url = new URL(xmlUrl);
        SAXBuilder builder = new SAXBuilder();
        document = builder.build(url);
    }

    public void read() {
        if (warnNum == 0) {
            warnNum = 100;
        }
        if (errorNum == 0) {
            errorNum = 500;
        }
        Element element = document.getRootElement();
        List<Element> list = element.getChildren();
        for (Element child : list) {
            StringBuilder sb = new StringBuilder();
            Attribute name = child.getAttribute("name");
            Element cEle = child.getChild("stats");
            Attribute size = cEle.getAttribute("size");
            Attribute consumerCount = cEle.getAttribute("consumerCount");
            Attribute enqueueCount = cEle.getAttribute("enqueueCount");
            Attribute dequeueCount = cEle.getAttribute("dequeueCount");
            String queName = name.getValue();
            if (queName.length() < 23) {
                int bc = 23 - queName.length();
                for (int i = 0; i < bc; i++) {
                    queName = " " + queName;
                }
            }
            sb.append(queName + " ->" + "  size:" + size.getValue() + "  consumerCount:" + consumerCount.getValue()
                    + "   enqueueCount:" + enqueueCount.getValue() + "  dequeueCount:" + dequeueCount.getValue() + ""
            );
            int count = 0;
            try {
                count = size.getIntValue();
            } catch (DataConversionException e) {
                e.printStackTrace();
            }
            if (count < errorNum && count > warnNum) {
                logger.warn(sb.toString());
            } else if (count > warnNum) {
                logger.error(sb.toString());
            } else {
                logger.info(sb.toString());
            }
        }
    }

    public void start(String xmlUrl, int warnNum, int errorNum, int pl) throws JDOMException, IOException {
        this.xmlUrl = xmlUrl;
        this.warnNum = warnNum;
        this.errorNum = errorNum;
        flag = true;
        init();
        while (flag) {
            read();
            try {
                Thread.sleep(pl * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void stop() {
        flag = false;
    }
}

UI类:

package com.ui;

import com.read.UrlStyle;
import com.util.FontUtil;
import com.util.MesBox;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * Created with IntelliJ IDEA.
 * User: wangq
 * Date: 12-12-11
 * Time: 下午5:31
 * To change this template use File | Settings | File Templates.
 */
public class MqLog extends JFrame {
    private JLabel urlLable;
    private JTextField urlText;
    private JLabel plLable;
    private JTextField plText;
    private JLabel warnLable;
    private JTextField warnText;
    private JLabel errorLable;
    private JTextField errorText;
    private JLabel statusLable;
    protected JButton startButton;
    protected JButton stopButton;
    private UrlStyle style = new UrlStyle();

    public MqLog() {
        super();
        this.setSize(680, 350);
        this.setResizable(false);
        this.setTitle("ActiveMQ队列监视器1.0");
        Container c = this.getContentPane();
        c.setLayout(null);
        JPanel jPanel=new JPanel();
        jPanel.setSize(680, 350);
        jPanel.setLayout(null);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        urlLable = new JLabel("Queue Views URL:", JLabel.RIGHT);
        urlLable.setFont(new Font(FontUtil.getHeiFont(), Font.BOLD, 13));
        urlLable.setBounds(5, 5, 120, 25);
        jPanel.add(urlLable);
        urlText = new JTextField("http://localhost:8161/admin/xml/queues.jsp");
        urlText.setBounds(140, 5, 500, 25);
        urlText.setFont(new Font(FontUtil.getHeiFont(), Font.BOLD, 13));
        jPanel.add(urlText);

        plLable = new JLabel("记录频率(S):", JLabel.RIGHT);
        plLable.setBounds(5, 33, 120, 25);
        plLable.setFont(new Font(FontUtil.getHeiFont(), Font.BOLD, 13));
        jPanel.add(plLable);
        plText = new JTextField("2");
        plText.setFont(new Font(FontUtil.getHeiFont(), Font.BOLD, 13));
        plText.setBounds(140, 33, 150, 25);
        jPanel.add(plText);

        warnLable = new JLabel("积压黄色告警数:", JLabel.RIGHT);
        warnLable.setBounds(5, 61, 120, 25);
        warnLable.setFont(new Font(FontUtil.getHeiFont(), Font.BOLD, 13));
        jPanel.add(warnLable);
        warnText = new JTextField("100");
        warnText.setBounds(140, 61, 150, 25);
        warnText.setFont(new Font(FontUtil.getHeiFont(), Font.BOLD, 13));
        jPanel.add(warnText);

        errorLable = new JLabel("积压红色警告数:", JLabel.RIGHT);
        errorLable.setBounds(5, 89, 120, 25);
        errorLable.setFont(new Font(FontUtil.getHeiFont(), Font.BOLD, 13));
        jPanel.add(errorLable);
        errorText = new JTextField("500");
        errorText.setBounds(140, 89, 150, 25);
        errorText.setFont(new Font(FontUtil.getHeiFont(), Font.BOLD, 13));
        jPanel.add(errorText);

        statusLable = new JLabel("运行状态: 未运行", JLabel.LEFT);
        statusLable.setBounds(10, 220, 300, 25);
        statusLable.setFont(new Font(FontUtil.getHeiFont(), Font.BOLD, 13));
        jPanel.add(statusLable);

        jPanel.add(getStartJButton());
        jPanel.add(getStopJButton());
        c.add(jPanel);
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    JFrame.setDefaultLookAndFeelDecorated(true);
                    JDialog.setDefaultLookAndFeelDecorated(true);
                    UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");//nimbus
                    //下面语句实现:将外观设置为系统外观.
//                    UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                } catch (Exception e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
            }
        });
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(NORMAL);
            }
        });



    }

    private JButton getStartJButton() {
        if (startButton == null) {
            startButton = new JButton();
            startButton.setBounds(150, 140, 130, 40);
            startButton.setFont(new Font(FontUtil.getHeiFont(), Font.BOLD, 14));
            startButton.setFocusPainted(false);
            startButton.setFocusable(false);
            startButton.setMargin(new Insets(0, 0, 0, 0));
            startButton.setText("开始");
        }
        startButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                final String url = urlText.getText();
                if (null == url || "".equals(url)) {
                    MesBox.warn("请填写URL");
                    return;
                } else {
                    startButton.setEnabled(false);
                    stopButton.setEnabled(true);
                    statusLable.setText("运行状态: 正在运行...............");
                    final int warn = Integer.valueOf(warnText.getText());
                    final int error = Integer.valueOf(errorText.getText());
                    final int pl = Integer.valueOf(plText.getText());
//                    SwingUtilities.invokeLater();
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                style.start(url, warn, error, pl);
                            } catch (Exception e1) {
                                MesBox.warn("启动异常,请检查URL地址是否正确并确保ActiveMq已启动");
                                style.stop();
                                startButton.setEnabled(true);
                                stopButton.setEnabled(false);
                                statusLable.setText("运行状态: 未运行");
                            }
                        }
                    }).start();
                }
            }
        });
        return startButton;
    }

    private JButton getStopJButton() {
        if (stopButton == null) {
            stopButton = new JButton();
            stopButton.setBounds(320, 140, 130, 40);
            stopButton.setText("停止");
            stopButton.setFocusPainted(false);
            stopButton.setMargin(new Insets(0, 0, 0, 0));
            stopButton.setFont(new Font(FontUtil.getHeiFont(), Font.BOLD, 14));
            stopButton.setEnabled(false);
        }
        stopButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                style.stop();
                startButton.setEnabled(true);
                stopButton.setEnabled(false);
                statusLable.setText("运行状态: 未运行");
            }
        });
        return stopButton;
    }

    public static void main(String[] args) {
        new MqLog().setVisible(true);
    }
}

三个Util类,略。

伸手党,请直奔附件。

https://files.cnblogs.com/zhishan/ActiveMq%E9%98%9F%E5%88%97%E7%9B%91%E6%8E%A7%E5%99%A8.rar