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

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) {}
}
}
}
}
主站蜘蛛池模板: 柳州市| 桃园市| 民勤县| 无为县| 黄龙县| 芦溪县| 茌平县| 泽州县| 专栏| 新绛县| 富蕴县| 瑞昌市| 巴林左旗| 沈丘县| 四平市| 万山特区| 海伦市| 安仁县| 奇台县| 建昌县| 雷州市| 越西县| 府谷县| 疏附县| 吕梁市| 达拉特旗| 元江| 读书| 郁南县| 南溪县| 离岛区| 千阳县| 蓝田县| 老河口市| 慈利县| 嘉义县| 乌拉特前旗| 崇信县| 定远县| 保山市| 曲沃县|