Is there a better way to do this? I would rather use an existing framework from j.u.c instead of rolling my own….
Please look at the “run” method.
I need to resubmit the Callable as soon as a result is obtained from the Future.
I’m using this separate Runnable Thread, just to do exactly this, as shown below.
Though this should compile, its obviously not a working piece of code..
public class ResubmitOnCompletion implements Runnable {
private CompletionService<DeviceData> completionService;
private ConcurrentHashMap<String, Device> devices;
public void run() {
//-- keep resubmitting the callable.
while(true){
try {
//-- Get the returned data from completed job
Future<DeviceData> f=null;
DeviceData dd=null;
f = completionService.take();
//-- Get the completed job's data
dd=f.get();
//-- now resubmit the job if device still is part of the active device collection
completionService.submit((Callable<DeviceData>) devices.get(dd.getDeviceId()));
}
catch (CancellationException e) {
e.printStackTrace();
}
catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
//-- Device Data
private class DeviceData {
/**
* Unique Device Identifier
*/
private final String deviceId;
/**
* Holds all numeric data of the device.
* Key=Variable id of the parameter
* Value=Numeric value of the parameter
*/
private final ConcurrentHashMap<String, BigDecimal> numericData;
private DeviceData(String deviceId,
ConcurrentHashMap<String, BigDecimal> numericData) {
super();
this.deviceId = deviceId;
this.numericData = numericData;
}
/**
* @return the deviceId
*/
public String getDeviceId() {
return deviceId;
}
/**
* @return the numericData
*/
public ConcurrentHashMap<String, BigDecimal> getNumericData() {
return numericData;
}
}
//-- Device
private class Device {
public DeviceData call() throws Exception {
return new DeviceData("",new ConcurrentHashMap<String, BigDecimal>());
}
}
}
You can simply submit the Runnable and within the run re submit itself
I am unsure why this would not work.