Listing 1: Implementing the interface into an MBean.
import java.util.Date;
public class MyProcess implements MyProcessMBean
{
private String processName = null;
private String processMask = null;
private String processHost = null;
private int processPort = 0;
private boolean processRunning = false;
private Date startTime = null;
private Date stopTime = null;
private long sessionTime = 0;
private long runTime = 0;
private boolean noProcess = false;
private boolean standBy = false;
//empty constructor (default)
public MyProcess()
{
processName = "JMXTest";
processMask = "Mike's process for test JMX capabilities ...";
processHost = "localhost";
processPort = 5566;
processRunning = false;
noProcess = false;
standBy = false;
startTime = null;
stopTime = null;
sessionTime = 0;
runTime = 0;
}
//constructor with parameters
public MyProcess(String processName, String processMask,
String processHost, int processPort)
{
this.processName = processName;
this.processMask = processMask;
this.processHost = processHost;
this.processPort = processPort;
this.processRunning = false;
noProcess = false;
standBy = false;
startTime = null;
stopTime = null;
sessionTime = 0;
runTime = 0;
}
//getter method for processName
public String getProcessName()
{
if(!noProcess)
{
System.out.println("Process name: " + processName);
return processName;
} else {System.out.println
("The process was destroyed !"); return "";} }
//getter method for processMask
public String getProcessMask()
{
if(!noProcess)
{
System.out.println("Process mask: " + processMask);
return processMask;
} else {System.out.println
("The process was destroyed !"); return "";} }
//getter method for processHost
public String getProcessHost()
{
if(!noProcess)
{
System.out.println("Process host: " + processHost);
return processHost;
} else {System.out.println
("The process was destroyed !"); return "";} }
//getter method for standBy
public boolean getStandBy()
{ System.out.println
("Current stand by: " + standBy); return standBy; }
//getter method for noProcess
public boolean getNoProcess()
{ System.out.println
("Current process: " + noProcess); return noProcess; }
//is method for processRunning
public boolean isProcessRunning()
{
if(!noProcess)
{
if((standBy == true)&&(processRunning == true))
{ System.out.println
("The process is running (stand by mode is on) ..."); }
if((standBy == false)&&(processRunning == true))
{ System.out.println
("The process is running (stand by mode is off) ..."); }
if(processRunning == false)
{ System.out.println
("The process is not curently running ..."); }
return processRunning;
} else {System.out.println
("The process was destroyed !"); return false;}
}
//getter method for startTime
public Date getStartTime()
{
if(!noProcess)
{
if(startTime != null)
{
System.out.println("Start time: " + startTime);
return startTime;
} else { System.out.println
("The process wasn't started yet !"); return null; }
} else {System.out.println
("The process was destroyed !"); return null; } }
//getter method for stopTime
public Date getStopTime()
{
if(!noProcess)
{
if(stopTime != null)
{
System.out.println("Stop time: " + stopTime);
return stopTime;
} else { System.out.println
("There is no stop time available..."); return null; }
} else {System.out.println
("The process was destroyed !"); return null; } }
//getter method for runTime
public long getRunTime()
{
if(!noProcess)
{
if((processRunning == false)&&(stopTime !=
null)&&(startTime != null))
{
runTime = stopTime.getTime() - startTime.getTime();
System.out.println("Running time: " + runTime);
} else { System.out.println
("There is no run time available ..."); return 0; }
return runTime;
} else {System.out.println
("The process was destroyed !"); return 0; }
}
//getter method for sessionTime
public long getSessionTime()
{
if(!noProcess)
{
if((processRunning == true)&&(startTime != null))
{
sessionTime = System.currentTimeMillis()
- startTime.getTime();
System.out.println
("Last session time: " + sessionTime);
} else { System.out.println
("There is no session time available ..."); return 0; }
return sessionTime;
} else {System.out.println
("The process was destroyed !"); return 0; }
}
//getter method for processPort
public int getProcessPort()
{
if(!noProcess)
{
System.out.println("Process port: " + processPort);
return processPort;
} else {System.out.println
("The process was destroyed !"); return 0;} }
//setter method for processPort
public void setProcessPort(int processPort)
{
if(!noProcess)
{
System.out.println("Setting the new port ... ");
stopProcess();
this.processPort = processPort;
startProcess();
} else {System.out.println
("The process was destroyed !");}
}
//operation startProcess
public void startProcess()
{
if(!noProcess)
{
if(processRunning == true)
{ System.out.println
("The process is running ... "); }
else
{
System.out.println
("Starting process ... ");
System.out.println
("Process started successfully !");
startTime = new Date();
stopTime = null;
processRunning = true;
standBy = false;
}
} else {System.out.println
("The process was destroyed !");}
}
//operation stopProcess
public void stopProcess()
{
if(!noProcess)
{
if(processRunning == false)
{ System.out.println
("The process is not running ... "); }
else
{
System.out.println
("Stopping process ... ");
System.out.println
("Process stopped successfully !");
stopTime = new Date();
processRunning = false;
standBy = false;
}
} else {System.out.println
("The process was destroyed !");}
}
//operation haltProcess
public void haltProcess(Integer halt)
{
if(!noProcess)
{
if(processRunning == false)
{ System.out.println
("The process is not running ... "); }
else
{
System.out.println
("Halting process ... ");
if(halt == 0)
{
//stand by on
System.out.println
("Stand by on ...");
standBy = true;
}
if(halt == 1)
{
//stand by off
System.out.println
("Stand by off ...");
standBy = false;
}
if(halt == 2)
{
//low power
System.out.println("Low power ...");
}
if(halt == 3)
{
//restart the process
System.out.println("Restarting ...");
stopProcess();
startProcess();
}
}
} else {System.out.println
("The process was destroyed !");}
}
//operation destroyProcess
public void destroyProcess()
{
if(!noProcess)
{
System.out.println(" Process destroyed ... no attributes or operations
will be available from now !");
noProcess = true;
} else { System.out.println("There is no process available !"); }
}
}
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.