[英]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.