📄 timeragent.java
字号:
package book.jmx.examples;
import javax.management.*;
import javax.management.timer.*;
import java.util.Date;
import java.util.List;
public class TimerAgent {
private MBeanServer server = null;
private ObjectName timer = null;
private ObjectName receiver = null;
public void run() {
// Find an agent from this JVM. Null argument will
// return a list of all MBeanServer instances.
List list = MBeanServerFactory.findMBeanServer(null);
server = (MBeanServer)list.iterator().next();
try {
// register the timer and receiver mbeans
timer = new ObjectName("service:name=timer");
receiver = new ObjectName("example:name=listener," +
"source=timer");
server.registerMBean(new Timer(), timer);
server.registerMBean(new TimerReceiver(), receiver);
// start the timer service
server.invoke(timer, "start", null, null);
// add scheduled notification to five seconds
// past the registration
Date date = new Date(System.currentTimeMillis() +
Timer.ONE_SECOND * 5);
Integer id = (Integer)server.invoke(
timer, // MBean
"addNotification", // operation
new Object[] { // arguments:
"timer.notification", // type
"Scheduled notification.", // message
null, // user data
date
},
new String[] { // signature
String.class.getName(),
String.class.getName(),
Object.class.getName(),
Date.class.getName()
}
);
// add listener to the timer
NotificationFilter filter = new TimerFilter(id);
server.addNotificationListener(
timer, receiver, filter, null);
}
catch (JMException e) {
e.printStackTrace();
}
}
//
// Notification filter implementation.
//
class TimerFilter implements NotificationFilter {
private Integer id = null;
TimerFilter(Integer id) {
this.id = id;
}
public boolean isNotificationEnabled(Notification n) {
if (n.getType().equals("timer.notification")) {
TimerNotification notif = (TimerNotification)n;
if (notif.getNotificationID().equals(id))
return true;
}
return false;
}
}
//
// Main method for the client. This will instantiate
// an agent in the JVM.
//
public static void main(String[] args) {
MBeanServer server =
MBeanServerFactory.createMBeanServer();
new TimerAgent().run();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -