Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2011, Red Hat, Inc. and/or its affiliates,
   * and individual contributors as indicated by the @author tags.
   * See the copyright.txt in the distribution for a
   * full listing of individual contributors.
   * This copyrighted material is made available to anyone wishing to use,
   * modify, copy, or redistribute it subject to the terms and conditions
   * of the GNU Lesser General Public License, v. 2.1.
  * This program is distributed in the hope that it will be useful, but WITHOUT A
  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
  * You should have received a copy of the GNU Lesser General Public License,
  * v.2.1 along with this distribution; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  * MA  02110-1301, USA.
  *
  * (C) 2011,
  * @author JBoss, by Red Hat.
  */
 package com.arjuna.ats.arjuna.tools.stats;
 
 
 
 
 
 public class TxPerfGraph extends javax.swing.JPanel { //JFrame {
     private final static int NUMBER_OF_SAMPLES = 100;
 
     private int POLL_PERIOD = 4000; // in ms
 
     public final static int NUMBER_OF_TRANSACTIONS_SERIES = 0;
     public final static int NUMBER_OF_INFLIGHT_SERIES = 1;
     public final static int NUMBER_OF_COMMITTED_SERIES = 2;
     public final static int NUMBER_OF_ABORTED_SERIES = 3;
     public final static int NUMBER_OF_HEURISTICS_SERIES = 4;
     public final static int NUMBER_OF_NESTED_SERIES = 5;
     public final static int NUMBER_OF_TIMEDOUT_SERIES = 6;
 
     private final static String[] SERIES_LABELS = {
         "Transactions Created",
         "In Flight Transactions",
         "Committed Transactions",
         "Aborted Transactions",
         "Heuristics Raised",
         "Nested Transactions Created",
         "Timed Out Transactions",
     };
 
     private final static String[] PIE_CHART_LABELS = {
         "Nested",
         "Heuristic",
         "Committed",
         "Aborted",
     };
 
     private final static int[] PIE_CHART_SERIES = new int[] {
          ,
          ,
          ,
          ,
     };
 
     private static final TimeZone GMT = TimeZone.getTimeZone("GMT");
     private static SimpleDateFormat timeFormatter = new SimpleDateFormat("HH:mm:ss");
 
     private MBeanServerConnection server;
     private JFrame frame;
     private TxStatsMBean txMBean;
    private XYSeries[] _dataSeries = new XYSeries[7];
    private XYSeriesCollection _tsDS[] = new XYSeriesCollection[7];
    private long counter = 0L;
    private DefaultPieDataset pieDS;
 //   TimerTask timerTask;
 //   Timer timer = new Timer("TxPerf Sampling thread");
Creates new form TxPerfGraph
    public TxPerfGraph(JFrame frame) {
        this. = frame;
        .setTimeZone();
        initComponents();
        for (int count = 0; count < .count++) {
            [count] = new XYSeries([count]);//, Second.class);
            //_dataSeries[count].setMaximumItemCount(NUMBER_OF_SAMPLES);
            [count] = new XYSeriesCollection([count]);
        }
        .setDataset([]);
        .setSubtitle("");
 //       DateAxis xAxis = new DateAxis("Time (hh:mm) Zulu", GMT);
 //       xAxis.setAutoRange(true);
 //       xAxis.setTickUnit(new DateTickUnit(DateTickUnit.MINUTE, 60, timeFormatter));
 //       xAxis.setVerticalTickLabels(true);
 //       xAxis.setDateFormatOverride(timeFormatter);
        .setSelected(true);
         = new DefaultPieDataset();
        .setDataset();
        .setTitle("All Transactions");
        .setSubtitle("(during last time slices)");
			"Select the number of (" +  + "ms) time slices over which to show the pie chart");
        SpinnerModel sm1 = new SpinnerNumberModel( / 1000, 1, 10000, 1);
        SpinnerModel sm2 = new SpinnerNumberModel(, 10, 1000, 1);
        .setModel(sm1);
        .setModel(sm2);
        .setVisible(false);
        .setVisible(false);
        .setSelected(false);
        .setVisible(false);
         = new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                newSwingWorker().execute();
            }
        };
        .setSelectedIndex(1);
    }
    public void setMBeanServerConnection(MBeanServerConnection mbs) {
        this. = mbs;
        try {
             = JMX.newMBeanProxy(,
                    new ObjectName("jboss.jta:name=TransactionStatistics"), TxStatsMBean.class);
             = JMX.newMBeanProxy(,
                    new ObjectName("jboss.jta:name=CoordinatorEnvironmentBean"),
                        CoordinatorEnvironmentBeanMBean.class);
            .setEnableStatistics(true);
            .setSelected(true);
//        } catch (javax.management.InstanceNotFoundException e) {
//            System.out.println("Transaction statistics MBean is not available.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private void resetStats() {
        //int cindex = _dataSeries[0].getIndex(now);
    }
    private void sample() {
        try {
             += 1;
            long [] stats = new long[7];
            stats[] = .getNumberOfHeuristics();
            for (int i = 0; i < 7; i++) {
                [i].add(stats[i]);
            }
            if ( > 1) {
                int cindex = [0].indexOf();
                int slices = .getValue();
                int lb = cindex < slices ? 0 : cindex - slices;
                for (int i = 0; i < .i++) {
                    XYSeries ts = [[i]];
                    Number n1 = ts.getDataItem(cindex).getY();
                    Number n2 = ts.getDataItem(lb).getY();
                    .setValue([i], n1.longValue() - n2.longValue());
                }
                .setSubtitle("(during last " + // (cindex - lb + 1) * POLL_PERIOD / 1000 + " seconds) - " +
                        (cindex - lb + 1) + " poll intervals");
            }
        } catch (Exception e) {
            ..println("MBean property failure: " + e);
        }
    }

    
This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this method is always regenerated by the Form Editor.
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
         = new javax.swing.ButtonGroup();
         = new javax.swing.ButtonGroup();
         = new javax.swing.JPanel();
         = new javax.swing.JPanel();
         = new javax.swing.JLabel();
         = new javax.swing.JRadioButton();
         = new javax.swing.JRadioButton();
         = new javax.swing.JRadioButton();
         = new javax.swing.JRadioButton();
         = new javax.swing.JRadioButton();
         = new javax.swing.JRadioButton();
         = new javax.swing.JRadioButton();
         = new javax.swing.JTabbedPane();
         = new javax.swing.JPanel();
         = new javax.swing.JButton();
         = new javax.swing.JSpinner();
         = new javax.swing.JButton();
         = new javax.swing.JCheckBox();
         = new javax.swing.JButton();
         = new javax.swing.JSpinner();
         = new org.jfree.beans.JLineChart();
         = new org.jfree.beans.JPieChart();
         = new javax.swing.JPanel();
         = new javax.swing.JLabel();
         = new javax.swing.JSlider();
         = new javax.swing.JMenuBar();
        .setText("Select Series");
        .setText("Transactions");
        .setToolTipText("Show all transactions");
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                allTxnBtnActionPerformed(evt);
            }
        });
        .setText("In Flight");
        .setToolTipText("Show transactions that have not yet been committed/aborted");
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                inFlightTxnBtnActionPerformed(evt);
            }
        });
        .setText("Committed");
        .setToolTipText("Show successfully committed transactions");
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                committedTxnBtnActionPerformed(evt);
            }
        });
        .setText("Aborted");
        .setToolTipText("Show stats for rolled back transactions");
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                abortedTxnBtnActionPerformed(evt);
            }
        });
        .setText("Heuristics");
        .setToolTipText("Show stats for transactions that terminated with a heuristic outcome");
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                heuristicTxnBtnActionPerformed(evt);
            }
        });
        .setText("Nested");
        .setToolTipText("Stats for nested transactions");
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nestedTxnBtnActionPerformed(evt);
            }
        });
        .setText("Timed Out");
        .setToolTipText("Stats for transactions which exceeded there time to live");
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                timedoutTxnBtnActionPerformed(evt);
            }
        });
        javax.swing.GroupLayout btnPanelLayout = new javax.swing.GroupLayout();
        .setLayout(btnPanelLayout);
        btnPanelLayout.setHorizontalGroup(
            btnPanelLayout.createParallelGroup(....)
            .addGroup(btnPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(btnPanelLayout.createParallelGroup(....)
                    .addGroup(btnPanelLayout.createSequentialGroup()
                        .addGroup(btnPanelLayout.createParallelGroup(....)
                            .addComponent()
                            .addComponent()
                            .addComponent()
                            .addComponent())
                        .addGap(35, 35, 35)
                        .addGroup(btnPanelLayout.createParallelGroup(....)
                            .addComponent()
                            .addComponent()
                            .addComponent()))
                    .addComponent())
                .addContainerGap(188, .))
        );
        btnPanelLayout.setVerticalGroup(
            btnPanelLayout.createParallelGroup(....)
            .addGroup(....btnPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent()
                .addGroup(btnPanelLayout.createParallelGroup(....)
                    .addGroup(btnPanelLayout.createSequentialGroup()
                        .addComponent()
                        .addPreferredGap(....)
                        .addComponent()
                        .addPreferredGap(....)
                        .addComponent())
                    .addGroup(btnPanelLayout.createSequentialGroup()
                        .addComponent()
                        .addPreferredGap(....)
                        .addComponent()
                        .addPreferredGap(....)
                        .addComponent()
                        .addPreferredGap(....)
                        .addComponent())))
        );
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                chartsPaneStateChanged(evt);
            }
        });
        .setText("Reset Stats");
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                resetStatsBtnActionPerformed(evt);
            }
        });
        .setToolTipText("Change polling interval (in seconds)");
        .setText("Set Poll Interval");
        .setToolTipText("Change polling interval (in seconds)");
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pollIntervalBtnActionPerformed(evt);
            }
        });
        .setSelected(true);
        .setText("Enable Statistics");
        .setToolTipText("Stop data collection by disabling the stats MBean in the target JVM");
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                enableStatsCBActionPerformed(evt);
            }
        });
        .setText("Set Sample Size");
        .setToolTipText("Change polling interval (in seconds)");
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sampleSizeBtn1ActionPerformed(evt);
            }
        });
        .setToolTipText("Change Number of Data Samples ");
        javax.swing.GroupLayout configTabLayout = new javax.swing.GroupLayout();
        .setLayout(configTabLayout);
        configTabLayout.setHorizontalGroup(
            configTabLayout.createParallelGroup(....)
            .addGroup(configTabLayout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(configTabLayout.createParallelGroup(....)
                    .addComponent(..., 246, .)
                    .addGroup(....configTabLayout.createSequentialGroup()
                        .addComponent(..., 120, .)
                        .addPreferredGap(....)
                        .addComponent(..., 114, ...))
                    .addGroup(configTabLayout.createSequentialGroup()
                        .addGap(2, 2, 2)
                        .addGroup(configTabLayout.createParallelGroup(....)
                            .addComponent()
                            .addGroup(configTabLayout.createSequentialGroup()
                                .addComponent()
                                .addPreferredGap(....)
                                .addComponent(..., 114, ...)))))
                .addGap(149, 149, 149))
        );
        configTabLayout.setVerticalGroup(
            configTabLayout.createParallelGroup(....)
            .addGroup(configTabLayout.createSequentialGroup()
                .addGap(38, 38, 38)
                .addComponent()
                .addGap(29, 29, 29)
                .addGroup(configTabLayout.createParallelGroup(....)
                    .addComponent()
                .addGroup(configTabLayout.createParallelGroup(....)
                    .addComponent()
                .addComponent()
                .addContainerGap(47, .))
        );
        .addTab("Settings");
        .setXAxisLabel("Number of Poll Intervals");
        .setXAxisScale(org.jfree.beans.AxisScale.INTEGER);
        .setYAxisLabel("Number of Txns");
        .setYAxisScale(org.jfree.beans.AxisScale.INTEGER);
        javax.swing.GroupLayout chart1Layout = new javax.swing.GroupLayout();
        .setLayout(chart1Layout);
        chart1Layout.setHorizontalGroup(
            chart1Layout.createParallelGroup(....)
            .addGap(0, 418, .)
        );
        chart1Layout.setVerticalGroup(
            chart1Layout.createParallelGroup(....)
            .addGap(0, 235, .)
        );
        .addTab("Transactions"null"View Transaction Statistics");
        javax.swing.GroupLayout txnPieChartLayout = new javax.swing.GroupLayout();
        .setLayout(txnPieChartLayout);
        txnPieChartLayout.setHorizontalGroup(
            txnPieChartLayout.createParallelGroup(....)
            .addGap(0, 418, .)
        );
        txnPieChartLayout.setVerticalGroup(
            txnPieChartLayout.createParallelGroup(....)
            .addGap(0, 235, .)
        );
        .addTab("Pie Chart"null"View Transactions as a Pie Chart");
        .setText("Time Slices");
        .setToolTipText("Select the number of polling periods to use for populating the pie chart");
        .setPaintLabels(true);
        .setPaintTicks(true);
        .setToolTipText("Select the number of time slices over which to show the pie chart");
        .setValue(30);
        javax.swing.GroupLayout periodSelectPanelLayout = new javax.swing.GroupLayout();
        .setLayout(periodSelectPanelLayout);
        periodSelectPanelLayout.setHorizontalGroup(
            periodSelectPanelLayout.createParallelGroup(....)
            .addGroup(periodSelectPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent()
        );
        periodSelectPanelLayout.setVerticalGroup(
            periodSelectPanelLayout.createParallelGroup(....)
            .addGroup(periodSelectPanelLayout.createSequentialGroup()
                .addGroup(periodSelectPanelLayout.createParallelGroup(....)
                    .addGroup(periodSelectPanelLayout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(..., 36, .))
                .addContainerGap())
        );
        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout();
        .setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(....)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(....)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(....)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(14, .))
        );
        add();
        add();
    }// </editor-fold>//GEN-END:initComponents
    private void btnActionPerformed(int series) {
        .setDataset([series]);
        .setTitle([series]);
    }
    private void allTxnBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_allTxnBtnActionPerformed
    }//GEN-LAST:event_allTxnBtnActionPerformed
    private void inFlightTxnBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_inFlightTxnBtnActionPerformed
    }//GEN-LAST:event_inFlightTxnBtnActionPerformed
    private void heuristicTxnBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_heuristicTxnBtnActionPerformed
    }//GEN-LAST:event_heuristicTxnBtnActionPerformed
    private void committedTxnBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_committedTxnBtnActionPerformed
    }//GEN-LAST:event_committedTxnBtnActionPerformed
    private void abortedTxnBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_abortedTxnBtnActionPerformed
    }//GEN-LAST:event_abortedTxnBtnActionPerformed
    private void nestedTxnBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nestedTxnBtnActionPerformed
    }//GEN-LAST:event_nestedTxnBtnActionPerformed
    private void timedoutTxnBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_timedoutTxnBtnActionPerformed
    }//GEN-LAST:event_timedoutTxnBtnActionPerformed
    private void chartsPaneStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_chartsPaneStateChanged
        int tab = .getSelectedIndex();
        .setVisible(false);
        .setVisible(false);
        switch (tab) {
            case 1: .setVisible(true); break;
            case 2: .setVisible(true); break;
            default:
                break;
        }
    }//GEN-LAST:event_chartsPaneStateChanged
    private void pollIntervalBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pollIntervalBtnActionPerformed
        Integer v = (Integer.getValue();
         = v * 1000;
        startPolling();
    }//GEN-LAST:event_pollIntervalBtnActionPerformed
    private void resetStatsBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetStatsBtnActionPerformed
        resetStats();
}//GEN-LAST:event_resetStatsBtnActionPerformed
    private void enableStatsCBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_enableStatsCBActionPerformed
    }//GEN-LAST:event_enableStatsCBActionPerformed
    private void sampleSizeBtn1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sampleSizeBtn1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_sampleSizeBtn1ActionPerformed
    private void startPolling() {
       // timer.cancel();       
        //timer.schedule(timerTask, 0, POLL_PERIOD);
        if (.isRunning())
            .restart();
        else
            .start();
    }
    private static void createAndShowGUI(TxPerfGraph perfPanel) {
        JFrame frame = perfPanel.getFrame();
        // Create and set up the window.
        // Create and set up the content pane.
        JComponent contentPane = (JComponentframe.getContentPane();
        contentPane.add(perfPanel.);
        contentPane.setOpaque(true); //content panes must be opaque
        contentPane.setBorder(new EmptyBorder(12, 12, 12, 12));
        frame.setContentPane(contentPane);
        // Display the window.
        frame.pack();
        frame.setVisible(true);
    }
    private static MBeanServerConnection connect(String hostnameint port) {
        String urlPath = "/jndi/rmi://" + hostname + ":" + port + "/jmxrmi";
        MBeanServerConnection server = null;
        try {
            JMXServiceURL url = new JMXServiceURL("rmi""", 0, urlPath);
            JMXConnector jmxc = JMXConnectorFactory.connect(url);
            server = jmxc.getMBeanServerConnection();
        } catch (MalformedURLException e) {
        } catch (IOException e) {
            ..println("Unable to get an MBean Server connection: " + e.getMessage());
            System.exit(1);
        }
        return server;
    }
    /*
     * SwingWorker for updating the TxPerf tab
     */
    public SwingWorker<XYSeries[], ObjectnewSwingWorker() {
        return new Worker();
    }
    void dispose() {
//        if (coordMBean != null && disableStatsOnDispose.isSelected())
//            coordMBean.setEnableStatistics(false);
    }
    class Worker extends SwingWorker<XYSeries[], Object> {
        @Override
        protected XYSeries[] doInBackground() throws Exception {
            sample();
            return ;
        }
    }
    public static void main(String args[]) throws InterruptedExceptionInvocationTargetException {
        final TxPerfGraph graphPanel = new TxPerfGraph(new JFrame("TxPerf"));
        String hostname = "localhost";
        int port = 1090;
        if (args.length > 0) {
            String[] opts = args[0].split(":");
            hostname = opts[0];
            if (opts.length > 1)
                port = Integer.parseInt(opts[1]);
        }
        ..println("Connecting to MBeanServer on endpoint " + hostname + ":" + port);
        MBeanServerConnection server = connect(hostnameport);
        graphPanel.setMBeanServerConnection(server);
        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                createAndShowGUI(graphPanel);
            }
        });
        graphPanel.startPolling();
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel btnPanel;
    private org.jfree.beans.JLineChart chart1;
    private javax.swing.JPanel configTab;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JButton resetStatsBtn;
    private org.jfree.beans.JPieChart txnPieChart;
    // End of variables declaration//GEN-END:variables

    

Returns:
the frame
    public JFrame getFrame() {
        return ;
    }
    private void setDefaultCloseOperation(int operation) {
        .setDefaultCloseOperation(operation);
    }
    private Container getContentPane() {
        return .getContentPane();
    }
    private void setJMenuBar(JMenuBar menuBar) {
        .setJMenuBar(menuBar);
    }
    private void pack() {
    }