Skip to content

Commit b7fbb78

Browse files
committed
Add lesson 17
1 parent e900f12 commit b7fbb78

File tree

14 files changed

+532
-0
lines changed

14 files changed

+532
-0
lines changed
Binary file not shown.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
4+
### STS ###
5+
.apt_generated
6+
.classpath
7+
.factorypath
8+
.project
9+
.settings
10+
.springBeans
11+
12+
### IntelliJ IDEA ###
13+
.idea
14+
*.iws
15+
*.iml
16+
*.ipr
17+
18+
### NetBeans ###
19+
nbproject/private/
20+
build/
21+
nbbuild/
22+
dist/
23+
nbdist/
24+
.nb-gradle/
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.segmentfault</groupId>
7+
<artifactId>spring-boot-lesson-17</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>spring-boot-lesson-17</name>
12+
<description>Demo project for Spring Boot</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.5.6.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
</properties>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-actuator</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-web</artifactId>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>org.springframework.boot</groupId>
39+
<artifactId>spring-boot-starter-test</artifactId>
40+
<scope>test</scope>
41+
</dependency>
42+
</dependencies>
43+
44+
<build>
45+
<plugins>
46+
<plugin>
47+
<groupId>org.springframework.boot</groupId>
48+
<artifactId>spring-boot-maven-plugin</artifactId>
49+
</plugin>
50+
</plugins>
51+
</build>
52+
53+
54+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.segmentfault.springbootlesson17;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class SpringBootLesson17Application {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(SpringBootLesson17Application.class, args);
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.segmentfault.springbootlesson17.controller;
2+
3+
import com.segmentfault.springbootlesson17.jmx.SimpleBean;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.stereotype.Controller;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.PostMapping;
8+
import org.springframework.web.bind.annotation.RequestParam;
9+
import org.springframework.web.bind.annotation.RestController;
10+
11+
/**
12+
* TODO
13+
*
14+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
15+
* @see
16+
* @since 2017.08.30
17+
*/
18+
@RestController
19+
public class JMXController {
20+
21+
@Autowired
22+
private SimpleBean simpleBean;
23+
24+
25+
@GetMapping("/jmx/simple-bean")
26+
public SimpleBean simpleBean(@RequestParam(required = false) Long id,
27+
@RequestParam(required = false) String name,
28+
@RequestParam(required = false) Integer value) {
29+
30+
if (id != null) {
31+
simpleBean.setId(id);
32+
}
33+
34+
if (name != null) {
35+
simpleBean.setName(name);
36+
}
37+
38+
if (value != null) {
39+
simpleBean.setValue(value);
40+
}
41+
42+
return simpleBean;
43+
44+
}
45+
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.segmentfault.springbootlesson17.jmx;
2+
3+
import org.springframework.jmx.export.annotation.ManagedAttribute;
4+
import org.springframework.jmx.export.annotation.ManagedOperation;
5+
import org.springframework.jmx.export.annotation.ManagedResource;
6+
import org.springframework.stereotype.Component;
7+
8+
/**
9+
* 简单的Bean
10+
*
11+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
12+
* @see
13+
* @since 2017.08.30
14+
*/
15+
@ManagedResource(
16+
objectName = "com.segmentfault.springbootlesson17.jmx:type=SimpleBean",
17+
description = "这是一个简单的Bean,被 Spring 托管"
18+
)
19+
@Component
20+
public class SimpleBean {
21+
22+
private Long id;
23+
24+
private String name;
25+
26+
private Integer value;
27+
28+
@ManagedAttribute(description = "ID 属性")
29+
public Long getId() {
30+
return id;
31+
}
32+
33+
public void setId(Long id) {
34+
this.id = id;
35+
}
36+
37+
@ManagedAttribute(description = "Name 属性")
38+
public String getName() {
39+
return name;
40+
}
41+
42+
public void setName(String name) {
43+
this.name = name;
44+
}
45+
46+
@ManagedAttribute(description = "Value 属性")
47+
public Integer getValue() {
48+
return value;
49+
}
50+
51+
public void setValue(Integer value) {
52+
this.value = value;
53+
}
54+
55+
@ManagedOperation(description = "display 操作")
56+
public String display() {
57+
return this.toString();
58+
}
59+
60+
@Override
61+
public String toString() {
62+
return "SimpleBean{" +
63+
"id=" + id +
64+
", name='" + name + '\'' +
65+
", value=" + value +
66+
'}';
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.segmentfault.springbootlesson17.mbean;
2+
3+
import javax.management.MBeanServer;
4+
import javax.management.MalformedObjectNameException;
5+
import javax.management.ObjectName;
6+
import java.lang.management.ManagementFactory;
7+
8+
/**
9+
* MBean 演示代码
10+
*
11+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
12+
* @see
13+
* @since 2017.08.30
14+
*/
15+
public class MBeanDemo {
16+
17+
public static void main(String[] args) throws Exception {
18+
19+
// MBean 服务器 - Agent Level
20+
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
21+
22+
// 注册对象
23+
SimpleData simpleData = new SimpleData();
24+
25+
// 注册名称
26+
ObjectName objectName = createObjectName(simpleData);
27+
28+
// 注册 MBean - SimpleDataMBean
29+
mBeanServer.registerMBean(simpleData, objectName);
30+
31+
// 服务器不马上停止
32+
Thread.sleep(Long.MAX_VALUE);
33+
34+
}
35+
36+
private static ObjectName createObjectName(Object mbean) throws MalformedObjectNameException {
37+
38+
Class<?> mBeanClass = mbean.getClass();
39+
40+
// e,g : com.segmentfault.springbootlesson17.mbean
41+
String packageName = mBeanClass.getPackage().getName();
42+
String className = mBeanClass.getSimpleName();
43+
44+
return new ObjectName(packageName + ":type=" + className);
45+
46+
}
47+
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package com.segmentfault.springbootlesson17.mbean;
2+
3+
import javax.management.*;
4+
import java.util.concurrent.atomic.AtomicLong;
5+
6+
import static javax.management.AttributeChangeNotification.ATTRIBUTE_CHANGE;
7+
8+
/**
9+
* TODO
10+
*
11+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
12+
* @see
13+
* @since 2017.08.30
14+
*/
15+
public class SimpleData extends NotificationBroadcasterSupport implements SimpleDataMBean,
16+
NotificationListener, NotificationFilter {
17+
18+
private String data;
19+
20+
private static final AtomicLong sequenceNumber = new AtomicLong();
21+
22+
public SimpleData() {
23+
this.addNotificationListener(this, this, null);
24+
}
25+
26+
@Override
27+
public String getData() {
28+
return data;
29+
}
30+
31+
@Override
32+
public String displayData() {
33+
return data;
34+
}
35+
36+
@Override
37+
public void setData(String data) {
38+
String oldData = this.data;
39+
this.data = data;
40+
Notification notification = new AttributeChangeNotification(this,
41+
sequenceNumber.incrementAndGet(),
42+
System.currentTimeMillis(),
43+
"Data has been changed from " + oldData + " to " + data,
44+
"data",
45+
String.class.getName(),
46+
oldData,
47+
data
48+
);
49+
sendNotification(notification);
50+
}
51+
52+
/**
53+
* 处理通知/事件
54+
*
55+
* @param notification
56+
* @param handback
57+
*/
58+
@Override
59+
public void handleNotification(Notification notification, Object handback) {
60+
61+
AttributeChangeNotification attributeChangeNotification = (AttributeChangeNotification) notification;
62+
63+
String oldData = (String) attributeChangeNotification.getOldValue();
64+
65+
String newData = (String) attributeChangeNotification.getNewValue();
66+
67+
System.out.printf("The notification of data's attribute - old data : %s , new data : %s \n", oldData, newData);
68+
69+
}
70+
71+
72+
@Override
73+
public boolean isNotificationEnabled(Notification notification) {
74+
75+
// 直关心 AttributeChangeNotification 通知,并且仅限于字段/属性名称为 "data"
76+
if (AttributeChangeNotification.class.isAssignableFrom(notification.getClass())) {
77+
78+
AttributeChangeNotification attributeChangeNotification = AttributeChangeNotification.class.cast(notification);
79+
80+
if ("data".equals(attributeChangeNotification.getAttributeName())) {
81+
return true;
82+
}
83+
}
84+
return false;
85+
}
86+
87+
/**
88+
* 暴露通知信息
89+
*
90+
* @return
91+
*/
92+
public MBeanNotificationInfo[] getNotificationInfo() {
93+
94+
return new MBeanNotificationInfo[]{
95+
new MBeanNotificationInfo(new String[]{ATTRIBUTE_CHANGE}, "Data Change Notification",
96+
"数据改变通知")
97+
};
98+
}
99+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.segmentfault.springbootlesson17.mbean;
2+
3+
/**
4+
* 简单数据 MBean
5+
*
6+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
7+
* @see
8+
* @since 2017.08.30
9+
*/
10+
public interface SimpleDataMBean {
11+
12+
13+
/**
14+
* Setter
15+
* Property
16+
* 属性
17+
* @param data
18+
*/
19+
void setData(String data);
20+
21+
/**
22+
* Getter
23+
* 属性
24+
* @return
25+
*/
26+
String getData();
27+
28+
29+
/**
30+
* 展示数据
31+
* 操作(Operation)
32+
* @return
33+
*
34+
*/
35+
String displayData();
36+
37+
}

0 commit comments

Comments
 (0)