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

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) {}
}
}
}
}
主站蜘蛛池模板: 清镇市| 海城市| 德昌县| 巴彦淖尔市| 正蓝旗| 富裕县| 裕民县| 凤凰县| 沂水县| 商河县| 惠安县| 林西县| 盐池县| 青州市| 车险| 梧州市| 黑河市| 太保市| 揭阳市| 曲周县| 京山县| 宜兰市| 陆河县| 澳门| 英德市| 天水市| 廉江市| 金湖县| 天柱县| 余江县| 永川市| 酉阳| 抚远县| 雅安市| 饶阳县| 宜兴市| 普宁市| 华亭县| 鄱阳县| 醴陵市| 察雅县|