71 lines
1.6 KiB
Java
71 lines
1.6 KiB
Java
package org.gcube.data.transfer.library;
|
|
|
|
import java.util.Map.Entry;
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
import java.util.concurrent.Executors;
|
|
import java.util.concurrent.ScheduledExecutorService;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import org.gcube.data.transfer.model.TransferCapabilities;
|
|
|
|
@Slf4j
|
|
public class CapabilitiesManager {
|
|
|
|
private static ConcurrentHashMap<String, TTLContainer> theMap=new ConcurrentHashMap<>();
|
|
private static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
|
|
|
|
static {
|
|
scheduler.scheduleAtFixedRate(new Runnable() {
|
|
|
|
private static final long TTL =30*60*1000;
|
|
|
|
|
|
@Override
|
|
public void run() {
|
|
log.debug("Running Ticket cleaner, TTL is "+TTL);
|
|
int removed=0;
|
|
for(Entry<String,TTLContainer> entry:theMap.entrySet())
|
|
if(System.currentTimeMillis()-entry.getValue().getLastUsageTime()>TTL){
|
|
theMap.remove(entry.getKey());
|
|
removed++;
|
|
}
|
|
log.debug("Removed "+removed+" old tickets");
|
|
|
|
}
|
|
}, 30, 30, TimeUnit.MINUTES);
|
|
|
|
}
|
|
|
|
|
|
public class TTLContainer {
|
|
|
|
private long lastUsageTime=System.currentTimeMillis();
|
|
private TransferCapabilities theObject;
|
|
public TTLContainer(TransferCapabilities theObject) {
|
|
this.theObject = theObject;
|
|
}
|
|
|
|
|
|
private void update(){
|
|
lastUsageTime=System.currentTimeMillis();
|
|
}
|
|
|
|
public TransferCapabilities getTicket(){
|
|
update();
|
|
return theObject;
|
|
}
|
|
|
|
public long getLastUsageTime() {
|
|
return lastUsageTime;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
public static TransferCapabilities get(String hostname){
|
|
|
|
}
|
|
}
|