官术网_书友最值得收藏!

Event broadcaster

The ReactiveBroadcaster class is an event broadcaster that handles the responsibility of subscribing the observers and sending the updates to interested observers asynchronously and will also do the cleanup after the completion of the events: 

    /**
* Handles the event broadcasting to the observers in an
* asynchronous way.
*/
class ReactiveBroadcaster {

/**
* Set of emitters for multiple events
*/
private var emitters = synchronizedSet(HashSet<SseEmitter>())

/**
* Subscribe to the event
*/
fun subscribe(): SseEmitter {
val sseEmitter = SseEmitter()
// Stop observing the event on completion
sseEmitter.onCompletion(
{this.emitters.remove(sseEmitter)
})
this.emitters.add(sseEmitter)
return sseEmitter
}

/**
* Trigger the event update to the observers
*/
fun send(o: Any) {
synchronized(emitters) {
emitters.iterator().forEach {
try {
it.send(o, MediaType.APPLICATION_JSON)
} catch (e: IOException) {}
}
}
}
}
主站蜘蛛池模板: 南召县| 舒兰市| 蒙城县| 五指山市| 来宾市| 昂仁县| 安仁县| 宜黄县| 阳城县| 历史| 孝昌县| 彭泽县| 壶关县| 昆明市| 宜兴市| 天祝| 南昌市| 西贡区| 十堰市| 祁东县| 太谷县| 五大连池市| 郎溪县| 陆良县| 石景山区| 凤庆县| 吴桥县| 青州市| 奇台县| 调兵山市| 长宁县| 叙永县| 独山县| 赤城县| 安乡县| 阳江市| 巴中市| 虞城县| 辉南县| 象山县| 吴江市|