private void syncFromStorage ( ) { giant . lock ( ) ; try { if ( active ) { final Map < String , Task > newTasks = toTaskIDMap ( taskStorage . getActiveTasks ( ) ) ; final int tasksSynced = newTasks . size ( ) ; final Map < String , Task > oldTasks = toTaskIDMap ( tasks ) ; // Calculate differences on IDs instead of Task Objects. Set < String > commonIds = Sets . newHashSet ( Sets . intersection ( newTasks . keySet ( ) , oldTasks . keySet ( ) ) ) ; for ( String taskID : commonIds ) { newTasks . remove ( taskID ) ; oldTasks . remove ( taskID ) ; } Collection < Task > addedTasks = newTasks . values ( ) ; Collection < Task > removedTasks = oldTasks . values ( ) ; // Clean up removed Tasks for ( Task task : removedTasks ) { removeTaskInternal ( task ) ; } // Add newly Added tasks to the queue for ( Task task : addedTasks ) { addTaskInternal ( task ) ; } log . info ( "Synced %d tasks from storage (%d tasks added, %d tasks removed)." , tasksSynced , addedTasks . size ( ) , removedTasks . size ( ) ) ; managementMayBeNecessary . signalAll ( ) ; } else { log . info ( "Not active. Skipping storage sync." ) ; } } catch ( Exception e ) { log . warn ( e , "Failed to sync tasks from storage!" ) ; throw new RuntimeException ( e ) ; } finally { giant . unlock ( ) ; } }
Resync the contents of this task queue with our storage facility . Useful to make sure our in - memory state corresponds to the storage facility even if the latter is manually modified .