Removing sleep() from the sweeper (use an executor, instead)

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/information-system/gCubeIS/Collector@40763 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Manuele Simi 2011-06-23 14:09:51 +00:00
parent 5e0baa7201
commit 30d08687ea
1 changed files with 28 additions and 33 deletions

View File

@ -18,11 +18,11 @@ import org.gcube.informationsystem.collector.impl.resources.GCUBEInstanceStateRe
import org.gcube.informationsystem.collector.impl.resources.GCUBEXMLResource; import org.gcube.informationsystem.collector.impl.resources.GCUBEXMLResource;
/** /**
* This class provides some cleanup procedures to use to maintain a consistent * This class provides some cleanup procedures to use to maintain a consistent content in the XML
* content in the XML storage. One of them is a thread, activated at RI startup * storage. One of them is a thread, activated at RI startup time, that periodically drops the out
* time, that periodically drops the out to date resources from the storage * to date resources from the storage
* *
* @author Manuele Simi (ISTI-CNR) * @author Manuele Simi (ISTI-CNR)
* *
*/ */
public class Sweeper implements Runnable { public class Sweeper implements Runnable {
@ -33,38 +33,34 @@ public class Sweeper implements Runnable {
private static GCUBELog logger = new GCUBELog(Sweeper.class); private static GCUBELog logger = new GCUBELog(Sweeper.class);
//private static XMLStorage storage = null; // private static XMLStorage storage = null;
/** /**
* Initializes a new Sweeper * Initializes a new Sweeper
* *
* @param delay the sweeper delay * @param delay
* @param resourceExpirationTime the time after that a resource is classified as exipired * the sweeper delay
* @throws Exception if the eXist connection fails * @param resourceExpirationTime
* the time after that a resource is classified as exipired
* @throws Exception
* if the eXist connection fails
*/ */
public Sweeper(long delay, long resourceExpirationTime) throws Exception { public Sweeper(long delay, long resourceExpirationTime) throws Exception {
Sweeper.DELAY = delay; Sweeper.DELAY = delay;
Sweeper.resourceExpirationTime = resourceExpirationTime; Sweeper.resourceExpirationTime = resourceExpirationTime;
logger.info("Starting sweeper thread with an interval of " + Sweeper.DELAY + " ms"); logger.info("Starting sweeper thread with an interval of " + Sweeper.DELAY + " ms");
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public void run() { public void run() {
if (Sweeper.DELAY == -1) return; if (Sweeper.DELAY == -1)
try { return;
while (!Thread.interrupted()) { logger.info("Starting IC sweeper...");
Thread.sleep(Sweeper.DELAY); this.cleanDeletedResourcesList();
logger.info("Starting IC sweeper..."); this.cleanExpiredResources();
this.cleanDeletedResourcesList();
this.cleanExpiredResources();
}
} catch (InterruptedException ie) {
logger.error("Unable to sleep (yawn)", ie);
// thread was interrupted
}
} }
/** /**
@ -77,14 +73,14 @@ public class Sweeper implements Runnable {
now.setTimeZone(TimeZone.getTimeZone("GMT")); now.setTimeZone(TimeZone.getTimeZone("GMT"));
try { try {
GCUBEInstanceStateResource fakeresource = new GCUBEInstanceStateResource(); GCUBEInstanceStateResource fakeresource = new GCUBEInstanceStateResource();
//get all IDs for Instance States' collection // get all IDs for Instance States' collection
String[] ids = State.getDataManager().listAllCollectionResourceIDs(fakeresource.getCollectionName()); String[] ids = State.getDataManager().listAllCollectionResourceIDs(fakeresource.getCollectionName());
for (String id : ids) { for (String id : ids) {
try { try {
GCUBEInstanceStateResource tempresource = new GCUBEInstanceStateResource(); GCUBEInstanceStateResource tempresource = new GCUBEInstanceStateResource();
tempresource.setResourceName(id); tempresource.setResourceName(id);
GCUBEXMLResource xmlresource = new GCUBEXMLResource(tempresource); GCUBEXMLResource xmlresource = new GCUBEXMLResource(tempresource);
State.getDataManager().retrieveResourceContent(xmlresource); State.getDataManager().retrieveResourceContent(xmlresource);
logger.trace("Checking resource " + id); logger.trace("Checking resource " + id);
if (ResourceFilter.isExpired(xmlresource)) { if (ResourceFilter.isExpired(xmlresource)) {
// removes the resources from the database // removes the resources from the database
@ -93,29 +89,28 @@ public class Sweeper implements Runnable {
} }
// break; // break;
} catch (Exception e) { } catch (Exception e) {
logger.debug("IC sweeper - the resource " + id + " is no longer available in the storage"); logger.debug("IC sweeper - the resource " + id + " is no longer available in the storage");
} }
} }
} catch (Exception e2) { } catch (Exception e2) {
logger.warn("IC sweeper - an exception was rised when trying to cleanup the storage ", e2); logger.warn("IC sweeper - an exception was rised when trying to cleanup the storage ", e2);
} }
} }
/** /**
* Deletes the Properties collection from the storage * Deletes the Properties collection from the storage
* @throws XMLStorageNotAvailableException *
* @throws XMLStorageNotAvailableException
* *
*/ */
public static void cleanRPs() throws XMLStorageNotAvailableException { public static void cleanRPs() throws XMLStorageNotAvailableException {
// cleanup the RPs collection // cleanup the RPs collection
State.getDataManager().deleteCollection(new GCUBEInstanceStateResource().getCollectionName()); State.getDataManager().deleteCollection(new GCUBEInstanceStateResource().getCollectionName());
} }
/** /**
* Deletes the expired resources from the list of deleted resources as * Deletes the expired resources from the list of deleted resources as notified by the AF
* notified by the AF
* *
*/ */
@ -123,7 +118,7 @@ public class Sweeper implements Runnable {
Calendar now = new GregorianCalendar(); Calendar now = new GregorianCalendar();
now.setTimeZone(TimeZone.getTimeZone("GMT")); now.setTimeZone(TimeZone.getTimeZone("GMT"));
List<GCUBEXMLResource> toRemove = new ArrayList<GCUBEXMLResource>(); List<GCUBEXMLResource> toRemove = new ArrayList<GCUBEXMLResource>();
for (GCUBEXMLResource res : State.deletedResources) { for (GCUBEXMLResource res : State.deletedResources) {
try { try {
if (now.getTimeInMillis() - res.getLastUpdateTimeinMills() > Sweeper.resourceExpirationTime) { if (now.getTimeInMillis() - res.getLastUpdateTimeinMills() > Sweeper.resourceExpirationTime) {
@ -145,7 +140,7 @@ public class Sweeper implements Runnable {
* @return the sweeper interval * @return the sweeper interval
*/ */
public long getIntervalInMS() { public long getIntervalInMS() {
return DELAY; return DELAY;
} }
} }