Java Spring Multiple Choice Questions & Answers (MCQs) on “JMX MBeans”.
1. There are two main options for scheduling tasks on the Java platform:-
a) JDK Timer
b) Quartz Scheduler
c) All of the mentioned
d) None of the mentioned
Answer: c
Clarification: JDK Timer offers simple task scheduling features that you can use conveniently because the features are bundled with JDK. Compared with JDK Timer, Quartz offers more powerful job scheduling features.
2. Spring supports JMX by allowing you to export any beans in its IoC container as model MBeans.
a) True
b) False
Answer: a
Clarification: This can be done simply by declaring an MBeanExporter instance.
3. Connectors to expose your MBeans for remote access over a specific protocol by using a factory bean.
a) MBeanExporter
b) JSR-160
c) All of the mentioned
d) None of the mentioned
Answer: c
Clarification: In addition, Spring enables you to declare JSR-160 (Java Management Extensions Remote API) connectors to expose your MBeans for remote access over a specific protocol by using a factory bean. Spring provides factory beans for both servers and clients.
4. Spring can also detect and export your MBeans automatically from beans declared in the IoC container and annotated with JMX-specific annotations defined by Spring.
a) True
b) False
Answer: a
Clarification: The MBeanExporter class exports beans, delegating to an instance of MBeanInfoAssembler to do the heavy lifting.
5. Method, all files in the source directory will be replicated to the destination directory.
a) replicate
b) copy
c) rep
d) none of the mentioned
Answer: a
Clarification: Each time you call the replicate() method, all files in the source directory will be replicated to the destination directory. To avoid unexpected problems caused by concurrent replication, you declare this method as synchronized.
6. To register an MBean, you need an instance of the interface :-
import java.lang.management.ManagementFactory;
import javax.management.Descriptor;
import javax.management.JMException;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.modelmbean.DescriptorSupport;
import javax.management.modelmbean.InvalidTargetObjectTypeException;
import javax.management.modelmbean.ModelMBeanAttributeInfo;
import javax.management.modelmbean.ModelMBeanInfo;
import javax.management.modelmbean.ModelMBeanInfoSupport;
import javax.management.modelmbean.ModelMBeanOperationInfo;
import javax.management.modelmbean.RequiredModelMBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {<code>code>
public static void main(String[] args) throws IOException {
ApplicationContext context =
new ClassPathXmlApplicationContext("beans-jmx.xml");
FileReplicator documentReplicator =
(FileReplicator) context.getBean("documentReplicator");
try {
MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
ObjectName objectName = new ObjectName("bean:name=documentReplicator");
RequiredModelMBean mbean = new RequiredModelMBean();
mbean.setManagedResource(documentReplicator, "objectReference");
Descriptor srcDirDescriptor = new DescriptorSupport(new String[] {
"name=SrcDir", "descriptorType=attribute",
"getMethod=getSrcDir", "setMethod=setSrcDir" });
ModelMBeanAttributeInfo srcDirInfo = new ModelMBeanAttributeInfo(
"SrcDir", "java.lang.String", "Source directory",
true, true, false, srcDirDescriptor);
Descriptor destDirDescriptor = new DescriptorSupport(new String[] {
"name=DestDir", "descriptorType=attribute",
"getMethod=getDestDir", "setMethod=setDestDir" });
public static void main(String[] args) throws IOException {
ApplicationContext context =
new ClassPathXmlApplicationContext("beans-jmx.xml");
FileReplicator documentReplicator =
(FileReplicator) context.getBean("documentReplicator");
ModelMBeanAttributeInfo destDirInfo = new ModelMBeanAttributeInfo(
"DestDir", "java.lang.String", "Destination directory",
true, true, false, destDirDescriptor);
ModelMBeanOperationInfo getSrcDirInfo = new ModelMBeanOperationInfo(
"Get source directory",
FileReplicator.class.getMethod("getSrcDir"));
ModelMBeanOperationInfo setSrcDirInfo = new ModelMBeanOperationInfo(
"Set source directory",
FileReplicator.class.getMethod("setSrcDir", String.class));
ModelMBeanOperationInfo getDestDirInfo = new ModelMBeanOperationInfo(
"Get destination directory",
FileReplicator.class.getMethod("getDestDir"));
ModelMBeanOperationInfo setDestDirInfo = new ModelMBeanOperationInfo(
"Set destination directory",
FileReplicator.class.getMethod("setDestDir", String.class));
ModelMBeanOperationInfo replicateInfo = new ModelMBeanOperationInfo(
"Replicate files",
FileReplicator.class.getMethod("replicate"));
ModelMBeanInfo mbeanInfo = new ModelMBeanInfoSupport(
"FileReplicator", "File replicator",
new ModelMBeanAttributeInfo[] { srcDirInfo, destDirInfo },
null,
new ModelMBeanOperationInfo[] { getSrcDirInfo, setSrcDirInfo,
getDestDirInfo, setDestDirInfo, replicateInfo },
null);
mbean.setModelMBeanInfo(mbeanInfo);
mbeanServer.registerMBean(mbean, objectName);
} catch (JMException e) {
...
} catch (InvalidTargetObjectTypeException e) {
...
} catch (NoSuchMethodException e) {
...
}
System.in.read();
}
}
a) javax.management.MBeanServer
b) javax.management
c) javax.management.MBean
d) none of the mentioned
Answer: a
Clarification: To register an MBean, you need an instance of the interface javax.management.MBeanServer.
7.The static method to locate a platform MBean server.
import java.lang.management.ManagementFactory;
import javax.management.Descriptor;
import javax.management.JMException;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.modelmbean.DescriptorSupport;
import javax.management.modelmbean.InvalidTargetObjectTypeException;
import javax.management.modelmbean.ModelMBeanAttributeInfo;
import javax.management.modelmbean.ModelMBeanInfo;
import javax.management.modelmbean.ModelMBeanInfoSupport;
import javax.management.modelmbean.ModelMBeanOperationInfo;
import javax.management.modelmbean.RequiredModelMBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) throws IOException {
ApplicationContext context =
new ClassPathXmlApplicationContext("beans-jmx.xml");
FileReplicator documentReplicator =
(FileReplicator) context.getBean("documentReplicator");
try {
MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
ObjectName objectName = new ObjectName("bean:name=documentReplicator");
RequiredModelMBean mbean = new RequiredModelMBean();
mbean.setManagedResource(documentReplicator, "objectReference");
Descriptor srcDirDescriptor = new DescriptorSupport(new String[] {
"name=SrcDir", "descriptorType=attribute",
"getMethod=getSrcDir", "setMethod=setSrcDir" });
ModelMBeanAttributeInfo srcDirInfo = new ModelMBeanAttributeInfo(
"SrcDir", "java.lang.String", "Source directory",
true, true, false, srcDirDescriptor);
Descriptor destDirDescriptor = new DescriptorSupport(new String[] {
"name=DestDir", "descriptorType=attribute",
"getMethod=getDestDir", "setMethod=setDestDir" });
public static void main(String[] args) throws IOException {
ApplicationContext context =
new ClassPathXmlApplicationContext("beans-jmx.xml");
FileReplicator documentReplicator =
(FileReplicator) context.getBean("documentReplicator");
ModelMBeanAttributeInfo destDirInfo = new ModelMBeanAttributeInfo(
"DestDir", "java.lang.String", "Destination directory",
true, true, false, destDirDescriptor);
ModelMBeanOperationInfo getSrcDirInfo = new ModelMBeanOperationInfo(
"Get source directory",
FileReplicator.class.getMethod("getSrcDir"));
ModelMBeanOperationInfo setSrcDirInfo = new ModelMBeanOperationInfo(
"Set source directory",
FileReplicator.class.getMethod("setSrcDir", String.class));
ModelMBeanOperationInfo getDestDirInfo = new ModelMBeanOperationInfo(
"Get destination directory",
FileReplicator.class.getMethod("getDestDir"));
ModelMBeanOperationInfo setDestDirInfo = new ModelMBeanOperationInfo(
"Set destination directory",
FileReplicator.class.getMethod("setDestDir", String.class));
ModelMBeanOperationInfo replicateInfo = new ModelMBeanOperationInfo(
"Replicate files",
FileReplicator.class.getMethod("replicate"));
ModelMBeanInfo mbeanInfo = new ModelMBeanInfoSupport(
"FileReplicator", "File replicator",
new ModelMBeanAttributeInfo[] { srcDirInfo, destDirInfo },
null,
new ModelMBeanOperationInfo[] { getSrcDirInfo, setSrcDirInfo,
getDestDirInfo, setDestDirInfo, replicateInfo },
null);
mbean.setModelMBeanInfo(mbeanInfo);
mbeanServer.registerMBean(mbean, objectName);
} catch (JMException e) {
...
} catch (InvalidTargetObjectTypeException e) {
...
} catch (NoSuchMethodException e) {
...
}
System.in.read();
}
}
a) ManagementFactory.getPlatform()
b) ManagementFactory.getPlatformMBeanServer()
c) ManagementFactory.getPlatformMBean()
d) All of the mentioned
Answer: b
Clarification: It will create an MBean server if none exists and then register this server instance for future use.
8.From the preceding code, you can see that for each MBean attribute and MBean operation, you need to create a:-
import java.lang.management.ManagementFactory;
import javax.management.Descriptor;
import javax.management.JMException;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.modelmbean.DescriptorSupport;
import javax.management.modelmbean.InvalidTargetObjectTypeException;
import javax.management.modelmbean.ModelMBeanAttributeInfo;
import javax.management.modelmbean.ModelMBeanInfo;
import javax.management.modelmbean.ModelMBeanInfoSupport;
import javax.management.modelmbean.ModelMBeanOperationInfo;
import javax.management.modelmbean.RequiredModelMBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) throws IOException {
ApplicationContext context =
new ClassPathXmlApplicationContext("beans-jmx.xml");
FileReplicator documentReplicator =
(FileReplicator) context.getBean("documentReplicator");
try {
MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
ObjectName objectName = new ObjectName("bean:name=documentReplicator");
RequiredModelMBean mbean = new RequiredModelMBean();
mbean.setManagedResource(documentReplicator, "objectReference");
Descriptor srcDirDescriptor = new DescriptorSupport(new String[] {
"name=SrcDir", "descriptorType=attribute",
"getMethod=getSrcDir", "setMethod=setSrcDir" });
ModelMBeanAttributeInfo srcDirInfo = new ModelMBeanAttributeInfo(
"SrcDir", "java.lang.String", "Source directory",
true, true, false, srcDirDescriptor);
Descriptor destDirDescriptor = new DescriptorSupport(new String[] {
"name=DestDir", "descriptorType=attribute",
"getMethod=getDestDir", "setMethod=setDestDir" });
public static void main(String[] args) throws IOException {
ApplicationContext context =
new ClassPathXmlApplicationContext("beans-jmx.xml");
FileReplicator documentReplicator =
(FileReplicator) context.getBean("documentReplicator");
ModelMBeanAttributeInfo destDirInfo = new ModelMBeanAttributeInfo(
"DestDir", "java.lang.String", "Destination directory",
true, true, false, destDirDescriptor);
ModelMBeanOperationInfo getSrcDirInfo = new ModelMBeanOperationInfo(
"Get source directory",
FileReplicator.class.getMethod("getSrcDir"));
ModelMBeanOperationInfo setSrcDirInfo = new ModelMBeanOperationInfo(
"Set source directory",
FileReplicator.class.getMethod("setSrcDir", String.class));
ModelMBeanOperationInfo getDestDirInfo = new ModelMBeanOperationInfo(
"Get destination directory",
FileReplicator.class.getMethod("getDestDir"));
ModelMBeanOperationInfo setDestDirInfo = new ModelMBeanOperationInfo(
"Set destination directory",
FileReplicator.class.getMethod("setDestDir", String.class));
ModelMBeanOperationInfo replicateInfo = new ModelMBeanOperationInfo(
"Replicate files",
FileReplicator.class.getMethod("replicate"));
ModelMBeanInfo mbeanInfo = new ModelMBeanInfoSupport(
"FileReplicator", "File replicator",
new ModelMBeanAttributeInfo[] { srcDirInfo, destDirInfo },
null,
new ModelMBeanOperationInfo[] { getSrcDirInfo, setSrcDirInfo,
getDestDirInfo, setDestDirInfo, replicateInfo },
null);
mbean.setModelMBeanInfo(mbeanInfo);
mbeanServer.registerMBean(mbean, objectName);
} catch (JMException e) {
...
} catch (InvalidTargetObjectTypeException e) {
...
} catch (NoSuchMethodException e) {
...
}
System.in.read();
}
}
a) ModelMBeanAttributeInfo
b) ModelMBeanOperationInfo
c) None of the mentioned
d) All of the mentioned
Answer: d
Clarification: From the preceding code, you can see that for each MBean attribute and MBean operation, you need to create a ModelMBeanAttributeInfo object and a ModelMBeanOperationInfo object for describing it.
9. VM argument to enable local monitoring of this application.
a) -Dcom.sun.management.jmxremote
b) -Dcom.sun.management
c) -Dcom.sun.management.jmx
d) None of the mentioned
Answer: a
Clarification: You have to add the VM argument -Dcom.sun.management.jmxremote to enable local monitoring of this application. You should also include all other options for your command, such as the classpath, as necessary.
Java –classpath … -Dcom.sun.management.jmxremote com.apress.springrecipes.replicator.Main
10. However, in an environment with no MBean server available, you have to create one explicitly using:-
a) MBeanServerFactoryBean
b) MBeanServerFactory
c) All of the mentioned
d) None of the mentioned
Answer: a
Clarification: To make your application portable to different runtime environments, you should enable the locateExistingServerIfPossible property so that this factory bean will create an MBean server only if none is available.
11. Spring allows you to create a JMX connector server through:-
a) ConnectorServerFactoryBean
b) ConnectorServer
c) ConnectorServerFactory
d) All of the mentioned
Answer: a
Clarification: By default, ConnectorServerFactoryBean creates and starts a JMX connector server bound to the service URL service:jmx:jmxmp://localhost:9875, which exposes the JMX connector through the JMX Messaging Protocol (JMXMP).
12. ? should be replaced by:-
<beans ...>
...
<bean id="rmiRegistry"
class="org.springframework.remoting.rmi.RmiRegistryFactoryBean" />
<bean id="connectorServer"
class="org.springframework.jmx.support.ConnectorServerFactoryBean"
depends-on="rmiRegistry">
<property name="serviceUrl" value=? />
bean>
beans>
a) service:jmx:rmi://localhost/jndi/rmi://localhost:1099/replicator
b) service:jmx:rmi://localhost/jndi/rmi://localhost:1099/
c) service:jmx:rmi://localhost/jndi//localhost:1099/replicator
d) none of the mentioned
Answer: a
Clarification: To expose your JMX connector through a specific protocol, you just provide the service URL for it.
13. If no RMI registry has been created externally, you should create one by using:-
a) RmiRegistryFactory
b) RmiRegistryFactoryBean
c) RmiRegistry
d) RmiRegister
Answer: b
Clarification: If no RMI registry has been created externally, you should create one by using RmiRegistryFactoryBean. The default port for this registry is 1099, but you can specify another one in its port property.
14. The simplest MBean assembler in Spring is, which allows you to specify the names of the methods to export.
a) MethodNameBasedMBeanInfoAssembler
b) MethodNameBasedMBeanInfo
c) MethodNameBasedMBean
d) MethodNameBasedInfoAssembler
Answer: a
Clarification: The simplest MBean assembler in Spring is MethodNameBasedMBeanInfoAssembler, which allows you to specify the names of the methods to export.
15. Spring allows your beans to publish JMX notifications through:-
a) NotificationPublisher
b) Notification
c) Notify
d) All of the mentioned
Answer: a
Clarification: Spring allows your beans to publish JMX notifications through the NotificationPublisher interface. You can also register standard JMX notification listeners in the IoC container to listen to JMX notifications.