Java Reference
In-Depth Information
After setting the default domain to
bean
, the
documentReplicator
bean will be exported under the
following MBean object name:
bean:name=documentReplicator,type=FileReplicatorJMXImpl
Moreover, you can specify a bean's MBean object name in the
objectName
attribute of the
@ManagedResource
annotation. For example, you can export your file copier as an MBean by annotating it
with the following annotations:
package com.apress.springenterpriserecipes.replicator;
...
import org.springframework.jmx.export.annotation.ManagedOperation;
import org.springframework.jmx.export.annotation.ManagedOperationParameter;
import org.springframework.jmx.export.annotation.ManagedOperationParameters;
import org.springframework.jmx.export.annotation.ManagedResource;
@ManagedResource(
objectName = "bean:name=fileCopier,type=FileCopierImpl",
description = "File Copier")
public class FileCopierImpl implements FileCopier {
@ManagedOperation(
description = "Copy file from source directory to destination directory")
@ManagedOperationParameters( {
@ManagedOperationParameter(
name = "srcDir", description = "Source directory"),
@ManagedOperationParameter(
name = "destDir", description = "Destination directory"),
@ManagedOperationParameter(
name = "filename", description = "File to copy") })
public void copyFile(String srcDir, String destDir, String filename)
throws IOException {
...
}
}
However, specifying the object name in this way works only for classes that you're going to create a
single instance of in the IoC container (e.g., file copier), not for classes that you may create multiple
instances of (e.g., file replicator). This is because you can only specify a single object name for a class. As
a result, you shouldn't try and run the same server multiple times without changing the names.
You can simply declare a
<context:mbean-export>
element in your bean configuration file, instead
of the
AnnotationMBeanExporter
declaration, which you can omit.
<beans xmlns="
http://www.springframework.org/schema/beans"
xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="
http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
<context:mbean-export server="mbeanServer" default-domain="bean" />
...
</beans>
Search WWH ::
Custom Search