Skip to main content

MBean server

Stand alone or as web component, building an application around a set of services is the best practice. Several good solutions exist but always with some drawback, Too complex, too heavy, platform dependent, endless configuration, overkill unneeded functionalities...Therefore, I created VostokMbean, an Mbean factory, not the faster, not the most versatile but one of the simplest to use and able to cover most of your needs.

  • Platform independent, tomcat, jboss, standalone app... same code, same build
  • Lightweight, just 4 jar for 78Kb
  • Easy to use, all done with a couple of annotations
  • no configuration, just put annotation on your class, the factory does the rest
  • Resolve service dependencies, same app, local server or even external
  • Can turn any class into a service, no special convention needed
Sample

This is a snippet from the sample source application

Declaring a factory and start discovering services. For web app, you don't need to manage the factory yourself, use VostokMBeanLibWebListener instead

// declare a new named factory
MBeanFactory factory = MBeanFactory.CreateFactory("org.vostok.management.test");

// start the factory synchronously, waiting for all service to load
factory.startAndWait();

Declare a simple service by adding @MBean to a class

import org.vostok.management.annotations.MBean;

@MBean
public class A {

	public void hello() {
		System.out.println("hello world");
	}
}

Now, let use "A". Because it doesn't declare an interface, calling method hello as to be done by reflection.
Note the helper MBeanFactory.getJndi Because no jndi name was provided, MbeanFactory created one itself

factory.getMbeanServer().invoke(new ObjectName(MBeanFactory.getJndi(A.class)), "hello", new Object[]{}, new String[]{});

By providing an interface a service can be used with a proxy and be injected on other services with @Resource.
Note on Otherservice a jndi name is mandatory. The code also demonstrate the use of @PostConstruct

public interface AServicesInterface {
	public String ping(String str);
}
@MBean(jndi = "bean:org.vostok.management.test.withinterface.server=AServiceService")
@Interface(value = AServicesInterface.class)
@LocalDependencies(OtherService.class)
public class AService implements AServicesInterface {

	// Note the use of interface instead of base class, unlike for @LocalDependencies
	// Also a jndi name is mandatory, both in the bean and on the resource
	@Resource(name = "bean:org.vostok.management.test.withinterface.server=OtherService")
	OtherServiceInterface other;
	
	String start = "";

	public String ping(String str) {
		return "ping from AService :" + str + " ! " + other.upper(start);
	}

	@PostConstruct
	public void onstart() {
		System.out.println("From AService : service registered");
		start = " added on @PostConstruct annotation callback";
	}
}

You can call the service by reflection, but take advantage of proxy instead...
If a jndi name was mandatory on declaration, you can use the helper now.

AServicesInterface mbean = factory.getMbean(AServicesInterface.class, MBeanFactory.getJndi(AService.class));
System.out.println("#### str :" + mbean.ping("Now says hello using a proxy"));
Install

VostokMbeanLib is made of a single jar weight 44.8Ko. Dependencies : VostokClassLoaderLib, VostokGlobLib and VostokIoLib

Download
Java servlet container

Use this additional jar to automatically start the factory on your web application.

on your web.xml description, it expect 2 context parameters :

  • vostok.management.factory : Name of the factory. Optional
  • vostok.management.path : Comma separated list of path where too look for services. Mandatory
Sample web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

	<context-param>
		<param-name>vostok.management.path</param-name>
		<param-value>org.vostok.vaadin.cons.services</param-value>
	</context-param>
	<context-param>
		<param-name>vostok.management.factory</param-name>
		<param-value>org.vostok.vaadin.cons</param-value>
	</context-param>

	<listener>
		<description>Context listener responsible of loading/unloading mbeans</description>
		<listener-class>org.vostok.management.servlet.ContextListener</listener-class>
	</listener>
<!-- -
Rest of your code
<!- -->

</web-app>
Maven repository

A repository will be available soon

Javadocs
Licence

VostokMBeanServer is provided for free, for community, private or commercial work under the creative commons licence, CC-BY, no attribution needed.