From 5e0baa72016ff6412bcf2750fbba64d6cd6b8a09 Mon Sep 17 00:00:00 2001 From: Manuele Simi Date: Thu, 23 Jun 2011 14:08:23 +0000 Subject: [PATCH] Changing service's initialization procedure, more control on activated threads git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/information-system/gCubeIS/Collector@40762 82a268e6-3cf1-43bd-a215-b396298e98cf --- .classpath | 2 +- .../impl/porttypes/XMLStorageAccess.java | 1 - .../impl/xmlstorage/backup/Scheduler.java | 13 +++--- .../impl/xmlstorage/exist/State.java | 44 +++++++++---------- .../impl/xmlstorage/exist/sweep/Sweeper.java | 18 +++----- .../stubs/testsuite/ConnectTester.java | 4 +- 6 files changed, 39 insertions(+), 43 deletions(-) diff --git a/.classpath b/.classpath index 8649e18..f36ed34 100644 --- a/.classpath +++ b/.classpath @@ -8,6 +8,6 @@ - + diff --git a/src/org/gcube/informationsystem/collector/impl/porttypes/XMLStorageAccess.java b/src/org/gcube/informationsystem/collector/impl/porttypes/XMLStorageAccess.java index 062953e..fc4e923 100644 --- a/src/org/gcube/informationsystem/collector/impl/porttypes/XMLStorageAccess.java +++ b/src/org/gcube/informationsystem/collector/impl/porttypes/XMLStorageAccess.java @@ -123,7 +123,6 @@ public class XMLStorageAccess extends GCUBEPortType { logger.info("Connect operation invoked"); try { State.initialize(); - //ICServiceContext.getContext().setStatus(Status.READIED); } catch (Exception e) { logger.error("Initialisation failed", e); XMLStorageNotAvailableFaultType fault = new XMLStorageNotAvailableFaultType(); diff --git a/src/org/gcube/informationsystem/collector/impl/xmlstorage/backup/Scheduler.java b/src/org/gcube/informationsystem/collector/impl/xmlstorage/backup/Scheduler.java index e4159e3..b7b38d0 100644 --- a/src/org/gcube/informationsystem/collector/impl/xmlstorage/backup/Scheduler.java +++ b/src/org/gcube/informationsystem/collector/impl/xmlstorage/backup/Scheduler.java @@ -29,7 +29,6 @@ public abstract class Scheduler implements Runnable { public void run() { - do { try { Thread.sleep(this.intervalInMS); this.doBackup(); @@ -39,10 +38,7 @@ public abstract class Scheduler implements Runnable { logger.error("Unable to backup", e); } - } while (! Thread.interrupted()); - - //logger.info("Backup Scheduler was interrupted"); - } + } /** * Performs the backup @@ -50,5 +46,12 @@ public abstract class Scheduler implements Runnable { * @throws Exception if the backup fails */ protected abstract void doBackup() throws Exception; + + /** + * @return the intervalInMS + */ + public int getIntervalInMS() { + return intervalInMS; + } } \ No newline at end of file diff --git a/src/org/gcube/informationsystem/collector/impl/xmlstorage/exist/State.java b/src/org/gcube/informationsystem/collector/impl/xmlstorage/exist/State.java index eea1573..77d1132 100755 --- a/src/org/gcube/informationsystem/collector/impl/xmlstorage/exist/State.java +++ b/src/org/gcube/informationsystem/collector/impl/xmlstorage/exist/State.java @@ -12,6 +12,8 @@ import java.util.ArrayList; import java.util.Enumeration; import java.util.List; import java.util.Collections; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; /** * The global state of an IC instance @@ -31,11 +33,6 @@ public class State { */ private static QueryManager queryManager; - /** Thread that periodically sweeps the XMLStorage from expired resources */ - public static Thread sweeperT = null; - - /** Thread that periodically backups the XMLStorage */ - public static Thread schedulerT = null; /** * List of recently deleted resources. It is used to avoid the storage of RPs of a deleted @@ -44,6 +41,8 @@ public class State { public static List deletedResources = Collections.synchronizedList(new ArrayList()); private static GCUBELog logger = new GCUBELog(State.class); + + private static boolean isInitialized = false; /** * Initializes the eXist DB connections using during the service life @@ -55,6 +54,10 @@ public class State { */ public static void initialize() throws Exception { logger.info("Starting IC service initialization..."); + if (isInitialized) { + logger.info("Service's state is already initialized"); + return; + } long maxOperations = Long.valueOf((String) ICServiceContext.getContext().getProperty("maxOperationsPerConnection", true)); State.initializeDataManager(maxOperations); State.initializeQueryManager(maxOperations); @@ -72,25 +75,16 @@ public class State { logger.info("Initialising the sweeper..."); // start the sweeper to periodically cleanup the storage and some data structures - if (State.sweeperT == null) { - Sweeper sweeper = new Sweeper(Long.valueOf((String) ICServiceContext.getContext().getProperty("sweeperIntervalInMillis", true)), - Long.valueOf((String) ICServiceContext.getContext().getProperty("resourceExpirationTimeInMillis", true))); - State.sweeperT = new Thread(sweeper); - State.sweeperT.setName("ICSweeper"); - State.sweeperT.start(); - } - - + Sweeper sweeper = new Sweeper(Long.valueOf((String) ICServiceContext.getContext().getProperty("sweeperIntervalInMillis", true)), + Long.valueOf((String) ICServiceContext.getContext().getProperty("resourceExpirationTimeInMillis", true))); + Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(sweeper, sweeper.getIntervalInMS(), sweeper.getIntervalInMS(), TimeUnit.MILLISECONDS); + //start the scheduler for automatic backups (if any) logger.info("Initialising the scheduled backups..."); org.gcube.informationsystem.collector.impl.xmlstorage.backup.Scheduler scheduler = DataManager.getScheduler(); - if (scheduler != null) { - if (State.schedulerT == null) - State.schedulerT = new Thread(scheduler); - State.schedulerT.setName("BackupScheduler"); - State.schedulerT.start(); - } + Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(scheduler, scheduler.getIntervalInMS(), scheduler.getIntervalInMS(), TimeUnit.MILLISECONDS); logger.info("IC service initialization completed"); + isInitialized = true; } private static void initializeDataManager(long maxOperations) throws Exception { @@ -118,17 +112,21 @@ public class State { */ public static void dispose() throws Exception { logger.info("Disposing IC service's resources..."); - if (State.sweeperT != null) { + if (!isInitialized) { + logger.info("Service's state is already disposed"); + return; + } + /*if (State.sweeperT != null) { State.sweeperT.interrupt(); State.sweeperT = null; } if (State.schedulerT == null) { State.schedulerT.interrupt(); State.schedulerT = null; - } + }*/ State.dataManager.shutdown(true); State.queryManager.shutdown(true); - + isInitialized = false; } /** diff --git a/src/org/gcube/informationsystem/collector/impl/xmlstorage/exist/sweep/Sweeper.java b/src/org/gcube/informationsystem/collector/impl/xmlstorage/exist/sweep/Sweeper.java index 3ab22ee..89ceb54 100755 --- a/src/org/gcube/informationsystem/collector/impl/xmlstorage/exist/sweep/Sweeper.java +++ b/src/org/gcube/informationsystem/collector/impl/xmlstorage/exist/sweep/Sweeper.java @@ -102,17 +102,6 @@ public class Sweeper implements Runnable { } } - /** - * Removes all the properties documents related to the given Running - * Instance ID - * - * @param id - * the ID of the Running Instance whose properties documents have - * to be removed - */ - public static void cleanResourceForRI(String id) { - // TO DO - } /** * Deletes the Properties collection from the storage @@ -152,4 +141,11 @@ public class Sweeper implements Runnable { } + /** + * @return the sweeper interval + */ + public long getIntervalInMS() { + return DELAY; + } + } diff --git a/src/org/gcube/informationsystem/collector/stubs/testsuite/ConnectTester.java b/src/org/gcube/informationsystem/collector/stubs/testsuite/ConnectTester.java index 08ba292..790d996 100644 --- a/src/org/gcube/informationsystem/collector/stubs/testsuite/ConnectTester.java +++ b/src/org/gcube/informationsystem/collector/stubs/testsuite/ConnectTester.java @@ -48,10 +48,10 @@ public class ConnectTester { port = new XMLStorageAccessServiceLocator().getXMLStorageAccessPortTypePort(new URL(portTypeURI)); port = GCUBERemotePortTypeContext.getProxy(port, GCUBEScope.getScope(args[2])); } catch (Exception e) { - logger.error("",e); + logger.error("Unable to create the IC portType",e); } - logger.info("Submitting shutdown request to " + portTypeURI+ "..."); + logger.info("Submitting connect request to " + portTypeURI+ "..."); try { port.connect(new VOID());