From: Fredrik Tolf Date: Mon, 28 Mar 2011 19:29:32 +0000 (+0200) Subject: Added some JMX support. X-Git-Url: http://git.dolda2000.com/gitweb/?a=commitdiff_plain;h=3299417335913b0886ca80ac0de62d76fc741dbe;p=jsvc.git Added some JMX support. --- diff --git a/src/dolda/jsvc/ThreadContext.java b/src/dolda/jsvc/ThreadContext.java index b6d9677..8e544d3 100644 --- a/src/dolda/jsvc/ThreadContext.java +++ b/src/dolda/jsvc/ThreadContext.java @@ -143,6 +143,10 @@ public class ThreadContext extends ThreadGroup { return(ctx.worker(root, req, workers, "Worker thread " + reqs++)); } + public long requests() { + return(reqs); + } + private Responder bootstrap(final Class bootclass) { final Throwable[] err = new Throwable[1]; final Responder[] res = new Responder[1]; diff --git a/src/dolda/jsvc/jmx/Context.java b/src/dolda/jsvc/jmx/Context.java new file mode 100644 index 0000000..2f062f6 --- /dev/null +++ b/src/dolda/jsvc/jmx/Context.java @@ -0,0 +1,30 @@ +package dolda.jsvc.jmx; + +import javax.management.*; +import java.util.*; +import dolda.jsvc.*; + +public class Context implements ContextMBean { + public final ObjectName name; + private final ThreadContext b; + + public Context(ThreadContext ctx) { + this.b = ctx; + Hashtable info = new Hashtable(); + info.put("type", "Context"); + info.put("name", getname()); + try { + name = new ObjectName("dolda.jsvc", info); + } catch(MalformedObjectNameException e) { + throw(new RuntimeException(e)); + } + } + + public long getrequests() { + return(b.requests()); + } + + public String getname() { + return(b.server().name()); + } +} diff --git a/src/dolda/jsvc/jmx/ContextMBean.java b/src/dolda/jsvc/jmx/ContextMBean.java new file mode 100644 index 0000000..4dcdee9 --- /dev/null +++ b/src/dolda/jsvc/jmx/ContextMBean.java @@ -0,0 +1,6 @@ +package dolda.jsvc.jmx; + +public interface ContextMBean { + public String getname(); + public long getrequests(); +} diff --git a/src/dolda/jsvc/scgi/DSContext.java b/src/dolda/jsvc/scgi/DSContext.java index c726887..09fe1a9 100644 --- a/src/dolda/jsvc/scgi/DSContext.java +++ b/src/dolda/jsvc/scgi/DSContext.java @@ -9,6 +9,7 @@ public class DSContext extends SimpleContext { public final long mtime; public final ThreadContext tg; private final Environment env; + public final dolda.jsvc.jmx.Context mbean; private static String mangle(File f) { String ret = f.getName(); @@ -34,6 +35,7 @@ public class DSContext extends SimpleContext { this.env = env; loadconfig(); this.tg = ThreadContext.create(this, loader); + mbean = new dolda.jsvc.jmx.Context(tg); } private void loadconfig() { diff --git a/src/dolda/jsvc/scgi/DirServer.java b/src/dolda/jsvc/scgi/DirServer.java index 1ac452c..5d82739 100644 --- a/src/dolda/jsvc/scgi/DirServer.java +++ b/src/dolda/jsvc/scgi/DirServer.java @@ -7,10 +7,12 @@ import java.util.logging.*; import dolda.jsvc.*; import dolda.jsvc.util.*; import dolda.jsvc.j2ee.PosixArgs; +import java.lang.management.ManagementFactory; +import javax.management.*; public class DirServer extends Server { private final Map contexts = new HashMap(); - private final Environment env; + public final Environment env; private final Logger logger = Logger.getLogger("dolda.jsvc.scgi.dirserver"); private Thread sdhook = null, main = null; @@ -26,6 +28,11 @@ public class DirServer extends Server { if(ctx != null) { if(ctx.mtime < file.lastModified()) { ctx.tg.shutdown(); + try { + ManagementFactory.getPlatformMBeanServer().unregisterMBean(ctx.mbean.name); + } catch(InstanceNotFoundException e) { + } catch(MBeanRegistrationException e) { + } contexts.remove(file); ctx = null; act = "reloaded %s as %s"; @@ -33,6 +40,12 @@ public class DirServer extends Server { } if(ctx == null) { ctx = new DSContext(file, env); + try { + ManagementFactory.getPlatformMBeanServer().registerMBean(ctx.mbean, ctx.mbean.name); + } catch(InstanceAlreadyExistsException e) { + } catch(MBeanRegistrationException e) { + } catch(NotCompliantMBeanException e) { + } contexts.put(file, ctx); logger.config(String.format(act, file, ctx.name())); } @@ -77,6 +90,11 @@ public class DirServer extends Server { } } super.shutdown(); + try { + ManagementFactory.getPlatformMBeanServer().unregisterMBean(dolda.jsvc.scgi.jmx.Server.name); + } catch(InstanceNotFoundException e) { + } catch(MBeanRegistrationException e) { + } } private static void usage(PrintStream out) { @@ -124,6 +142,12 @@ public class DirServer extends Server { return; /* Because javac is stupid. :-/ */ } DirServer s = new DirServer(sk, env); + try { + ManagementFactory.getPlatformMBeanServer().registerMBean(new dolda.jsvc.scgi.jmx.Server(s), dolda.jsvc.scgi.jmx.Server.name); + } catch(InstanceAlreadyExistsException e) { + } catch(MBeanRegistrationException e) { + } catch(NotCompliantMBeanException e) { + } if(charset != null) s.headcs = charset; diff --git a/src/dolda/jsvc/scgi/jmx/Server.java b/src/dolda/jsvc/scgi/jmx/Server.java new file mode 100644 index 0000000..573e9a7 --- /dev/null +++ b/src/dolda/jsvc/scgi/jmx/Server.java @@ -0,0 +1,25 @@ +package dolda.jsvc.scgi.jmx; + +import javax.management.*; +import dolda.jsvc.scgi.*; + +public class Server implements ServerMBean { + public static final ObjectName name; + private final DirServer b; + + static { + try { + name = new ObjectName("dolda.jsvc", "type", "ScgiServer"); + } catch(MalformedObjectNameException e) { + throw(new RuntimeException(e)); + } + } + + public Server(DirServer b) { + this.b = b; + } + + public String getdataroot() { + return(b.env.root.getPath()); + } +} diff --git a/src/dolda/jsvc/scgi/jmx/ServerMBean.java b/src/dolda/jsvc/scgi/jmx/ServerMBean.java new file mode 100644 index 0000000..f3131f3 --- /dev/null +++ b/src/dolda/jsvc/scgi/jmx/ServerMBean.java @@ -0,0 +1,5 @@ +package dolda.jsvc.scgi.jmx; + +public interface ServerMBean { + public String getdataroot(); +}