kotlin - CUBA 平台将消息从后端推送到 UI
问题描述
我想知道是否可以将消息从后端(例如从外部系统接收信息的正在运行的任务)发送到 UI。在我的情况下,它需要是一个特定的会话(没有广播)并且只在一个特定的屏幕上
计划 B 会经常轮询后端,但我希望得到更“实时”的东西
我试图解决这样的问题,但我不断收到 NotSerializableException。
@Push
class StorageAccess : Screen(), MessageListener {
@Inject
private lateinit var stationWSService: StationWebSocketService
@Inject
private lateinit var notifications: Notifications
@Subscribe
private fun onInit(event: InitEvent) {
}
@Subscribe("stationPicker")
private fun onStationPickerValueChange(event: HasValue.ValueChangeEvent<StorageUnit>) {
val current = AppUI.getCurrent()
current.userSession.id ?: return
val prevValue = event.prevValue
if (prevValue != null) {
stationWSService.remove(current.userSession.id)
}
val value = event.value ?: return
stationWSService.listen(current.userSession.id, value, this)
}
override fun messageReceived(message: String) {
val current = AppUI.getCurrent()
current.access {
notifications.create().withCaption(message).show()
}
}
@Subscribe
private fun onAfterDetach(event: AfterDetachEvent) {
val current = AppUI.getCurrent()
current.userSession.id ?: return
stationWSService.remove(current.userSession.id)
}
}
-- 回调接口
interface MessageListener : Serializable {
fun messageReceived(message: String);
}
-- 我的后端服务的监听方法
private val listeners: MutableMap<String, MutableMap<UUID, MessageListener>> = HashMap()
override fun listen(id: UUID, storageUnit: StorageUnit, callback: MessageListener) {
val unitStationIP: String = storageUnit.unitStationIP ?: return
if (!listeners.containsKey(unitStationIP))
listeners[unitStationIP] = HashMap()
listeners[unitStationIP]?.set(id, callback)
}
我得到的异常是NotSerializableException: com.haulmont.cuba.web.sys.WebNotifications
在将侦听器添加到后端期间发生的:stationWSService.listen(current.userSession.id, value, this)
据我了解,这是 UI 将信息发送到后端的地方 - 以及 StorageAccess 类的整个状态,包括其所有成员。
有一个优雅的解决方案吗?
问候
解决方案
有一个插件可以解决这个问题:https ://github.com/cuba-platform/global-events-addon
推荐阅读
- macos - nginx: [emerg] bind() to 0.0.0.0:8080 failed (48: Address already in use) on mac
- user-interface - Maximo:将“创建新工作订单”按钮添加到标题栏?
- vectorization - Numba Vectorize TypeError:“类型”对象不可下标
- server - F11 键在 VMWARE 中的 ESXi 服务器上不起作用
- mysql - MySQL - 存储单个属性的不同数据类型的最佳方法是什么?
- apache-kafka - 启动 Kafka Connect Debezium
- powerbi - PowerBI 在切片器中显示截至最大日期的项目状态
- postman - IBM MQ..向主题发布消息
- node.js - 在没有循环的情况下使用 csv-parse
- python - 如何键入提示模块未公开的类