S
S
Spring 实战(第五版)
搜索文档…
S
S
Spring 实战(第五版)
Spring 实战(第 5 版)
第一部分 Spring 基础
第 1 章 Spring 入门
第 2 章 开发 Web 应用程序
第 3 章 处理数据
第 4 章 Spring 安全
第 5 章 使用配置属性
第二部分 集成 Spring
第 6 章 创建 REST 服务
第 7 章 调用 REST 服务
第 8 章 发送异步消息
第 9 章 集成 Spring
第三部分 响应式 Spring
第 10 章 Reactor 介绍
第 11 章 开发响应式 API
第 12 章 响应式持久化数据
第四部分 云原生 Spring
第 13 章 服务发现
第 14 章 配置管理
第 15 章 处理失败和时延
第五部分 部署Spring
第 16 章 使用 SpringBoot Actuator
第 17 章 管理 Spring
第 18 章 使用 JMX 监控 Spring
18.1 使用 Actuator MBean
18.2 创建自己的 MBean
18.3 发送通知
18.4 总结
第 19 章 部署 Spring
由
GitBook
提供支持
18.3 发送通知
MBean 可以使用 Spring 的 NotificationPublisher,将通知推送到感兴趣的 JMX 客户端。NotificationPublisher 有一个
sendNotification()
方法,当给定 Notification 对象时,会将通知发布到任何已经订阅此 MBean 的 JMX 客户端。
为了使 MBean 能够发布通知,它必须实现 NotificationPublisherAware 接口,该接口要求实现
setNotificationPublisher()
方法。例如,假设每生产 100 个玉米卷,您要发布一次通知,可以修改 TacoCounter 类,实现 NotificationPublisherAware 并使用注入的 NotificationPublisher 发送通知。下面的清单显示了,实现此功能后的 TacoCounter。
程序清单 18.2 每生产 100 个玉米卷就发送通知
1
@Service
2
@ManagedResource
3
public
class
TacoCounter
4
extends
AbstractRepositoryEventListener
<
Taco
>
5
implements
NotificationPublisherAware
{
6
7
private
AtomicLong
counter
;
8
private
NotificationPublisher
np
;
9
10
...
11
12
@Override
13
public
void
setNotificationPublisher
(
NotificationPublisher
np
)
{
14
this
.
np
=
np
;
15
}
16
...
17
18
@ManagedOperation
19
public
long
increment
(
long
delta
)
{
20
long
before
=
counter
.
get
();
21
long
after
=
counter
.
addAndGet
(
delta
);
22
if
((
after
/
100
)
>
(
before
/
100
))
{
23
Notification
notification
=
new
Notification
(
24
"taco.count"
,
this
,
25
before
,
after
+
"th taco created!"
);
26
np
.
sendNotification
(
notification
);
27
}
28
return
after
;
29
}
30
}
Copied!
在 JMX 客户端中,您需要订阅 TacoCounter MBean 以接收通知。然后,随着玉米卷的生产,每生产 100 个玉米卷,客户端将收到一条通知。图 18.5 显示了在 JConsole 中显示的通知。
图 18.5 订阅 TacoCounter MBean 的 JConsole,每 100 个玉米卷被制造出来就收到一次通知。
推送通知是应用程序主动向客户端发送数据和警报的好方法,而不要求客户端轮询或主动发起调用操作。
以前
18.2 创建自己的 MBean
下一个
18.4 总结
最近更新
9mo ago
复制链接