org.jgroups.util.ResponseCollectorTask Maven / Gradle / Ivy
Go to download
This artifact provides a single jar that contains all classes required to use remote EJB and JMS, including
all dependencies. It is intended for use by those not using maven, maven users should just import the EJB and
JMS BOM's instead (shaded JAR's cause lots of problems with maven, as it is very easy to inadvertently end up
with different versions on classes on the class path).
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;
}
}