Skip to content

Instantly share code, notes, and snippets.

@luca3m
Created March 22, 2017 10:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luca3m/c6b35709adbc451429674a3a6e814e4f to your computer and use it in GitHub Desktop.
Save luca3m/c6b35709adbc451429674a3a6e814e4f to your computer and use it in GitHub Desktop.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package testjmx;
import java.lang.management.ManagementFactory;
import java.util.Random;
import javax.management.MBeanServer;
import javax.management.ObjectName;
/**
*
* @author luca
*/
public class TestJMX {
static public interface HelloMBean {
int getRequestCount();
double getRequestTime();
int getRequestErrors();
int getRequestOK();
int getDBErrors();
}
static public class Hello implements HelloMBean {
private final Random r;
public Hello() {
r = new Random();
}
@Override
public int getRequestCount() {
return r.nextInt(400);
}
@Override
public double getRequestTime() {
return r.nextDouble()*100.0;
}
@Override
public int getRequestErrors() {
return r.nextInt(400);
}
@Override
public int getRequestOK() {
return r.nextInt(400);
}
@Override
public int getDBErrors() {
return r.nextInt(400);
}
}
/**
* @param args the command line arguments
* @throws java.lang.InterruptedException
*/
public static void main(String[] args) throws Exception {
Random r = new Random();
final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
if(args.length >= 1) {
int beans = Integer.parseInt(args[0]);
System.err.printf("Registering %d beans\n", beans);
for(int j = 0; j < beans; ++j) {
final ObjectName name = new ObjectName(String.format("com.example:type=Hello,n=test%d", j));
final HelloMBean hello = new Hello();
mbs.registerMBean(hello, name);
}
}
while (true) {
for(int j = 0; j < 10; ++j) {
byte[] t = new byte[(r.nextInt(6)+1) << 20];
}
System.gc();
Thread.sleep((r.nextInt(10)+1)*100);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment