Updated to fix the cancellation of task
git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/portlets/admin/accounting-manager@142602 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
9d79ef0cf0
commit
c701df8aef
|
@ -1,6 +1,7 @@
|
|||
package org.gcube.portlets.admin.accountingmanager.server;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
@ -13,6 +14,7 @@ import java.util.concurrent.TimeoutException;
|
|||
import javax.servlet.ServletContextEvent;
|
||||
|
||||
import org.gcube.portlets.admin.accountingmanager.server.amservice.cache.AccountingCache;
|
||||
import org.gcube.portlets.admin.accountingmanager.server.util.TaskInProgress;
|
||||
import org.gcube.portlets.admin.accountingmanager.server.util.TaskRequest;
|
||||
import org.gcube.portlets.admin.accountingmanager.server.util.TaskStatus;
|
||||
import org.gcube.portlets.admin.accountingmanager.shared.Constants;
|
||||
|
@ -32,13 +34,13 @@ public class AccountingClientDaemon implements Runnable {
|
|||
private ServletContextEvent sce;
|
||||
private volatile boolean running = true;
|
||||
private volatile AccountingCache accountingCache;
|
||||
private ArrayList<Future<TaskStatus>> workers;
|
||||
private ArrayList<TaskInProgress> tasks;
|
||||
|
||||
public AccountingClientDaemon(ServletContextEvent sce,
|
||||
AccountingCache accountingCache) {
|
||||
this.sce = sce;
|
||||
this.accountingCache = accountingCache;
|
||||
workers = new ArrayList<>();
|
||||
tasks = new ArrayList<>();
|
||||
}
|
||||
|
||||
public void terminate() {
|
||||
|
@ -62,18 +64,21 @@ public class AccountingClientDaemon implements Runnable {
|
|||
taskRequest, accountingCache);
|
||||
Future<TaskStatus> futureResult = executorService
|
||||
.submit(accountingClientCallable);
|
||||
workers.add(futureResult);
|
||||
|
||||
TaskInProgress taskInProgress = new TaskInProgress(
|
||||
new GregorianCalendar(), futureResult);
|
||||
tasks.add(taskInProgress);
|
||||
}
|
||||
|
||||
if (!workers.isEmpty()) {
|
||||
ArrayList<Future<TaskStatus>> dones=new ArrayList<>();
|
||||
for (Future<TaskStatus> futureResult : workers) {
|
||||
if (!tasks.isEmpty()) {
|
||||
ArrayList<TaskInProgress> dones = new ArrayList<>();
|
||||
for (TaskInProgress taskInProgress : tasks) {
|
||||
Future<TaskStatus> futureResult = taskInProgress
|
||||
.getFuture();
|
||||
if (futureResult.isDone()) {
|
||||
TaskStatus result = null;
|
||||
try {
|
||||
result = futureResult
|
||||
.get(Constants.SERVICE_CLIENT_TIMEOUT_DEFAULT_MINUTES,
|
||||
.get(Constants.SERVICE_CLIENT_TIMEOUT_DEFAULT_MILLIS,
|
||||
TimeUnit.MINUTES);
|
||||
logger.info("AccountingClientTask: " + result);
|
||||
|
||||
|
@ -83,14 +88,23 @@ public class AccountingClientDaemon implements Runnable {
|
|||
+ e.getLocalizedMessage(), e);
|
||||
} catch (TimeoutException e) {
|
||||
logger.error("AccountingClientTask No response after "
|
||||
+ Constants.SERVICE_CLIENT_TIMEOUT_DEFAULT_MINUTES
|
||||
+ Constants.SERVICE_CLIENT_TIMEOUT_DEFAULT_MILLIS
|
||||
+ " minutes!");
|
||||
futureResult.cancel(true);
|
||||
}
|
||||
dones.add(futureResult);
|
||||
dones.add(taskInProgress);
|
||||
} else {
|
||||
GregorianCalendar now = new GregorianCalendar();
|
||||
long diff = now.getTimeInMillis()
|
||||
- taskInProgress.getStartTime()
|
||||
.getTimeInMillis();
|
||||
if (diff > Constants.SERVICE_CLIENT_TIMEOUT_DEFAULT_MILLIS) {
|
||||
futureResult.cancel(true);
|
||||
dones.add(taskInProgress);
|
||||
}
|
||||
}
|
||||
}
|
||||
workers.removeAll(dones);
|
||||
tasks.removeAll(dones);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
package org.gcube.portlets.admin.accountingmanager.server.util;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Calendar;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Giancarlo Panichi email: <a
|
||||
* href="mailto:g.panichi@isti.cnr.it">g.panichi@isti.cnr.it</a>
|
||||
*
|
||||
*/
|
||||
public class TaskInProgress implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = -1957012551318695316L;
|
||||
private Calendar startTime;
|
||||
private Future<TaskStatus> future;
|
||||
|
||||
public TaskInProgress(Calendar startTime, Future<TaskStatus> future) {
|
||||
super();
|
||||
this.startTime = startTime;
|
||||
this.future = future;
|
||||
}
|
||||
|
||||
public Calendar getStartTime() {
|
||||
return startTime;
|
||||
}
|
||||
|
||||
public void setStartTime(Calendar startTime) {
|
||||
this.startTime = startTime;
|
||||
}
|
||||
|
||||
public Future<TaskStatus> getFuture() {
|
||||
return future;
|
||||
}
|
||||
|
||||
public void setFuture(Future<TaskStatus> future) {
|
||||
this.future = future;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "TaskInProgress [startTime=" + startTime + ", future=" + future
|
||||
+ "]";
|
||||
}
|
||||
|
||||
}
|
|
@ -41,7 +41,7 @@ public class Constants {
|
|||
public static final String ACCOUNTING_CATEGORY = "AccountingProfile";
|
||||
|
||||
//TimeOut
|
||||
public static final long SERVICE_CLIENT_TIMEOUT_DEFAULT_MINUTES = 30;
|
||||
public static final long SERVICE_CLIENT_TIMEOUT_DEFAULT_MILLIS = 1800000;
|
||||
public static final int CLIENT_MONITOR_PERIODMILLIS = 2000;
|
||||
public static final int CLIENT_MONITOR_TIME_OUT_PERIODMILLIS = 1800000;
|
||||
|
||||
|
|
Loading…
Reference in New Issue