This commit is contained in:
Lucio Lelii 2009-01-14 18:40:54 +00:00
parent 46f38e74c9
commit f514f06059
2 changed files with 30 additions and 41 deletions

View File

@ -57,8 +57,8 @@ import static org.gcube.informationsystem.registry.impl.core.RegistryConfigurati
*/
public class RegistryFactory{
private final long delayRemoveTyme= 120000;
/**
* The UUIDGen
@ -173,32 +173,28 @@ public class RegistryFactory{
//try to create resource
try {
logger.debug(resource.getID()+" Creating the stateful resource for " + resource.getID());
//finding the timestamp
ISClient client = GHNContext.getImplementation(ISClient.class);
GCUBEGenericQuery query = client.getQuery(GCUBEGenericQuery.class);
query.setExpression("declare namespace is = 'http://gcube-system.org/namespaces/informationsystem/registry';" +
"for $result in collection(\"/db/Profiles\")//Document where ($result/Data/is:Profile/Resource/ID/string() eq '"+resource.getID()+"') return $result/LastUpdateMs");
List<XMLResult> list= client.execute(query, ServiceContext.getContext().getScope());
long timestamp=0;
if (list.size()>0)
timestamp=Long.parseLong(list.get(0).evaluate("/LastUpdateMs/text()").get(0));
ProfileResource presource = (ProfileResource) ProfileContext.getContext().getWSHome().create(ProfileContext.getContext().makeKey(resource.getID()),resource);
presource.getPersistenceDelegate().store(presource);
//Deleting the WSResource created
long timestamp;
if (resource.getType().compareTo(GCUBEHostingNode.TYPE)!=0){
GCUBEScope tmpScope= ServiceContext.getContext().getScope().getType() == GCUBEScope.Type.VRE ? ServiceContext.getContext().getScope().getEnclosingScope() : ServiceContext.getContext().getScope();
timestamp=System.currentTimeMillis()+delayRemoveTyme;
Couple c= new Couple(timestamp, presource);
if(!ServiceContext.threadTable.get(tmpScope.getName()).getStack().contains(c)){
ServiceContext.threadTable.get(tmpScope.getName()).getStack().add(c);
logger.trace("Adding resource to EliminatePoolingThread "+presource.getGCubeResource().getID());
}else{
int index= ServiceContext.threadTable.get(tmpScope.getName()).getStack().indexOf(c);
ServiceContext.threadTable.get(tmpScope.getName()).getStack().get(index).timestamp=timestamp;
ServiceContext.threadTable.get(tmpScope.getName()).getStack().get(index).timestamp=timestamp+delayRemoveTyme;
}
}
}
catch (Exception ex) {
String msg = "Error creating Resource";
String msg = "Error creating Resource "+resource.getID();
logger.error(msg,ex);
throw new RemoteException(msg);
}
@ -264,28 +260,21 @@ public class RegistryFactory{
try {
resource = ResourceType.valueOf(mess.getType()).getResourceClass();
resource.load(new StringReader(xmlProfile));
//finding the timestamp
ISClient client = GHNContext.getImplementation(ISClient.class);
GCUBEGenericQuery query = client.getQuery(GCUBEGenericQuery.class);
query.setExpression("declare namespace is = 'http://gcube-system.org/namespaces/informationsystem/registry';" +
"for $result in collection(\"/db/Profiles\")//Document where ($result/Data/is:Profile/Resource/ID/string() eq '"+resource.getID()+"') return $result/LastUpdateMs");
List<XMLResult> list= client.execute(query, ServiceContext.getContext().getScope());
long timestamp=0;
if (list.size()>0)
timestamp=Long.parseLong(list.get(0).evaluate("/LastUpdateMs/text()").get(0));
ProfileResource pr= getProfileResource(ID);
pr.updateResource(resource);
//Deleting the WSResource created
long timestamp;
if (resource.getType().compareTo(GCUBEHostingNode.TYPE)!=0){
GCUBEScope tmpScope= ServiceContext.getContext().getScope().getType() == GCUBEScope.Type.VRE ? ServiceContext.getContext().getScope().getEnclosingScope() : ServiceContext.getContext().getScope();
timestamp=System.currentTimeMillis()+delayRemoveTyme;
Couple c= new Couple(timestamp, pr);
if(!ServiceContext.threadTable.get(tmpScope.getName()).getStack().contains(c)){
ServiceContext.threadTable.get(tmpScope.getName()).getStack().add(c);
logger.trace("Adding resource to EliminatePoolingThread "+pr.getGCubeResource().getID());
}else{
int index= ServiceContext.threadTable.get(tmpScope.getName()).getStack().indexOf(c);
ServiceContext.threadTable.get(tmpScope.getName()).getStack().get(index).timestamp=timestamp;
ServiceContext.threadTable.get(tmpScope.getName()).getStack().get(index).timestamp=timestamp+delayRemoveTyme;
}
}
}

View File

@ -7,47 +7,47 @@ import java.util.List;
import org.gcube.common.core.contexts.GHNContext;
import org.gcube.common.core.informationsystem.client.ISClient;
import org.gcube.common.core.informationsystem.client.XMLResult;
import org.gcube.common.core.informationsystem.client.queries.GCUBEGenericQuery;
import org.gcube.common.core.utils.logging.GCUBELog;
import org.gcube.informationsystem.registry.impl.RegistryFactory;
import org.gcube.informationsystem.registry.impl.contexts.ProfileContext;
import org.gcube.informationsystem.registry.impl.contexts.ServiceContext;
import org.gcube.informationsystem.registry.impl.state.ProfileResource;
public class EliminatePoolingThread extends Thread {
private final long sleepTime= 60000;
private List<Couple> stack = Collections.synchronizedList(new LinkedList<Couple>());
private static GCUBELog logger = new GCUBELog(RegistryFactory.class.getName());
@SuppressWarnings("static-access")
public void run(){
boolean noErrors= true;
while(noErrors){
try {
this.sleep(120000);
this.sleep(sleepTime);
} catch (InterruptedException e) {}
System.out.println("Thread svegliato");
LinkedList<Couple> tmpStack = new LinkedList<Couple>();
ISClient client;
GCUBEGenericQuery query;
try {
client = GHNContext.getImplementation(ISClient.class);
query = client.getQuery(GCUBEGenericQuery.class);
long timestamp= System.currentTimeMillis();
synchronized (stack) {
int numRes= stack.size();
while (stack.size()>0){
Couple c=stack.remove(stack.size()-1);
query.setExpression("declare namespace is = 'http://gcube-system.org/namespaces/informationsystem/registry';" +
"for $result in collection(\"/db/Profiles\")//Document where ($result/Data/is:Profile/Resource/ID/string() eq '"+c.resource.getGCubeResource().getID()+"') return $result/LastUpdateMs");
List<XMLResult> list= client.execute(query, ServiceContext.getContext().getScope());
if ( (list.size()>0) && (Long.parseLong(list.get(0).evaluate("/LastUpdateMs/text()").get(0))>c.timestamp)) try {
ProfileContext.getContext().getWSHome().remove(c.resource.getID());
System.out.println("trying to delete "+c.resource.getID() );
System.out.println("timenstamp now: "+timestamp+" resource timestamp: "+c.timestamp);
if ( (timestamp>=c.timestamp)) try {
System.out.println("the resource will be deleted "+c.resource.getID());
ProfileContext.getContext().getWSHome().remove(c.resource.getID());
}catch(Exception e){e.printStackTrace();tmpStack.offer(c);}
else tmpStack.offer(c);
}