2. Code Example

The ServerManagerImpl is registered as a Spring bean, and then exposed as a JMX MBean.

Example 1. ServerManagerImpl

The @Component annotation indicates that this class is eligible to be a Spring bean when context:component-scan runs. The @ManagedResource annotation indicates that the bean can be exported as a JMX MBean. It's objectName attribute is optional, but can be used to specify a specific name for the MBean. In this case it will be exposed under the 'org.springbyexample.jmx' directory with the name 'ServerManager'. By default it would have been exposed under the directory 'org.springbyexample.jmx' and subdirectory 'serverManagerImpl' with the name 'ServerManagerImpl'.

The @ManagedAttribute can expose a getter or setter by annotating the method. A method that takes multiple values can be exposed using @ManagedOperation along with @ManagedOperationParameters and @ManagedOperationParameter to parameters the operation can take.

                
@Component
@ManagedResource(objectName="org.springbyexample.jmx:name=ServerManager", 
                 description="Server manager.")
public class ServerManagerImpl implements ServerManager { 

    final Logger logger = LoggerFactory.getLogger(ServerManagerImpl.class);

    private String serverName = "springServer";
    private boolean serverRunning = true;
    private int minPoolSize = 5;
    private int maxPoolSize = 10;

    /**
     * Gets server name.
     */
    @ManagedAttribute(description="The server name.")
    public String getServerName() {
        return serverName;
    }
    
    /**
     * Whether or not the server is running.
     */
    @ManagedAttribute(description="Server's running status.")
    public boolean isServerRunning() {
        return serverRunning;
    }

    /**
     * Sets whether or not the server is running.
     */
    @ManagedAttribute(description="Whether or not the server is running.",
                      currencyTimeLimit=20,
                      persistPolicy="OnUpdate")
    public void setServerRunning(boolean serverRunning) {
        this.serverRunning = serverRunning;
    }

    /**
     * Change db connection pool size.
     * 
     * @param   min     Minimum pool size.
     * @param   max     Maximum pool size.
     * 
     * @return  int     Current pool size.
     */
    @ManagedOperation(description="Change db connection pool size.")
    @ManagedOperationParameters({
        @ManagedOperationParameter(name="min", description= "Minimum pool size."),
        @ManagedOperationParameter(name="max", description= "Maximum pool size.")})
    public int changeConnectionPoolSize(int minPoolSize, int maxPoolSize) {
        Assert.isTrue((minPoolSize > 0), 
                      "Minimum connection pool size must be larger than zero.  min=" + minPoolSize);
        Assert.isTrue((minPoolSize < maxPoolSize), 
                      "Minimum connection pool size must be smaller than the maximum." + 
                      "  min=" + minPoolSize + ", max=" + maxPoolSize);
        
        this.minPoolSize = minPoolSize;
        this.maxPoolSize = maxPoolSize;
        
        int diff = (maxPoolSize - minPoolSize);

        // randomly generate current pool size between new min and max
        Random rnd = new Random();
        int currentSize = (minPoolSize + rnd.nextInt(diff));
        
        logger.info("Changed connection pool size. min={}, max={}, current={}", 
                    new Object[] { this.minPoolSize, this.maxPoolSize, currentSize});
        
        return currentSize; 
    
    }

}
                
            

Example 2. Excerpt from JmxTest

                
@Autowired
private MBeanServerConnection clientConnector = null;

@Autowired
@Qualifier("serverManagerProxy")
private ServerManager serverManager = null;

public void testMBeanServerConnection() {   
    MBeanInfo beanInfo = null;
    
    try {
        beanInfo = clientConnector.getMBeanInfo(new ObjectName("org.springbyexample.jmx:name=ServerManager"));
    } ...
}

public void testServerManagerRemoteProxy() {
    ...
    
    logger.info("serverName={}, serverRunning={}", 
                serverManager.getServerName(), serverManager.isServerRunning());
    
    int min = 10;
    int max = 20;
    
    int result = serverManager.changeConnectionPoolSize(min, max);
    
    ...
}