-2
\$\begingroup\$

Firstly, take in account is my first question here in CodeReview. Any purpose misuderstanding will be highly appreciated if orientaded.

Scenario: I want to answer randomly a pojo (json with two fields) to Angular using a WebFlux restservice. I can succesfully answer an integer but I got stuck while trying retruning a model. On top of that I am using Server Sent Events but I don't think it is relevant to this question.

Successfully done with an Integer:

@GetMapping(value = "/randomNumbers", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<ServerSentEvent<Integer>> randomNumbers() {
    return Flux.interval(Duration.ofSeconds(1)).map(seq -> Tuples.of(seq, ThreadLocalRandom.current().nextInt()))
            .map(data -> ServerSentEvent.<Integer>builder().event("random").id(Long.toString(data.getT1()))
                    .data(data.getT2()).build());
}

Completely stuck trying to follow same approach to answer a status model:

    @GetMapping(value = "/randomStatus", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<ServerSentEvent<Status>> randomStatus() {

        ArrayList<String> statusList = new ArrayList<String>();
        statusList.add("Iniciado");
        statusList.add("Recusado");
        statusList.add("Sucesso");

//        for (int i = 0; i < 100; i++) {
//            int x = ThreadLocalRandom.current().nextInt(0, statusList.size());
//            System.out.println(statusList.get(x));
//        }

//the next line it is not working but I pasted to show where I am stuck. Let's say I can't create a new Status object return Flux.interval(Duration.ofSeconds(1)) .map(seq -> Tuples.of(seq, ThreadLocalRandom.current().nextInt())) .map(data -> ServerSentEvent.builder().event("random").data(new Status(Long.toString(data.getT1()), ThreadLocalRandom.current().nextInt(0, statusList.size() ) .data(data.getT2()).build()))); }

}

In case it is relevant, here is Status model:

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Document
public class Status {
    @Id
    private String id;
    private String status;

... getters/setters/contructor hidden for simplicity

Imports in case it is relevants are:

import java.time.Duration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import com.reactive.fluxdemo.domain.Status;
import com.reactive.fluxdemo.domain.Transfer;
import com.reactive.fluxdemo.repository.StatusRepository;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.util.function.Tuples;

import java.util.ArrayList;
import java.util.concurrent.ThreadLocalRandom;
\$\endgroup\$
2
  • 3
    \$\begingroup\$ Hello, this question is off-topic, since the code is not working as intended; I suggest that you read the What topics can I ask about here? Code Review aims to help improve working code. If you are trying to figure out why your program crashes or produces a wrong result, ask on Stack Overflow instead. Code Review is also not the place to ask for implementing new features. \$\endgroup\$ Commented Apr 8, 2020 at 21:18
  • 1
    \$\begingroup\$ Sorry, I will take this in account next time. \$\endgroup\$ Commented Apr 8, 2020 at 22:22

1 Answer 1

1
\$\begingroup\$

I think I didn't write my question folloing the expectations of this forum. BTW, here is the solution for future readers.

I wonder if this is the best way to achive it since I am creating a new instance (new Status) each time before the flux is closed. I would appreciated if someone can comment on bellow code.

Honestly I still stumble on Lambda skill. Hope it can be usefull for future readers.

@GetMapping(value = "/randomStatus", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<ServerSentEvent<Status>> randomStatus() {

    ArrayList<String> statusList = new ArrayList<String>();
    statusList.add("Iniciado");
    statusList.add("Recusado");
    statusList.add("Sucesso");

    return Flux.interval(Duration.ofSeconds(1))
            .map(seq -> Tuples.of(seq, ThreadLocalRandom.current().nextInt()))
            .map(data -> ServerSentEvent.<Status>builder().data(
                    new Status(Long.toString(data.getT1()),statusList.get(ThreadLocalRandom.current().nextInt(0, statusList.size() ))
                    ))
                    .build());

}
\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.