Spring JMX:-
JMX is good way to expose parts of your application for monitoring
and management. Spring also provides support for exposing JMX for remote
management (JSR-160) and creating a client to manage it. This example registers
a Spring bean as a JMX MBean, exposes the JMX server over JMX Messaging Protocol
(JMXMP), and creates a client to access it.
To connect to the example using a JMX Management tool, JConsole can be used and
comes with the JDK installation. In the IDE a break point could be set in the
unit test or Thread.sleep could be added to pause the application from shutting
down. To activate local JMX access, the Java argument -Dcom.sun.management.jmxremote
must be used when starting the test.
$ jconsole
JMX is a great way to check or change state variables or invoke a method in a
(remote) running application via a management GUI such as JConsole. And Spring
makes it trivial to expose any POJO as a JMX MBean with only little
configuration in a few minutes. The Spring JMX documentation is very good,
however there are few points that I struggled with for a while and would
therefore like to record here the right solutions.
Exporting your beans to JMX:-
The core class in Spring's JMX framework is the MBeanExporter. This class
is responsible for taking your Spring beans and registering them with a JMX
MBeanServer.
For example, consider the following class:
package
org.springframework.jmx;
public class JmxTestBean implements IJmxTestBean {
private String name;
private int age;
public int getAge() {
return age;}
public void setAge(int age) {
this.age = age;}
public void setName(String name) {
this.name = name;}
public String getName() {
return name;}
public int add(int x, int y) {
return x + y;}
public void dontExposeMe() {
throw new RuntimeException();
}}
To expose the properties and methods of this bean as attributes and operations of an MBean you simply configure an instance of the MBeanExporter class in your configuration file and pass in the bean as shown below:
The pertinent bean definition from the above configuration snippet is the exporter bean. The beans property tells the MBeanExporter exactly which of your beans must be exported to the JMX MBeanServer. In the default configuration, the key of each entry in the beans Map is used as the ObjectName for the bean referenced by the corresponding entry value.
Spring and JAXB Integration Example:-
JAXB is an acronym for Java Architecture for XML Binding. It allows java
developers to map Java class to XML representation. JAXB can be used to marshal
java objects into XML and vice-versa.
It is an OXM (Object XML Mapping) or O/M framework provided by Sun.
Example of Spring and JAXB Integration (Marshalling Java Object into XML):-
You need to create following files for marshalling java object into XML using
Spring with JAXB:
1 Student.java
2 applicationContext.xml
3 Test.java
1 Student.java It defines two properties id, name . We
have used following annotations in this class:
1 @XmlRootElement It specifies the root element for the xml file.
2 @XmlAttribute It specifies attribute for the property.
3 @XmlElement It specifies the element.
package com.r4r;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="student")
public class Student {
private int id;
private String name;
@XmlAttribute(name="id")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@XmlElement(name="name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} }
2 applicationContext.xml It defines a bean jaxbMarshallerBean where Employee class is bound with the OXM framework.
"1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:oxm="http://www.springframework.org/schema/oxm"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/oxm
http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd">
3 Test.java It gets the instance of Marshaller from the applicationContext.xml file and calls the marshal method.
package com.r4r;
import java.io.FileWriter;
import java.io.IOException;
import javax.xml.transform.stream.StreamResult;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.oxm.Marshaller;
public class Test{
public static void main(String[] args)throws IOException{
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Marshaller marshaller = (Marshaller)context.getBean("jaxbMarshallerBean");
Student st=new Student();
st.setId(001);
st.setName("R4R is a Good Tutorials...");
marshaller.marshal(student, new StreamResult(new FileWriter("student.xml")));
System.out.println("XML Created Sucessfully");
} }