All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jgroups.util.ResponseCollectorTask Maven / Gradle / Ivy

There is a newer version: 5.3.13.Final
Show newest version
package org.jgroups.util;

import org.jgroups.Address;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

/**
 * Task which is seeded with an initial membership. Periodically executes a runnable (which e.g. sends a message) and
 * stops when responses from all members have been received, or the task is stopped.
 * @author Bela Ban
 * @since  4.0.5
 */
public class ResponseCollectorTask extends ResponseCollector {
    protected Consumer> periodic_task, finalizer_task; // run periodically until the task has been completed
    protected Future                          runner;
    protected final Runnable                     stub=() -> periodic_task.accept(this);

    public ResponseCollectorTask() {
    }


    public ResponseCollectorTask(Collection
members) { super(members); } public ResponseCollectorTask(Address... members) { super(members); } public ResponseCollectorTask setPeriodicTask(Consumer> pt) {this.periodic_task=pt; return this;} public ResponseCollectorTask setFinalizerTask(Consumer> r) {this.finalizer_task=r; return this;} public synchronized boolean isDone() {return runner == null || runner.isDone();} public synchronized ResponseCollectorTask start(TimeScheduler timer, long initial_delay, long interval) { if(hasAllResponses()) return this; if(periodic_task != null && (runner == null || runner.isDone())) runner=timer.scheduleAtFixedRate(stub, initial_delay, interval, TimeUnit.MILLISECONDS); return this; } public synchronized ResponseCollectorTask stop() { if(runner != null) { runner.cancel(true); runner=null; if(finalizer_task != null) finalizer_task.accept(this); } return this; } public boolean add(Address member, T data) { boolean retval; if((retval=super.add(member, data)) && hasAllResponses()) stop(); return retval; } public boolean retainAll(List
members) { boolean retval=super.retainAll(members); if(retval && this.hasAllResponses()) stop(); return retval; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy