简体   繁体   中英

Dynamic interval in Flux / Mono

How to make a dynamic interval? when I put for example an atomic value in the interval it only works with the first value even when I change to a larger value, it works every 5s

I suggested this https://stackoverflow.com/a/47351359/8851815

public static void main(String[] args) throws InterruptedException {
        final AtomicInteger time = new AtomicInteger(0);
        System.out.println("START "+ LocalTime.now());
        Flux.interval(Duration.of(5,ChronoUnit.SECONDS))
                .startWith(0L)
                .delayUntil(s -> Mono.just(s).delayElement(Duration.of(time.get(),ChronoUnit.SECONDS)))
                .subscribe(s -> test(s,time));
        Thread.sleep(70000);

    }

    public static void test(long s, AtomicInteger time){
        try{
            if(s <= 1) {
                Thread.sleep(10000);
                time.addAndGet(5);
            } else {
                time.set(0);
            }
            System.out.println("TIME " +  LocalTime.now());
        } catch (Exception e){
            System.out.println(e);
        }

    }
}

Result:

START 15:18:55.710771800
TIME 15:19:06.356155800 ->10s
TIME 15:19:21.376629100 ->15s
TIME 15:19:41.385095    ->20s
TIME 15:19:56.400575500 ->5s
TIME 15:19:56.401578800 ->5s
TIME 15:19:56.402576700 ->5s
TIME 15:19:56.402576700 ->5s
TIME 15:19:56.403571400 ->5s
TIME 15:19:56.403571400 ->5s
TIME 15:19:56.404569800 ->5s
TIME 15:19:56.404569800 ->5s
TIME 15:19:56.404569800 ->5s
TIME 15:19:56.404569800 ->5s [how to skip / drop those requests?]
TIME 15:20:01.371023600 ->5s
TIME 15:20:06.360791800 ->5s

Your code is quite hard to understand, but basically what you want to achieve - is dynamic interval for delaying your Mono. It is possible, of course.

I'll simplify this with the following example:

public static void main(String[] args) throws InterruptedException {
    final AtomicInteger time = new AtomicInteger(0);
    System.out.println("START "+ LocalTime.now());
    Flux.interval(Duration.of(5,ChronoUnit.SECONDS))
            .startWith(0L)
            .delayUntil(s -> Mono.just(s).delayElement(randomDuration()))
            .subscribe(s -> test(s,time));

    Thread.sleep(70000);
}

// generate random Duration in millis between 3000 and 7000 ms
public static Duration randomDuration() {
    long min = 3000;
    long max = 7000;
    long randomMillis = ThreadLocalRandom.current().nextLong(min, max + 1);
    return Duration.ofMillis(randomMillis);
}

Which will give you output like this:

START 01:48:53.160028
TIME 01:48:59.786793
TIME 01:49:04.279440
TIME 01:49:07.771783
TIME 01:49:13.483046
TIME 01:49:17.003886
TIME 01:49:23.819252
TIME 01:49:29.021108
TIME 01:49:33.972711
TIME 01:49:40.832304
TIME 01:49:44.501199
TIME 01:49:51.029825
TIME 01:49:56.869309

You can see different intervals here

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