public void registerForLaterWriting ( @ Nonnull final AbstractWALDAO < ? > aDAO , @ Nonnull final String sWALFilename , @ Nonnull final TimeValue aWaitingWime ) { // In case many DAOs of the same class exist, the filename is also added final String sKey = aDAO . getClass ( ) . getName ( ) + "::" + sWALFilename ; // Check if the passed DAO is already scheduled for writing final boolean bDoScheduleForWriting = m_aRWLock . writeLocked ( ( ) -> m_aWaitingDAOs . add ( sKey ) ) ; if ( bDoScheduleForWriting ) { // We need to schedule it now if ( LOGGER . isDebugEnabled ( ) ) LOGGER . debug ( "Now scheduling writing for DAO " + sKey ) ; // What should be executed upon writing final Runnable r = ( ) -> { // Use DAO lock! aDAO . internalWriteLocked ( ( ) -> { // Main DAO writing aDAO . _writeToFileAndResetPendingChanges ( "ScheduledWriter.run" ) ; // Delete the WAL file aDAO . _deleteWALFileAfterProcessing ( sWALFilename ) ; if ( LOGGER . isDebugEnabled ( ) ) LOGGER . debug ( "Finished scheduled writing for DAO " + sKey ) ; } ) ; // Remove from the internal set so that another job will be // scheduled for the same DAO // Do this after the writing to the file m_aRWLock . writeLocked ( ( ) -> { // Remove from the overall set as well as from the scheduled items m_aWaitingDAOs . remove ( sKey ) ; m_aScheduledItems . remove ( sKey ) ; } ) ; } ; // Schedule exactly once in the specified waiting time final ScheduledFuture < ? > aFuture = m_aES . schedule ( r , aWaitingWime . getDuration ( ) , aWaitingWime . getTimeUnit ( ) ) ; // Remember the scheduled item and the runnable so that the task can // be rescheduled upon shutdown. m_aRWLock . writeLocked ( ( ) -> m_aScheduledItems . put ( sKey , new WALItem ( aFuture , r ) ) ) ; } // else the writing of the passed DAO is already scheduled and no further // action is necessary }
This is the main method for registration of later writing .