Real Time Job Monitor in Java

One thing that has always annoyed me in MicroStrategy is that the Job Monitor requires a manual refresh.  Sometimes when debugging issues, request come through so quickly, I have to sit there and spam the refresh button to catch a glimpse.  I could always look at the execution log in Enterprise Manager, but that’s usually less efficient.  Today I’ll share a quick Java application I built the other day to poll the Job Monitor every second to create a Real Time Job Monitor.


DISCLAIMER: I only use this in specific debugging scenarios and I don’t leave it running all day.  I haven’t noticed any system performance impacts while it’s running, but every environment is different.  Please tread carefully when running this code against a Production environment.

I’m not a particularly accomplished Java Developer, so this sample (and other’s I’ll inevitably share) are mostly hacked together via trial and error.  There are probably much better ways to accomplish this task, and please share any suggestions in the comments.

This particular code is very basic, and surely lots of features and enhancements could be added to make this a more robust Real Time Job Monitor.  But for now, it serves as a Proof of Concept, and a handy program in a pinch.


package JobMonitor;

import java.awt.*;
import java.awt.event.*;
import java.util.TimerTask;
import java.util.Timer;
import javax.swing.*;
import java.util.Date;

import com.microstrategy.web.objects.WebIServerSession;
import com.microstrategy.web.objects.WebObjectsException;
import com.microstrategy.web.objects.WebObjectsFactory;
import com.microstrategy.web.objects.admin.monitors.EnumWebMonitorType;
import com.microstrategy.web.objects.admin.monitors.Job;
import com.microstrategy.web.objects.admin.monitors.JobResults;
import com.microstrategy.web.objects.admin.monitors.JobSource;
import com.microstrategy.webapi.EnumDSSXMLLevelFlags;

public class JobMonitor extends JApplet {
private static WebObjectsFactory factory = null;
private static WebIServerSession serverSession = null;
private static JobSource source = null;
JPanel jPanel1 = new JPanel();
JButton jButton1 = new JButton("");
  static JTextArea jText = new JTextArea("");

  //Construct the applet
  public JobMonitor() {
  }
  //Initialize the applet
  public void init() {
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
  //Component initialization
  private void jbInit() throws Exception {
    this.setSize(new Dimension(800,500));
    jPanel1.setLayout(null);
    jButton1.setText("Start Monitor");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButton1_actionPerformed(e);
      }
    });
    jButton1.setBounds(new Rectangle(700, 0, 100, 50));
    jButton1.setSize(100,50);
    this.getContentPane().add(jPanel1, BorderLayout.CENTER);
    jPanel1.add(jButton1);
    jText.setBounds(new Rectangle(0, 0, 700, 500));
    jText.setSize(700,500);
    jPanel1.add(jText);
  }
  //Get Applet information
  public String getAppletInfo() {
    return "Applet Information";
  }
  //Get parameter info
  public String[][] getParameterInfo() {
    return null;
  }

  void jButton1_actionPerformed(ActionEvent e) {

   getSession();
// Sends the request to Intelligence Server to retrieve job information
source.setLevel(EnumDSSXMLLevelFlags.DssXmlBrowsingLevel);
int delay = 1000;   // delay for 1 sec.
int period = 1000;  // repeat every sec.
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
       public void run() {
        try{
        JobResults results = source.getJobs();
 
String[] returns = new String[results.getCount()];

if (results.getCount() != 0) {
     for (int i=0; i < results.getCount(); i++) {
          Job singleJob = results.get(i);
          returns[i] = singleJob.getJobID() + ": " + singleJob.getUserName() + " - " + (singleJob.getJobStatus() == 1 ? "Executing" : singleJob.getJobStatus() == 2 ? "Waiting" : singleJob.getJobStatus() == 11 ? "Waiting for Children" : singleJob.getJobStatus()) + " - " + singleJob.getDescription() + " - " + singleJob.getDuration() + " sec";
     }
}

String ret="";
for (String s : returns) {
ret += s;
ret += "n";
}
if (ret.length() > 1)
say(ret.substring(0, ret.length()-2));
else
say("No Jobs Running");
  }catch(Exception ev){
ev.printStackTrace();
}
       }
   }, delay, period);

  }

  public static void getSession() {
System.out.println("Connecting to MicroStrategy");
factory = WebObjectsFactory.getInstance();
serverSession = factory.getIServerSession();
serverSession.setServerName("ISERVER");
serverSession.setServerPort(0);
serverSession.setProjectName("PROJECT"); //Required to connect, but doesn't impact this program.
serverSession.setLogin("USER");
serverSession.setPassword("PASSWORD");
try {
serverSession.getSessionID();
source = (JobSource) factory.getMonitorSource(EnumWebMonitorType.WebMonitorTypeJob);
say("Connected");
} catch (WebObjectsException ex) {
say("Error: " + ex.getMessage());
}
}

private static void say(String words) {
jText.setText(words);
//System.out.println(words);
}
}

You may also like...