Skip to content

Instantly share code, notes, and snippets.

@jarrodhroberson
Created September 10, 2013 15:12
Show Gist options
  • Save jarrodhroberson/6510878 to your computer and use it in GitHub Desktop.
Save jarrodhroberson/6510878 to your computer and use it in GitHub Desktop.
Proper idiom to correctly implement the "Singleton" pattern in modern versions of Java.
import javax.management.InstanceAlreadyExistsException;
import javax.management.JMX;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.NotCompliantMBeanException;
import javax.management.ObjectName;
import java.lang.management.ManagementFactory;
public class MyApp
{
private static final MyObject OBJ1;
private static final MyObject OBJ2;
static
{
try
{
final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
final ObjectName on1 = new ObjectName("com.example:type=MyObject1");
final ObjectName on2 = new ObjectName("com.example:type=MyObject2");
OBJ1 = JMX.newMBeanProxy(mbs, on1, MyObject.class);
OBJ2 = JMX.newMBeanProxy(mbs, on2, MyObject.class);
}
catch (MalformedObjectNameException e) { throw new RuntimeException(e); }
}
public MyObject getObj(final int param)
{
switch (param)
{
case 1:
return MyApp.OBJ1;
case 2:
return MyApp.OBJ2;
default:
throw new IllegalArgumentException(String.format("%d is not a valid object id", param));
}
}
public static void main(String[] args)
{
final MyApp e = new MyApp();
System.out.println("e.getObj(1) = " + e.getObj(1));
}
public static class MyObject1 implements MyObjectMBean
{
static
{
try
{
final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
final ObjectName on1 = new ObjectName("com.example:type=MyObject1");
mbs.registerMBean(new MyObject1(), on1);
}
catch (MalformedObjectNameException e) { throw new RuntimeException(e); }
catch (NotCompliantMBeanException e) { throw new RuntimeException(e); }
catch (InstanceAlreadyExistsException e) { throw new RuntimeException(e); }
catch (MBeanRegistrationException e) { throw new RuntimeException(e); }
}
private MyObject1() { /* your interface implementations go here */ }
}
public static class MyObject2 implements MyObjectMBean
{
static
{
try
{
final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
final ObjectName on1 = new ObjectName("com.example:type=MyObject2");
mbs.registerMBean(new MyObject2(), on1);
}
catch (MalformedObjectNameException e) { throw new RuntimeException(e); }
catch (NotCompliantMBeanException e) { throw new RuntimeException(e); }
catch (InstanceAlreadyExistsException e) { throw new RuntimeException(e); }
catch (MBeanRegistrationException e) { throw new RuntimeException(e); }
}
private MyObject2() { /* your interface implementations go here */ }
}
private static interface MyObjectMBean extends MyObject { /* mbean specific methods go here */ }
private static interface MyObject { /* your interface methods go here */ }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment