org.rrd4j.core

## Class RrdNioBackendFactory

- java.lang.Object
- - org.rrd4j.core.RrdBackendFactory
  - - org.rrd4j.core.RrdFileBackendFactory
    - - org.rrd4j.core.RrdNioBackendFactory

- All Implemented Interfaces:
    Closeable, AutoCloseable

      @RrdBackendAnnotation(name="NIO",
                            shouldValidateHeader=true)
      public class RrdNioBackendFactory
      extends RrdFileBackendFactory

  Factory class which creates actual `RrdNioBackend` objects. This is the default factory since 1.4.0 version.

  ## Managing the thread pool

  Each RrdNioBackendFactory is optionally backed by a `RrdSyncThreadPool`, which it uses to sync the memory-mapped files to disk. In order to avoid having these threads live longer than they should, it is recommended that clients create and destroy thread pools at the appropriate time in their application's life time. Failure to manage thread pools appropriately may lead to the thread pool hanging around longer than necessary, which in turn may cause memory leaks.

  if sync period is negative, no sync thread will be launched

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static int` | `DEFAULT_SYNC_CORE_POOL_SIZE` The core pool size for the sync executor.
    `static int` | `DEFAULT_SYNC_PERIOD` Period in seconds between consecutive synchronizations when sync-mode is set to SYNC_BACKGROUND.

    - ### Fields inherited from class org.rrd4j.core.RrdBackendFactory

      `cachingAllowed, DEFAULTFACTORY, name, scheme, validateHeader`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `RrdNioBackendFactory()` Creates a new RrdNioBackendFactory with default settings.
    `RrdNioBackendFactory(int syncPeriod)` Creates a new RrdNioBackendFactory, using a default `RrdSyncThreadPool`.
    `RrdNioBackendFactory(int syncPeriod, int syncPoolSize)` Creates a new RrdNioBackendFactory.
    `RrdNioBackendFactory(int syncPeriod, RrdSyncThreadPool syncThreadPool)` Creates a new RrdNioBackendFactory.
    `RrdNioBackendFactory(int syncPeriod, ScheduledExecutorService syncThreadPool)` Creates a new RrdNioBackendFactory.

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `close()` A generic close handle, default implementation does nothing.
    `static int` | `getSyncPeriod()` Returns time between two consecutive background synchronizations.
    `static int` | `getSyncPoolSize()` Returns the number of synchronizing threads.
    `RrdSyncThreadPool` | `getSyncThreadPool()`
    `protected RrdBackend` | `open(String path, boolean readOnly)`
    `static void` | `setSyncPeriod(int syncPeriod)` Sets time between consecutive background synchronizations.
    `static void` | `setSyncPoolSize(int syncPoolSize)` Sets the number of synchronizing threads.

    - ### Methods inherited from class org.rrd4j.core.RrdFileBackendFactory

      `canStore, exists, getCanonicalUri, getPath, getUri`

    - ### Methods inherited from class org.rrd4j.core.RrdBackendFactory

      `addActiveFactories, buildGenericUri, checkClosing, exists, findFactory, getActiveFactories, getBackend, getBackend, getDefaultFactory, getName, getRootUri, getScheme, resolve, setActiveFactories, shouldValidateHeader, shouldValidateHeader`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### DEFAULT_SYNC_CORE_POOL_SIZE

          public static final int DEFAULT_SYNC_CORE_POOL_SIZE

      The core pool size for the sync executor. Defaults to 6.
      See Also:
        Constant Field Values

    - #### DEFAULT_SYNC_PERIOD

          public static final int DEFAULT_SYNC_PERIOD

      Period in seconds between consecutive synchronizations when sync-mode is set to SYNC_BACKGROUND. By default in-memory cache will be transferred to the disc every 300 seconds (5 minutes). Default value can be changed via `setSyncPeriod(int)` method.
      See Also:
        Constant Field Values

  - ### Constructor Detail

    - #### RrdNioBackendFactory

          public RrdNioBackendFactory()

      Creates a new RrdNioBackendFactory with default settings.

    - #### RrdNioBackendFactory

          public RrdNioBackendFactory(int syncPeriod)

      Creates a new RrdNioBackendFactory, using a default `RrdSyncThreadPool`.
      Parameters:
        `syncPeriod` - the sync period, in seconds. If negative or 0, sync threads are disabled.

    - #### RrdNioBackendFactory

          public RrdNioBackendFactory(int syncPeriod,
                                      int syncPoolSize)

      Creates a new RrdNioBackendFactory.
      Parameters:
        `syncPeriod` - the sync period, in seconds.
        `syncPoolSize` - The number of threads to use to sync the mapped file to disk, if negative or 0, sync threads are disabled.

    - #### RrdNioBackendFactory

          public RrdNioBackendFactory(int syncPeriod,
                                      RrdSyncThreadPool syncThreadPool)

      Creates a new RrdNioBackendFactory.
      Parameters:
        `syncPeriod` - the sync period, in seconds.
        `syncThreadPool` - If null, disable background sync threads

    - #### RrdNioBackendFactory

          public RrdNioBackendFactory(int syncPeriod,
                                      ScheduledExecutorService syncThreadPool)

      Creates a new RrdNioBackendFactory.
      Parameters:
        `syncPeriod` - the sync period, in seconds.
        `syncThreadPool` - If null, disable background sync threads

  - ### Method Detail

    - #### close

          public void close()

      Description copied from class: `RrdBackendFactory`
      A generic close handle, default implementation does nothing.
      Specified by:
        `close` in interface `Closeable`
      Specified by:
        `close` in interface `AutoCloseable`
      Overrides:
        `close` in class `RrdBackendFactory`

    - #### getSyncPeriod

          public static int getSyncPeriod()

      Returns time between two consecutive background synchronizations. If not changed via `setSyncPeriod(int)` method call, defaults to `DEFAULT_SYNC_PERIOD`. See `setSyncPeriod(int)` for more information.
      Returns:
        Time in seconds between consecutive background synchronizations.

    - #### getSyncPoolSize

          public static int getSyncPoolSize()

      Returns the number of synchronizing threads. If not changed via `setSyncPoolSize(int)` method call, defaults to `DEFAULT_SYNC_CORE_POOL_SIZE`. See `setSyncPoolSize(int)` for more information.
      Returns:
        Number of synchronizing threads.

    - #### getSyncThreadPool

          public RrdSyncThreadPool getSyncThreadPool()

      Returns:
        The `RrdSyncThreadPool` or null if syncing is disabled

    - #### open

          protected RrdBackend open(String path,
                                    boolean readOnly)
                             throws IOException

      Creates RrdNioBackend object for the given file path.

      Specified by:
        `open` in class `RrdBackendFactory`
      Throws:
        `IOException`

    - #### setSyncPeriod

          public static void setSyncPeriod(int syncPeriod)

      Sets time between consecutive background synchronizations. If negative, it will disabling syncing for all NIO backend factory.
      Parameters:
        `syncPeriod` - Time in seconds between consecutive background synchronizations.

    - #### setSyncPoolSize

          public static void setSyncPoolSize(int syncPoolSize)

      Sets the number of synchronizing threads. It must be set before the first use of this factory. It will not have any effect afterward.
      Parameters:
        `syncPoolSize` - Number of synchronizing threads.