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. 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.
Original question:
I get a NullPointerException when trying to stream database entries using a Spring Data Reactive Mongodb repository via Rsocket's Java server.
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.
So I'm either missing something really basic or there might be an issue with using Reactive Mongodb and Rsocket together.
Here is the original server side Rsocket configuration:
@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:
@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().
@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:
@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 :)
java mongodb spring-boot spring-data rsocket
add a comment |
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. 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.
Original question:
I get a NullPointerException when trying to stream database entries using a Spring Data Reactive Mongodb repository via Rsocket's Java server.
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.
So I'm either missing something really basic or there might be an issue with using Reactive Mongodb and Rsocket together.
Here is the original server side Rsocket configuration:
@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:
@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().
@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:
@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 :)
java mongodb spring-boot spring-data rsocket
add a comment |
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. 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.
Original question:
I get a NullPointerException when trying to stream database entries using a Spring Data Reactive Mongodb repository via Rsocket's Java server.
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.
So I'm either missing something really basic or there might be an issue with using Reactive Mongodb and Rsocket together.
Here is the original server side Rsocket configuration:
@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:
@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().
@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:
@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 :)
java mongodb spring-boot spring-data rsocket
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. 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.
Original question:
I get a NullPointerException when trying to stream database entries using a Spring Data Reactive Mongodb repository via Rsocket's Java server.
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.
So I'm either missing something really basic or there might be an issue with using Reactive Mongodb and Rsocket together.
Here is the original server side Rsocket configuration:
@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:
@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().
@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:
@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 :)
java mongodb spring-boot spring-data rsocket
java mongodb spring-boot spring-data rsocket
edited Feb 7 at 15:02
Péter Veres
asked Jan 1 at 5:17
Péter VeresPéter Veres
285
285
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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().
Is messageEntityService null? Because that looks like the only thing that could cause an error if the variables topicStart and module aren't. Especially if the other code works - I can't really see anything that would cause a problem from the RSocket side.
Oh, indeed you are right! The messageEntityService does not return a null value, the service itself is null Which is unexpected as it works perfectly when injected it to a different class. Do you have any idea while this could be happening? I'm updating my question with more details on the reactive mongo repository and the messageEntityService as it might still be relevant under this question.
– Péter Veres
Feb 3 at 18:42
1
I think this thread may help you figure out why Spring is injecting a null in your @Autowired variable: stackoverflow.com/questions/19896870/…
– Robert Roeser
Feb 5 at 22:03
Haha, thanks! The first thing that I've checked was if the service is managed by the container, but missed the part where I should have injected it as the server side acceptor, instead of creating a new instance outside of the Spring container. I was a bit confused since all the examples I've seen even the ones involving SpringBoot were using this way of adding the acceptor - but I just realized now that in those cases it didn't really matter if it's maintained by spring since they all used data generated within the acceptor and didn't depend on other services (like the db in my case)
– Péter Veres
Feb 7 at 14:31
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53993152%2frsocket-works-with-generated-data-but-not-with-spring-reactive-mongodb%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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().
Is messageEntityService null? Because that looks like the only thing that could cause an error if the variables topicStart and module aren't. Especially if the other code works - I can't really see anything that would cause a problem from the RSocket side.
Oh, indeed you are right! The messageEntityService does not return a null value, the service itself is null Which is unexpected as it works perfectly when injected it to a different class. Do you have any idea while this could be happening? I'm updating my question with more details on the reactive mongo repository and the messageEntityService as it might still be relevant under this question.
– Péter Veres
Feb 3 at 18:42
1
I think this thread may help you figure out why Spring is injecting a null in your @Autowired variable: stackoverflow.com/questions/19896870/…
– Robert Roeser
Feb 5 at 22:03
Haha, thanks! The first thing that I've checked was if the service is managed by the container, but missed the part where I should have injected it as the server side acceptor, instead of creating a new instance outside of the Spring container. I was a bit confused since all the examples I've seen even the ones involving SpringBoot were using this way of adding the acceptor - but I just realized now that in those cases it didn't really matter if it's maintained by spring since they all used data generated within the acceptor and didn't depend on other services (like the db in my case)
– Péter Veres
Feb 7 at 14:31
add a comment |
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().
Is messageEntityService null? Because that looks like the only thing that could cause an error if the variables topicStart and module aren't. Especially if the other code works - I can't really see anything that would cause a problem from the RSocket side.
Oh, indeed you are right! The messageEntityService does not return a null value, the service itself is null Which is unexpected as it works perfectly when injected it to a different class. Do you have any idea while this could be happening? I'm updating my question with more details on the reactive mongo repository and the messageEntityService as it might still be relevant under this question.
– Péter Veres
Feb 3 at 18:42
1
I think this thread may help you figure out why Spring is injecting a null in your @Autowired variable: stackoverflow.com/questions/19896870/…
– Robert Roeser
Feb 5 at 22:03
Haha, thanks! The first thing that I've checked was if the service is managed by the container, but missed the part where I should have injected it as the server side acceptor, instead of creating a new instance outside of the Spring container. I was a bit confused since all the examples I've seen even the ones involving SpringBoot were using this way of adding the acceptor - but I just realized now that in those cases it didn't really matter if it's maintained by spring since they all used data generated within the acceptor and didn't depend on other services (like the db in my case)
– Péter Veres
Feb 7 at 14:31
add a comment |
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().
Is messageEntityService null? Because that looks like the only thing that could cause an error if the variables topicStart and module aren't. Especially if the other code works - I can't really see anything that would cause a problem from the RSocket side.
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().
Is messageEntityService null? Because that looks like the only thing that could cause an error if the variables topicStart and module aren't. Especially if the other code works - I can't really see anything that would cause a problem from the RSocket side.
answered Jan 29 at 7:03
Robert RoeserRobert Roeser
362
362
Oh, indeed you are right! The messageEntityService does not return a null value, the service itself is null Which is unexpected as it works perfectly when injected it to a different class. Do you have any idea while this could be happening? I'm updating my question with more details on the reactive mongo repository and the messageEntityService as it might still be relevant under this question.
– Péter Veres
Feb 3 at 18:42
1
I think this thread may help you figure out why Spring is injecting a null in your @Autowired variable: stackoverflow.com/questions/19896870/…
– Robert Roeser
Feb 5 at 22:03
Haha, thanks! The first thing that I've checked was if the service is managed by the container, but missed the part where I should have injected it as the server side acceptor, instead of creating a new instance outside of the Spring container. I was a bit confused since all the examples I've seen even the ones involving SpringBoot were using this way of adding the acceptor - but I just realized now that in those cases it didn't really matter if it's maintained by spring since they all used data generated within the acceptor and didn't depend on other services (like the db in my case)
– Péter Veres
Feb 7 at 14:31
add a comment |
Oh, indeed you are right! The messageEntityService does not return a null value, the service itself is null Which is unexpected as it works perfectly when injected it to a different class. Do you have any idea while this could be happening? I'm updating my question with more details on the reactive mongo repository and the messageEntityService as it might still be relevant under this question.
– Péter Veres
Feb 3 at 18:42
1
I think this thread may help you figure out why Spring is injecting a null in your @Autowired variable: stackoverflow.com/questions/19896870/…
– Robert Roeser
Feb 5 at 22:03
Haha, thanks! The first thing that I've checked was if the service is managed by the container, but missed the part where I should have injected it as the server side acceptor, instead of creating a new instance outside of the Spring container. I was a bit confused since all the examples I've seen even the ones involving SpringBoot were using this way of adding the acceptor - but I just realized now that in those cases it didn't really matter if it's maintained by spring since they all used data generated within the acceptor and didn't depend on other services (like the db in my case)
– Péter Veres
Feb 7 at 14:31
Oh, indeed you are right! The messageEntityService does not return a null value, the service itself is null Which is unexpected as it works perfectly when injected it to a different class. Do you have any idea while this could be happening? I'm updating my question with more details on the reactive mongo repository and the messageEntityService as it might still be relevant under this question.
– Péter Veres
Feb 3 at 18:42
Oh, indeed you are right! The messageEntityService does not return a null value, the service itself is null Which is unexpected as it works perfectly when injected it to a different class. Do you have any idea while this could be happening? I'm updating my question with more details on the reactive mongo repository and the messageEntityService as it might still be relevant under this question.
– Péter Veres
Feb 3 at 18:42
1
1
I think this thread may help you figure out why Spring is injecting a null in your @Autowired variable: stackoverflow.com/questions/19896870/…
– Robert Roeser
Feb 5 at 22:03
I think this thread may help you figure out why Spring is injecting a null in your @Autowired variable: stackoverflow.com/questions/19896870/…
– Robert Roeser
Feb 5 at 22:03
Haha, thanks! The first thing that I've checked was if the service is managed by the container, but missed the part where I should have injected it as the server side acceptor, instead of creating a new instance outside of the Spring container. I was a bit confused since all the examples I've seen even the ones involving SpringBoot were using this way of adding the acceptor - but I just realized now that in those cases it didn't really matter if it's maintained by spring since they all used data generated within the acceptor and didn't depend on other services (like the db in my case)
– Péter Veres
Feb 7 at 14:31
Haha, thanks! The first thing that I've checked was if the service is managed by the container, but missed the part where I should have injected it as the server side acceptor, instead of creating a new instance outside of the Spring container. I was a bit confused since all the examples I've seen even the ones involving SpringBoot were using this way of adding the acceptor - but I just realized now that in those cases it didn't really matter if it's maintained by spring since they all used data generated within the acceptor and didn't depend on other services (like the db in my case)
– Péter Veres
Feb 7 at 14:31
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53993152%2frsocket-works-with-generated-data-but-not-with-spring-reactive-mongodb%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown