反应性WebFlux将通知发布到特定订户

问题描述

我想构建一个用户通知系统。想法是,如果用户已登录,则系统将订阅通知服务,并且在针对该用途生成通知时,系统必须返回新通知的数量。

我正在将Java Spring Boot与Webflux结合使用来实现这一目标。 我能够建立一个基本的示例,使用EmitterProcessor来工作,在该示例中,每次添加通知时,它都会根据用户标识符将新通知的数量发送给用户。

我运行该应用程序,并设置1个用户,说用户A,当我添加通知时,用户A收到更新,当我再添加2个用户(B和C)时,问题就开始了。当我为用户A创建通知时,用户B和C都会收到更新。

所以我的问题是,使用webflux是否可以将通知更新直接发送到代表正确用户的订阅者?

我的代码基础如下:

助焊剂处理器和FluxSink初始化

private final FluxProcessor processor;
private final FluxSink<Integer> sink;

public NotificationController() {

    this.processor = EmitterProcessor.create().serialize();
    this.sink = processor.sink();

}

订阅收件箱方法

@GetMapping(value = "/inbox/{userId}")
public Flux<ServerSentEvent> subscribeInbox(@PathVariable String userId) {

    Flux<ServerSentEvent> serverSentEventFlux = this.processor.map(e -> ServerSentEvent.builder(e).build());

    List<Notification> notificationList = this.repositoryMap.get(userId);
    if (notificationList == null) {
        notificationList = new ArrayList<>();
    }

    this.sink.next(notificationList.size());

    return serverSentEventFlux;

}

外部强制发布通知方法

@PostMapping(value = "/{userId}",produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity publishNotifications(@PathVariable String userId) {

    List<Notification> notificationList = this.repositoryMap.get(userId);
    if (notificationList == null) {
        notificationList = new ArrayList<>();
    }

    this.sink.next(notificationList.size());
    return ResponseEntity.ok().build();

}

预先感谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...