简体   繁体   English

RSocket适用于生成的数据,但不适用于Spring Reactive MongoDB

[英]RSocket works with generated data but not with Spring Reactive MongoDB

Resolution summary: 决议摘要:

In most of the RSocket examples currently out there the server side acceptor is simply constructed as a new object (like new MqttMessageService() below) even in SpringBoot related tutorials. 在当前的大多数RSocket示例中,即使在SpringBoot相关教程中,服务器端接受器也只是构造为一个新对象(如下面的新MqttMessageService())。 Which is fine if you generate example content right in the acceptor class but might lead to the below dependency injection related confusion when the acceptor depends on other beans in the container. 如果您在接受器类中生成示例内容,但是当接受器依赖于容器中的其他bean时,可能会导致下面的依赖注入相关混淆。

Original question: 原始问题:

I get a NullPointerException when trying to stream database entries using a Spring Data Reactive Mongodb repository via Rsocket's Java server. 尝试使用Spring Data Reactive Mongodb存储库通过Rsocket的Java服务器流式传输数据库条目时,我得到NullPointerException。

The problem is that during debugging all components work separately : I can get the requested data via the same Mongodb repository and I can also stream random generated data between the same server and client using Rsocket. 问题是在调试期间所有组件都单独工作 :我可以通过相同的Mongodb存储库获取所请求的数据,我也可以使用Rsocket在同一服务器和客户端之间传输随机生成的数据。

So I'm either missing something really basic or there might be an issue with using Reactive Mongodb and Rsocket together. 所以我要么缺少一些非常基本的东西,要么将Reactive Mongodb和Rsocket一起使用可能存在问题。

Here is the original server side Rsocket configuration : 这是原始服务器端Rsocket配置

@Configuration
public class RsocketConfig {

    @PostConstruct
    public void startServer() {
        RSocketFactory.receive()
                .acceptor((setup, sendingSocket) -> Mono.just(new MqttMessageService()))
                .transport(TcpServerTransport.create(8802))
                .start()
                .block()
                .onClose()
    }
}

And here is the working server side Rsocket configuration with proper DI: 这是工作服务器端Rsocket配置与适当的DI:

@Configuration
public class RsocketConfig {

    @Autowired
    MqttMessageService messageService;

    @PostConstruct
    public void startServer() {
        RSocketFactory.receive()
                .acceptor((setup, sendingSocket) -> Mono.just(messageService))
                .transport(TcpServerTransport.create(8802))
                .start()
                .block()
                .onClose()
    }
}

Here is the server side AbstractRSocket implementation where a NullPointerException is thrown at return service.findAll(). 这是服务器端的AbstractRSocket实现 ,其中返回service.findAll()时抛出NullPointerException。

@Service
public class MqttMessageService extends AbstractRSocket {



    @Autowired 
    private MqttMessageEntityService service;

    @Override
    public Flux<Payload> requestStream(Payload payload) {
        return service.findAll()
            .map(mqttMessageEntity -> DefaultPayload.create(mqttMessageEntity.toString()));

    }
}

Here are the reactive repository and the related service. 这是反应式存储库和相关服务。 The service returns null when injected to the server's AbstractRSocket implementation, but works fine when injected into other classes: 注入到服务器的AbstractRSocket实现时,该服务返回null,但在注入其他类时工作正常:

@Service
public class MqttMessageEntityService {

    @Autowired
    private MqttMessageEntityRepository repository;

    public Flux<MqttMessageEntity> findAll() {
        return repository.findAll();
    }

}

public interface MqttMessageEntityRepository extends ReactiveMongoRepository<MqttMessageEntity, String> {

}

And here is the client side code that works perfectly with the test contents: 以下是与测试内容完美配合的客户端代码:

@Configuration
public class RsocketConfig {

    @PostConstruct
    public void testRsocket() {

        RSocket rSocketClient = RSocketFactory
                .connect()
                .transport(TcpClientTransport.create(8802))
                .start()
                .block();

        rSocketClient
                .requestStream(DefaultPayload.create(""))
                .blockLast();
    }        
}

I might be a little over my knowledge level here and resources are very limited on the topic so I appreciate any hints towards the solution :) 我可能在这方面有点超过我的知识水平,资源在这个主题上非常有限,所以我感谢任何有关解决方案的提示:)

Regarding 关于

@PostConstruct
public void startServer() {
    RSocketFactory.receive()
            .acceptor((setup, sendingSocket) -> Mono.just(new MqttMessageService()))
            .transport(TcpServerTransport.create(8802))
            .start()
            .block()
            .onClose();
}

Are you using the to keep the server alive? 您是否使用它来保持服务器活着? If so add another block after the onClose(). 如果是这样,在onClose()之后添加另一个块。

Is messageEntityService null? messageEntityService是否为空? Because that looks like the only thing that could cause an error if the variables topicStart and module aren't. 因为如果变量topicStart和module不是唯一可能导致错误的东西。 Especially if the other code works - I can't really see anything that would cause a problem from the RSocket side. 特别是如果其他代码有效 - 我无法真正看到任何可能导致RSocket方面出现问题的内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 spring data react mongodb:如何在insertAll()失败后检索生成的ID - spring data reactive mongodb: How to retrieve generated IDs after failed insertAll() 在 Spring Data Mongodb Reactive 中执行顺序查询 - Execute sequencial queries in Spring Data Mongodb Reactive Spring WebFlux:Spring Data Reactive MongoDB中的block()方法返回null - Spring WebFlux: block() method return null in Spring Data Reactive MongoDB 响应式Spring Data Mongodb查询在不应该返回旧数据的情况下 - Reactive Spring Data Mongodb Query is returning old data when it should not 如何使用Spring data-mongodb-reactive从受限制的集合中流式传输 - How to stream from a capped collection with Spring data-mongodb-reactive 如何将数据库引用与反应式 Spring Data MongoDB 一起使用? - How to use db references with reactive Spring Data MongoDB? 等待无功更改 ZF7B44CFFAFD5C52223D5498196C8A2E7BZ 订阅激活 Spring 数据 MongoDB? - Wait for reactive change stream subscription to be active with Spring Data MongoDB? Spring Data Mongodb Near Query 不起作用 - Spring Data Mongodb Near Query not works 更新 1 个或多个特定字段 MongoDB 使用 Spring 启动 WebFlux,Spring 数据 Z206E3718AF0921CC1D12F80ZReposit7 - Update 1 or multiple specific field MongoDB using Spring boot WebFlux,Spring Data MongoDB Reactive and ReactiveMongoRepository Java RSocket 客户端与 Spring RSocket 通道连接 - Java RSocket client connect with Spring RSocket channel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM