ai removed boilerplate
All checks were successful
build-and-deploy / build-deploy (push) Successful in 31s
All checks were successful
build-and-deploy / build-deploy (push) Successful in 31s
This commit is contained in:
@@ -7,65 +7,30 @@ import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import org.springframework.scheduling.annotation.Scheduled
|
||||
import org.springframework.stereotype.Component
|
||||
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter
|
||||
import java.io.IOException
|
||||
import java.util.UUID
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import java.util.concurrent.CopyOnWriteArrayList
|
||||
|
||||
@Component
|
||||
class GuestDocumentEvents(
|
||||
private val guestDocumentRepo: GuestDocumentRepo,
|
||||
private val objectMapper: ObjectMapper
|
||||
) {
|
||||
private val emitters: MutableMap<GuestDocKey, CopyOnWriteArrayList<SseEmitter>> = ConcurrentHashMap()
|
||||
private val hub = SseHub<GuestDocKey>("guest-documents") { key ->
|
||||
buildSnapshot(key.propertyId, key.guestId)
|
||||
}
|
||||
|
||||
fun subscribe(propertyId: UUID, guestId: UUID): SseEmitter {
|
||||
val key = GuestDocKey(propertyId, guestId)
|
||||
val emitter = SseEmitter(0L)
|
||||
emitters.computeIfAbsent(key) { CopyOnWriteArrayList() }.add(emitter)
|
||||
emitter.onCompletion { emitters[key]?.remove(emitter) }
|
||||
emitter.onTimeout { emitters[key]?.remove(emitter) }
|
||||
emitter.onError { emitters[key]?.remove(emitter) }
|
||||
try {
|
||||
emitter.send(SseEmitter.event().name("guest-documents").data(buildSnapshot(propertyId, guestId)))
|
||||
} catch (_: IOException) {
|
||||
emitters[key]?.remove(emitter)
|
||||
}
|
||||
return emitter
|
||||
return hub.subscribe(key)
|
||||
}
|
||||
|
||||
fun emit(propertyId: UUID, guestId: UUID) {
|
||||
val key = GuestDocKey(propertyId, guestId)
|
||||
val list = emitters[key] ?: return
|
||||
val data = buildSnapshot(propertyId, guestId)
|
||||
val dead = mutableListOf<SseEmitter>()
|
||||
for (emitter in list) {
|
||||
try {
|
||||
emitter.send(SseEmitter.event().name("guest-documents").data(data))
|
||||
} catch (_: IOException) {
|
||||
dead.add(emitter)
|
||||
}
|
||||
}
|
||||
if (dead.isNotEmpty()) {
|
||||
list.removeAll(dead.toSet())
|
||||
}
|
||||
hub.emit(key)
|
||||
}
|
||||
|
||||
@Scheduled(fixedDelayString = "25000")
|
||||
fun heartbeat() {
|
||||
emitters.forEach { (_, list) ->
|
||||
val dead = mutableListOf<SseEmitter>()
|
||||
for (emitter in list) {
|
||||
try {
|
||||
emitter.send(SseEmitter.event().name("ping").data("ok"))
|
||||
} catch (_: IOException) {
|
||||
dead.add(emitter)
|
||||
}
|
||||
}
|
||||
if (dead.isNotEmpty()) {
|
||||
list.removeAll(dead.toSet())
|
||||
}
|
||||
}
|
||||
hub.heartbeat()
|
||||
}
|
||||
|
||||
private fun buildSnapshot(propertyId: UUID, guestId: UUID): List<GuestDocumentResponse> {
|
||||
|
||||
@@ -7,63 +7,28 @@ import com.android.trisolarisserver.repo.RoomStayRepo
|
||||
import org.springframework.scheduling.annotation.Scheduled
|
||||
import org.springframework.stereotype.Component
|
||||
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter
|
||||
import java.io.IOException
|
||||
import java.util.UUID
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import java.util.concurrent.CopyOnWriteArrayList
|
||||
|
||||
@Component
|
||||
class RoomBoardEvents(
|
||||
private val roomRepo: RoomRepo,
|
||||
private val roomStayRepo: RoomStayRepo
|
||||
) {
|
||||
private val emitters: MutableMap<UUID, CopyOnWriteArrayList<SseEmitter>> = ConcurrentHashMap()
|
||||
private val hub = SseHub<UUID>("room-board") { propertyId ->
|
||||
buildSnapshot(propertyId)
|
||||
}
|
||||
|
||||
fun subscribe(propertyId: UUID): SseEmitter {
|
||||
val emitter = SseEmitter(0L)
|
||||
emitters.computeIfAbsent(propertyId) { CopyOnWriteArrayList() }.add(emitter)
|
||||
emitter.onCompletion { emitters[propertyId]?.remove(emitter) }
|
||||
emitter.onTimeout { emitters[propertyId]?.remove(emitter) }
|
||||
emitter.onError { emitters[propertyId]?.remove(emitter) }
|
||||
try {
|
||||
emitter.send(SseEmitter.event().name("room-board").data(buildSnapshot(propertyId)))
|
||||
} catch (_: IOException) {
|
||||
emitters[propertyId]?.remove(emitter)
|
||||
}
|
||||
return emitter
|
||||
return hub.subscribe(propertyId)
|
||||
}
|
||||
|
||||
fun emit(propertyId: UUID) {
|
||||
val data = buildSnapshot(propertyId)
|
||||
val list = emitters[propertyId] ?: return
|
||||
val dead = mutableListOf<SseEmitter>()
|
||||
for (emitter in list) {
|
||||
try {
|
||||
emitter.send(SseEmitter.event().name("room-board").data(data))
|
||||
} catch (_: IOException) {
|
||||
dead.add(emitter)
|
||||
}
|
||||
}
|
||||
if (dead.isNotEmpty()) {
|
||||
list.removeAll(dead.toSet())
|
||||
}
|
||||
hub.emit(propertyId)
|
||||
}
|
||||
|
||||
@Scheduled(fixedDelayString = "25000")
|
||||
fun heartbeat() {
|
||||
emitters.forEach { (_, list) ->
|
||||
val dead = mutableListOf<SseEmitter>()
|
||||
for (emitter in list) {
|
||||
try {
|
||||
emitter.send(SseEmitter.event().name("ping").data("ok"))
|
||||
} catch (_: IOException) {
|
||||
dead.add(emitter)
|
||||
}
|
||||
}
|
||||
if (dead.isNotEmpty()) {
|
||||
list.removeAll(dead.toSet())
|
||||
}
|
||||
}
|
||||
hub.heartbeat()
|
||||
}
|
||||
|
||||
private fun buildSnapshot(propertyId: UUID): List<RoomBoardResponse> {
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.android.trisolarisserver.component
|
||||
|
||||
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter
|
||||
import java.io.IOException
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import java.util.concurrent.CopyOnWriteArrayList
|
||||
|
||||
class SseHub<K>(
|
||||
private val eventName: String,
|
||||
private val snapshot: (K) -> Any
|
||||
) {
|
||||
private val emitters: MutableMap<K, CopyOnWriteArrayList<SseEmitter>> = ConcurrentHashMap()
|
||||
|
||||
fun subscribe(key: K): SseEmitter {
|
||||
val emitter = SseEmitter(0L)
|
||||
emitters.computeIfAbsent(key) { CopyOnWriteArrayList() }.add(emitter)
|
||||
emitter.onCompletion { emitters[key]?.remove(emitter) }
|
||||
emitter.onTimeout { emitters[key]?.remove(emitter) }
|
||||
emitter.onError { emitters[key]?.remove(emitter) }
|
||||
try {
|
||||
emitter.send(SseEmitter.event().name(eventName).data(snapshot(key)))
|
||||
} catch (_: IOException) {
|
||||
emitters[key]?.remove(emitter)
|
||||
}
|
||||
return emitter
|
||||
}
|
||||
|
||||
fun emit(key: K) {
|
||||
val list = emitters[key] ?: return
|
||||
val data = snapshot(key)
|
||||
val dead = mutableListOf<SseEmitter>()
|
||||
for (emitter in list) {
|
||||
try {
|
||||
emitter.send(SseEmitter.event().name(eventName).data(data))
|
||||
} catch (_: IOException) {
|
||||
dead.add(emitter)
|
||||
}
|
||||
}
|
||||
if (dead.isNotEmpty()) {
|
||||
list.removeAll(dead.toSet())
|
||||
}
|
||||
}
|
||||
|
||||
fun heartbeat() {
|
||||
emitters.forEach { (_, list) ->
|
||||
val dead = mutableListOf<SseEmitter>()
|
||||
for (emitter in list) {
|
||||
try {
|
||||
emitter.send(SseEmitter.event().name("ping").data("ok"))
|
||||
} catch (_: IOException) {
|
||||
dead.add(emitter)
|
||||
}
|
||||
}
|
||||
if (dead.isNotEmpty()) {
|
||||
list.removeAll(dead.toSet())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user