October 31, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Integrating your Java Application with Existing Network Management Solutions with JMX

  • July 1, 2004
  • By Olexiy & Alexander Prokhorenko
  • Send Email »
  • More Articles »

Layer of Distributed Services

Last but not least from the considerable number of layers is the layer of management distributed services. This layer provides the interface that is being used by remote tools to interact with agents.

So, we have very briefly considered the structure of the JMX framework, have received some information on what is necessary and what its basic functions are. Next, we will disassemble the practical use of this technology with a concrete example.

Example of a "controled" application

As an example, we shall consider the ordinary GUI Java application: an applet. This applet will contain only one element, JLabel. As a matter of fact, this applet represents a non-functional window with a text label. With the help of JMX, we will have an opportunity to operate the contents of this label. The appearance of our extremely simple applet is shown below:

All code is divided into two parts (two classes): MainApp.java is a class that represents a launching pad of our window and that contains the main() static method; and CounterFrame.java is a class of frame in which, except for elements of a window, a private property counterValue of int type is also defined; it is responsible for storing the current counter value that is displayed in label in our frame. The setCounterValue() and getCounterValue() methods serve for setting and getting the counter's value, and the incCounterValue() and decCounterValue() methods are intended for increasing and decreasing of counter's value by 1. All these methods, except for getCounterValue(), automatically update the contents of our label with the counter.

You can examine source code:

// MainApp.java
import java.awt.*;
import javax.swing.UIManager;

public class MainApp {
   private boolean packFrame = false;

   public MainApp() {
      CounterFrame frame = new CounterFrame();
      if (packFrame) 
         frame.pack();
      else 
         frame.validate();

      Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
      Dimension frameSize = frame.getSize();
      if (frameSize.height > screenSize.height) 
         frameSize.height = screenSize.height;
      if (frameSize.width > screenSize.width)
         frameSize.width = screenSize.width;
      frame.setLocation((screenSize.width - frameSize.width) / 2, 
                        (screenSize.height - frameSize.height) / 2);
      frame.setVisible(true);
  }

  public static void main(String[] args) {
     try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
     } catch (Exception e) {
        e.printStackTrace();
     }
     new MainApp();
  }
}

// CounterFrame.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class CounterFrame extends JFrame implements CounterFrameMBean {
   private JPanel contentPane;
   private JLabel jLabel1 = new JLabel();
   private int counterValue = 0; 

   public CounterFrame() {
      enableEvents(AWTEvent.WINDOW_EVENT_MASK);
      try {
         init();
      } catch (Exception e) {
         e.printStackTrace();
      }
   }

   private void init() throws Exception {
      contentPane = (JPanel) this.getContentPane();
      contentPane.setLayout(new BorderLayout());
      this.setSize(new Dimension(400, 300));
      this.setTitle("Example");
      jLabel1.setFont(new java.awt.Font("Dialog", 0, 24));
      jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
      setCounterValue(new Integer(0));
      this.getContentPane().add(jLabel1, BorderLayout.CENTER);
   }

   protected void processWindowEvent(WindowEvent e) {
      super.processWindowEvent(e);
      if (e.getID() == WindowEvent.WINDOW_CLOSING) {
         System.exit(0);
      }
   }

   public Integer getCounterValue() {
      return new Integer(counterValue);
   }

   public void setCounterValue(Integer value) {
      counterValue = value.intValue();
      jLabel1.setText(String.valueOf(getCounterValue().intValue()));
   }

   public void incCounterValue() {
      counterValue++;
      jLabel1.setText(String.valueOf(getCounterValue().intValue()));
   }

   public void decCounterValue() {
      counterValue--;
      jLabel1.setText(String.valueOf(getCounterValue().intValue()));
   }
}




Page 2 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel