linux事件通知模式,Spring事件通知机制详解

ApplicationEvent

依赖关系

38ab75348a9d8f794781099b132e3420.png

ContextEvent事件机制简介

ContextRefreshedEvent:当整个ApplicationContext容器初始化完毕或者刷新时触发该事件;

@Override

public void refresh() throws BeansException, IllegalStateException {

synchronized (this.startupShutdownMonitor) {

......

try {

......

// Last step: publish corresponding event.

finishRefresh();

}

catch (BeansException ex) {

......

}

}

}

protected void finishRefresh() {

// Initialize lifecycle processor for this context.

initLifecycleProcessor();

// Propagate refresh to lifecycle processor first.

getLifecycleProcessor().onRefresh();

// Publish the final event.

publishEvent(new ContextRefreshedEvent(this));

// Participate in LiveBeansView MBean, if active.

LiveBeansView.registerApplicationContext(this);

}

ContextClosedEvent:当ApplicationContext doClose时触发该事件,这个时候会销毁所有的单例bean;

@Override

public void registerShutdownHook() {

if (this.shutdownHook == null) {

// No shutdown hook registered yet.

this.shutdownHook = new Thread() {

@Override

public void run() {

doClose();

}

};

Runtime.getRuntime().addShutdownHook(this.shutdownHook);

}

}

@Override

public void close() {

synchronized (this.startupShutdownMonitor) {

doClose();

// If we registered a JVM shutdown hook, we don't need it anymore now:

// We've already explicitly closed the context.

if (this.shutdownHook != null) {

try {

Runtime.getRuntime().removeShutdownHook(this.shutdownHook);

}

catch (IllegalStateException ex) {

// ignore - VM is already shutting down

}

}

}

}

protected void doClose() {

if (this.active.get() && this.closed.compareAndSet(false, true)) {

......

try {

// Publish shutdown event.

publishEvent(new ContextClosedEvent(this));

}

catch (Throwable ex) {

logger.warn("Exception thrown from ApplicationListener handling ContextClosedEvent", ex);

}

......

}

}

ContextStartedEvent:当ApplicationContext start时触发该事件;

1 @Override2 public voidstart() {3 getLifecycleProcessor().start();4 publishEvent(new ContextStartedEvent(this));5 }

ContextStoppedEvent:当ApplicationContext stop时触发该事件;

1 @Override2 public voidstop() {3 getLifecycleProcessor().stop();4 publishEvent(new ContextStoppedEvent(this));5 }

ApplicationListener

依赖关系

4118c67bd3476870ff17d9a78fac36e6.png

带你一步步走向源码的世界

从上边打印的线程信息可以知道,spring处理事件通知采用的是当前线程,并没有为为我们启动新的线程,所以,如果需要,你要自己处理线程信息哦,当然也可以设定(如何设置?)!

AbstractApplicationContext

a2e2fd4c0153b5720e188b13f2182854.png

10a3c8256aa755788cd74113bd8efa5f.png

d5daefb964006b395382e06daaa0998e.png

9ef5b9b78cc96c11476d5d3857aa45db.png

bdf0cbcb58b873e8886e4202c653351c.png

648d84026d4f0bb7037599a860611e61.png

282ccf0f72bf461a6bb98626ddb94cdc.png

afd632426bef55d601babfa6ba8dc941.png

848b26b2281d1aa6df4f5212db063cd4.png

f9f55e3da908d6ff68843eeda47331dc.png

b21c305f85552c2912e249451c090271.png

bd4054f21f695fe91006d583b9df5f52.png

补齐:同一个event,被多个listener监听,先被哪个listener执行是由下边的代码决定的:

bb5e36e7eed2473bb95a3e404c304097.png

如何设置线程池?

回到上边的问题,到底该如何设置线程池呢?

AbstractApplicationEventMulticaster 是private的,并且没有提供写入方法...

实际案例

用在自己的代码里就是最好的例子了 ^_^

Spring 的详细介绍:请点这里

Spring 的下载地址:请点这里

0b1331709591d260c1c78e86d0c51c18.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值