简体   繁体   中英

How to retrieve data from Mono<T> on java

I have an entity like this:

public class Period implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    private String id;
    private PeriodType type;
    private Date startDate;
    private Date endDate;
    private Long lastQuranId;
    private Long lastUvoiceId;
    private List<Integer> exclusiveContentTypeOrder;
}

and i have

Mono<Period> periodMono = periodReactiveRepository.findOneByTypeAndStartDateLessThanEqualAndEndDateGreaterThanEqual(type,
                currentDate, currentDate);

My question is if I want to get StartDate how to get that?

I have try use this

Date startDate = (Date) periodMono.subscribe(period-> period.getStartDate());

My question is this code it is correct or not? If not how to get StartDate from my Mono ?

Blocking way:

Mono completes successfully by emitting an element at some point in time.

Date startDate = periodMono.block().getStartDate();

When you are calling block method, it will be block until result arrive. So actually you may miss the reactive non blocking feature. To get more control, you can set an explicit duration also.

Non-Blocking Way

As you mentioned you can get in a non-blocking way using the subscribe() method.

periodMono.subscribe(period-> {Date startDate = period.getStartDate(); //continue ur operation });

you cannot cast Disposable into date object. Ref

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM