How to create a stream of lists using Spring Webflux?
I am using Spring Webflux and ReactiveMongoRepository in a Spring Boot project and I want that each flux is a list of objects like below for example:
// 1st Flux Started:
[
{
// 1st Dashboard
},
{
// 2nd Dashboard
}
]
// 1st Flux Ended
// 2nd Flux Started:
[
{
// 1st Dashboard
},
{
// 2nd Dashboard
}
]
// 2nd Flux Ended
The repository:
@Repository
public interface ReactiveDashboardRepository extends ReactiveMongoRepository<Dashboard, String> {
}
The service:
@Service
public class ReactiveDashboardServiceImpl implements ReactiveDashboardService {
private ReactiveDashboardRepository reactiveDashboardRepository;
public ReactiveDashboardServiceImpl(ReactiveDashboardRepository reactiveDashboardRepository) {
this.reactiveDashboardRepository = reactiveDashboardRepository;
}
public Flux<Dashboard> getDashboards() {
return this.reactiveDashboardRepository.findAll();
}
}
The controller:
@CrossOrigin
@RestController
@RequestMapping("/api/sse")
public class ReactiveDashboardRestController {
private ReactiveDashboardService reactiveDashboardService;
public ReactiveDashboardRestController(ReactiveDashboardService reactiveDashboardService) {
this.reactiveDashboardService = reactiveDashboardService;
}
@GetMapping(value = "/dashboards", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<Dashboard> getDashboards() {
return this.reactiveDashboardService.getDashboards();
}
}
So basically I want all the object in the array to be part of a single stream because that is how the client app I will be using is designed. For now in total there are only 3 of them. So in the each stream will hold an array of all the object. I know that this isn't the best use of webflux.
How can this be achieved, if it can be achieved?
spring-webflux
add a comment |
I am using Spring Webflux and ReactiveMongoRepository in a Spring Boot project and I want that each flux is a list of objects like below for example:
// 1st Flux Started:
[
{
// 1st Dashboard
},
{
// 2nd Dashboard
}
]
// 1st Flux Ended
// 2nd Flux Started:
[
{
// 1st Dashboard
},
{
// 2nd Dashboard
}
]
// 2nd Flux Ended
The repository:
@Repository
public interface ReactiveDashboardRepository extends ReactiveMongoRepository<Dashboard, String> {
}
The service:
@Service
public class ReactiveDashboardServiceImpl implements ReactiveDashboardService {
private ReactiveDashboardRepository reactiveDashboardRepository;
public ReactiveDashboardServiceImpl(ReactiveDashboardRepository reactiveDashboardRepository) {
this.reactiveDashboardRepository = reactiveDashboardRepository;
}
public Flux<Dashboard> getDashboards() {
return this.reactiveDashboardRepository.findAll();
}
}
The controller:
@CrossOrigin
@RestController
@RequestMapping("/api/sse")
public class ReactiveDashboardRestController {
private ReactiveDashboardService reactiveDashboardService;
public ReactiveDashboardRestController(ReactiveDashboardService reactiveDashboardService) {
this.reactiveDashboardService = reactiveDashboardService;
}
@GetMapping(value = "/dashboards", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<Dashboard> getDashboards() {
return this.reactiveDashboardService.getDashboards();
}
}
So basically I want all the object in the array to be part of a single stream because that is how the client app I will be using is designed. For now in total there are only 3 of them. So in the each stream will hold an array of all the object. I know that this isn't the best use of webflux.
How can this be achieved, if it can be achieved?
spring-webflux
1
Which data do you want to convert into that form? Be more specific and give some code.
– uneq95
Nov 14 '18 at 2:31
I edited my question by adding extra information.
– KrisKris1
Nov 14 '18 at 10:57
Is their any attribute in Dashboard class, using which the flux can be grouped? If yes, then you can group together similar fluxes and finally get a flux of list of dashboards.
– uneq95
Nov 14 '18 at 11:46
add a comment |
I am using Spring Webflux and ReactiveMongoRepository in a Spring Boot project and I want that each flux is a list of objects like below for example:
// 1st Flux Started:
[
{
// 1st Dashboard
},
{
// 2nd Dashboard
}
]
// 1st Flux Ended
// 2nd Flux Started:
[
{
// 1st Dashboard
},
{
// 2nd Dashboard
}
]
// 2nd Flux Ended
The repository:
@Repository
public interface ReactiveDashboardRepository extends ReactiveMongoRepository<Dashboard, String> {
}
The service:
@Service
public class ReactiveDashboardServiceImpl implements ReactiveDashboardService {
private ReactiveDashboardRepository reactiveDashboardRepository;
public ReactiveDashboardServiceImpl(ReactiveDashboardRepository reactiveDashboardRepository) {
this.reactiveDashboardRepository = reactiveDashboardRepository;
}
public Flux<Dashboard> getDashboards() {
return this.reactiveDashboardRepository.findAll();
}
}
The controller:
@CrossOrigin
@RestController
@RequestMapping("/api/sse")
public class ReactiveDashboardRestController {
private ReactiveDashboardService reactiveDashboardService;
public ReactiveDashboardRestController(ReactiveDashboardService reactiveDashboardService) {
this.reactiveDashboardService = reactiveDashboardService;
}
@GetMapping(value = "/dashboards", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<Dashboard> getDashboards() {
return this.reactiveDashboardService.getDashboards();
}
}
So basically I want all the object in the array to be part of a single stream because that is how the client app I will be using is designed. For now in total there are only 3 of them. So in the each stream will hold an array of all the object. I know that this isn't the best use of webflux.
How can this be achieved, if it can be achieved?
spring-webflux
I am using Spring Webflux and ReactiveMongoRepository in a Spring Boot project and I want that each flux is a list of objects like below for example:
// 1st Flux Started:
[
{
// 1st Dashboard
},
{
// 2nd Dashboard
}
]
// 1st Flux Ended
// 2nd Flux Started:
[
{
// 1st Dashboard
},
{
// 2nd Dashboard
}
]
// 2nd Flux Ended
The repository:
@Repository
public interface ReactiveDashboardRepository extends ReactiveMongoRepository<Dashboard, String> {
}
The service:
@Service
public class ReactiveDashboardServiceImpl implements ReactiveDashboardService {
private ReactiveDashboardRepository reactiveDashboardRepository;
public ReactiveDashboardServiceImpl(ReactiveDashboardRepository reactiveDashboardRepository) {
this.reactiveDashboardRepository = reactiveDashboardRepository;
}
public Flux<Dashboard> getDashboards() {
return this.reactiveDashboardRepository.findAll();
}
}
The controller:
@CrossOrigin
@RestController
@RequestMapping("/api/sse")
public class ReactiveDashboardRestController {
private ReactiveDashboardService reactiveDashboardService;
public ReactiveDashboardRestController(ReactiveDashboardService reactiveDashboardService) {
this.reactiveDashboardService = reactiveDashboardService;
}
@GetMapping(value = "/dashboards", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<Dashboard> getDashboards() {
return this.reactiveDashboardService.getDashboards();
}
}
So basically I want all the object in the array to be part of a single stream because that is how the client app I will be using is designed. For now in total there are only 3 of them. So in the each stream will hold an array of all the object. I know that this isn't the best use of webflux.
How can this be achieved, if it can be achieved?
spring-webflux
spring-webflux
edited Nov 16 '18 at 8:38
cchantep
6,35732034
6,35732034
asked Nov 14 '18 at 0:30
KrisKris1KrisKris1
13
13
1
Which data do you want to convert into that form? Be more specific and give some code.
– uneq95
Nov 14 '18 at 2:31
I edited my question by adding extra information.
– KrisKris1
Nov 14 '18 at 10:57
Is their any attribute in Dashboard class, using which the flux can be grouped? If yes, then you can group together similar fluxes and finally get a flux of list of dashboards.
– uneq95
Nov 14 '18 at 11:46
add a comment |
1
Which data do you want to convert into that form? Be more specific and give some code.
– uneq95
Nov 14 '18 at 2:31
I edited my question by adding extra information.
– KrisKris1
Nov 14 '18 at 10:57
Is their any attribute in Dashboard class, using which the flux can be grouped? If yes, then you can group together similar fluxes and finally get a flux of list of dashboards.
– uneq95
Nov 14 '18 at 11:46
1
1
Which data do you want to convert into that form? Be more specific and give some code.
– uneq95
Nov 14 '18 at 2:31
Which data do you want to convert into that form? Be more specific and give some code.
– uneq95
Nov 14 '18 at 2:31
I edited my question by adding extra information.
– KrisKris1
Nov 14 '18 at 10:57
I edited my question by adding extra information.
– KrisKris1
Nov 14 '18 at 10:57
Is their any attribute in Dashboard class, using which the flux can be grouped? If yes, then you can group together similar fluxes and finally get a flux of list of dashboards.
– uneq95
Nov 14 '18 at 11:46
Is their any attribute in Dashboard class, using which the flux can be grouped? If yes, then you can group together similar fluxes and finally get a flux of list of dashboards.
– uneq95
Nov 14 '18 at 11:46
add a comment |
1 Answer
1
active
oldest
votes
If you want an infinite Flux that sends data with interval, you need the function "interval".
To simply your code I would recommend to return you Dashboards direct in List and transform the List in Flux in your Controller.
It will be something like:
List<Dashboard> lDashBoard = new ArrayList<Dashboard>(Arrays.asList(
Dashboard.builder().name("Dash1").build(),
Dashboard.builder().name("Dash2").build(),
Dashboard.builder().name("Dash3").build()
));
Flux<List<Dashboard>> intervalFlux = Flux
.interval(Duration.ofMillis(500))
.map(tick -> {
return lDashBoard;
});
intervalFlux.subscribe(t->log.info("Dashboards:{}", lDashBoard));
The output looks like:
2018-11-14 15:55:18.575 INFO [Dashboards:[Dashboard(name=Dash1), Dashboard(name=Dash2), Dashboard(name=Dash3)]
2018-11-14 15:55:19.073 INFO [Dashboards:[Dashboard(name=Dash1), Dashboard(name=Dash2), Dashboard(name=Dash3)]
2018-11-14 15:55:19.573 INFO [Dashboards:[Dashboard(name=Dash1), Dashboard(name=Dash2), Dashboard(name=Dash3)]
My idea is to push all the items to the client in one stream with an interval of 30 seconds between each stream. The flux stream will be an infinite stream. Do you have an idea how to work on this? I am really new to reactive programming in general I have mandatory to apply something like this in my project even if the solution doesn't make sense in based on the client logic.
– KrisKris1
Nov 14 '18 at 20:18
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%2f53291465%2fhow-to-create-a-stream-of-lists-using-spring-webflux%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
If you want an infinite Flux that sends data with interval, you need the function "interval".
To simply your code I would recommend to return you Dashboards direct in List and transform the List in Flux in your Controller.
It will be something like:
List<Dashboard> lDashBoard = new ArrayList<Dashboard>(Arrays.asList(
Dashboard.builder().name("Dash1").build(),
Dashboard.builder().name("Dash2").build(),
Dashboard.builder().name("Dash3").build()
));
Flux<List<Dashboard>> intervalFlux = Flux
.interval(Duration.ofMillis(500))
.map(tick -> {
return lDashBoard;
});
intervalFlux.subscribe(t->log.info("Dashboards:{}", lDashBoard));
The output looks like:
2018-11-14 15:55:18.575 INFO [Dashboards:[Dashboard(name=Dash1), Dashboard(name=Dash2), Dashboard(name=Dash3)]
2018-11-14 15:55:19.073 INFO [Dashboards:[Dashboard(name=Dash1), Dashboard(name=Dash2), Dashboard(name=Dash3)]
2018-11-14 15:55:19.573 INFO [Dashboards:[Dashboard(name=Dash1), Dashboard(name=Dash2), Dashboard(name=Dash3)]
My idea is to push all the items to the client in one stream with an interval of 30 seconds between each stream. The flux stream will be an infinite stream. Do you have an idea how to work on this? I am really new to reactive programming in general I have mandatory to apply something like this in my project even if the solution doesn't make sense in based on the client logic.
– KrisKris1
Nov 14 '18 at 20:18
add a comment |
If you want an infinite Flux that sends data with interval, you need the function "interval".
To simply your code I would recommend to return you Dashboards direct in List and transform the List in Flux in your Controller.
It will be something like:
List<Dashboard> lDashBoard = new ArrayList<Dashboard>(Arrays.asList(
Dashboard.builder().name("Dash1").build(),
Dashboard.builder().name("Dash2").build(),
Dashboard.builder().name("Dash3").build()
));
Flux<List<Dashboard>> intervalFlux = Flux
.interval(Duration.ofMillis(500))
.map(tick -> {
return lDashBoard;
});
intervalFlux.subscribe(t->log.info("Dashboards:{}", lDashBoard));
The output looks like:
2018-11-14 15:55:18.575 INFO [Dashboards:[Dashboard(name=Dash1), Dashboard(name=Dash2), Dashboard(name=Dash3)]
2018-11-14 15:55:19.073 INFO [Dashboards:[Dashboard(name=Dash1), Dashboard(name=Dash2), Dashboard(name=Dash3)]
2018-11-14 15:55:19.573 INFO [Dashboards:[Dashboard(name=Dash1), Dashboard(name=Dash2), Dashboard(name=Dash3)]
My idea is to push all the items to the client in one stream with an interval of 30 seconds between each stream. The flux stream will be an infinite stream. Do you have an idea how to work on this? I am really new to reactive programming in general I have mandatory to apply something like this in my project even if the solution doesn't make sense in based on the client logic.
– KrisKris1
Nov 14 '18 at 20:18
add a comment |
If you want an infinite Flux that sends data with interval, you need the function "interval".
To simply your code I would recommend to return you Dashboards direct in List and transform the List in Flux in your Controller.
It will be something like:
List<Dashboard> lDashBoard = new ArrayList<Dashboard>(Arrays.asList(
Dashboard.builder().name("Dash1").build(),
Dashboard.builder().name("Dash2").build(),
Dashboard.builder().name("Dash3").build()
));
Flux<List<Dashboard>> intervalFlux = Flux
.interval(Duration.ofMillis(500))
.map(tick -> {
return lDashBoard;
});
intervalFlux.subscribe(t->log.info("Dashboards:{}", lDashBoard));
The output looks like:
2018-11-14 15:55:18.575 INFO [Dashboards:[Dashboard(name=Dash1), Dashboard(name=Dash2), Dashboard(name=Dash3)]
2018-11-14 15:55:19.073 INFO [Dashboards:[Dashboard(name=Dash1), Dashboard(name=Dash2), Dashboard(name=Dash3)]
2018-11-14 15:55:19.573 INFO [Dashboards:[Dashboard(name=Dash1), Dashboard(name=Dash2), Dashboard(name=Dash3)]
If you want an infinite Flux that sends data with interval, you need the function "interval".
To simply your code I would recommend to return you Dashboards direct in List and transform the List in Flux in your Controller.
It will be something like:
List<Dashboard> lDashBoard = new ArrayList<Dashboard>(Arrays.asList(
Dashboard.builder().name("Dash1").build(),
Dashboard.builder().name("Dash2").build(),
Dashboard.builder().name("Dash3").build()
));
Flux<List<Dashboard>> intervalFlux = Flux
.interval(Duration.ofMillis(500))
.map(tick -> {
return lDashBoard;
});
intervalFlux.subscribe(t->log.info("Dashboards:{}", lDashBoard));
The output looks like:
2018-11-14 15:55:18.575 INFO [Dashboards:[Dashboard(name=Dash1), Dashboard(name=Dash2), Dashboard(name=Dash3)]
2018-11-14 15:55:19.073 INFO [Dashboards:[Dashboard(name=Dash1), Dashboard(name=Dash2), Dashboard(name=Dash3)]
2018-11-14 15:55:19.573 INFO [Dashboards:[Dashboard(name=Dash1), Dashboard(name=Dash2), Dashboard(name=Dash3)]
edited Nov 14 '18 at 21:03
answered Nov 14 '18 at 20:10
P.RousseauP.Rousseau
406
406
My idea is to push all the items to the client in one stream with an interval of 30 seconds between each stream. The flux stream will be an infinite stream. Do you have an idea how to work on this? I am really new to reactive programming in general I have mandatory to apply something like this in my project even if the solution doesn't make sense in based on the client logic.
– KrisKris1
Nov 14 '18 at 20:18
add a comment |
My idea is to push all the items to the client in one stream with an interval of 30 seconds between each stream. The flux stream will be an infinite stream. Do you have an idea how to work on this? I am really new to reactive programming in general I have mandatory to apply something like this in my project even if the solution doesn't make sense in based on the client logic.
– KrisKris1
Nov 14 '18 at 20:18
My idea is to push all the items to the client in one stream with an interval of 30 seconds between each stream. The flux stream will be an infinite stream. Do you have an idea how to work on this? I am really new to reactive programming in general I have mandatory to apply something like this in my project even if the solution doesn't make sense in based on the client logic.
– KrisKris1
Nov 14 '18 at 20:18
My idea is to push all the items to the client in one stream with an interval of 30 seconds between each stream. The flux stream will be an infinite stream. Do you have an idea how to work on this? I am really new to reactive programming in general I have mandatory to apply something like this in my project even if the solution doesn't make sense in based on the client logic.
– KrisKris1
Nov 14 '18 at 20:18
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%2f53291465%2fhow-to-create-a-stream-of-lists-using-spring-webflux%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
1
Which data do you want to convert into that form? Be more specific and give some code.
– uneq95
Nov 14 '18 at 2:31
I edited my question by adding extra information.
– KrisKris1
Nov 14 '18 at 10:57
Is their any attribute in Dashboard class, using which the flux can be grouped? If yes, then you can group together similar fluxes and finally get a flux of list of dashboards.
– uneq95
Nov 14 '18 at 11:46