A very quick, easy to embed, web management interface for Java
I've been trying to come up with a very simple way of managing a Java-based server application i've written.
My first port of call was JMX, which worked very well locally (using JConsole) but it seems RMI requires ports both-ways and this limits it's usefulness over the internet, as i'd ideally like to manage the server (should the need arise) from my G1.
Next step was to try using the HttpAdaptor from MX4J, which worked fairly well but meant I really needed to code up a web interface to connect to it and that was a little more work than i'd anticipated.
On a side note, an awful lot of projects dealing with JMX seem to have been discontinued or abandoned. Even Sun's own OpenDMK seems to have been last released in 2007.
With those options discarded, I present, Trivial Manager!
It's designed to be very easy to embed (the servlet is a single Java class). Essentially, you create two things:
# A Java Interface to describe the methods you wish to present in the management app
# A Java class implementing the above management app
You then create the Servlet, passing the interface and an instance of the object to it in the constructor.
You may ask, how is embedding a servlet in to your application a quick solution? That's where Jetty comes to the rescue.
As an example of embedding Jetty and using TrivialManager, here is a few bits of code:
TestInterface.java:
public interface TestInterface
{
public void doSomething() throws Exception;
public Collection<String> getSomethings();
public int startParty(String name);
public void countPeople(int whiskers);
public Map<String,Integer> getIDs();
}
TestClass.java:
public class TestClass implements TestInterface
{
public void countPeople(int whiskers)
{
System.out.println("Whiskers: " + whiskers);
}
public void doSomething() throws Exception
{
System.out.println("Call to doSomething");
throw new Exception();
}
public Collection<String> getSomethings()
{
List<String> vals = Arrays.asList("Testing", "Lalala", "1231223523", "Foomoo");
return vals;
}
public int startParty(String name)
{
return 42;
}
public Map<String, Integer> getIDs()
{
Map<String,Integer> map = new HashMap<String,Integer>();
map.put("foo", 123);
map.put("rgaehre", 426432643);
return map;
}
}
and finally, here's what goes in to your main code:
Server server = new Server(8080);
Context root = new Context(server, "/", Context.SESSIONS);
root.addServlet(new ServletHolder(new TrivialManagerServlet<TestInterface>(TestInterface.class, new TestClass())), "/*");
server.start();
The end result is:

TrivialManager currently supports taking Integer, Double and String parameters.. and returning anything with a String representation, as well as Maps and Collections, which it renders as tables. I leave security and authorisation as an exercise to the reader.
The code is available: trivialmanagerservlet.java
You'll want to grab Jetty too (jetty.jar and jetty-uti.jar will need to be in your classpath).
The TrivialManager code is under MIT. Enjoy.
Sadiq Jaffer
Please wait - comments are loading














Loading…