Class AbstractDatabaseManager

  • All Implemented Interfaces:
    java.io.Flushable, java.lang.AutoCloseable
    Direct Known Subclasses:
    JdbcDatabaseManager, NoSqlDatabaseManager

    public abstract class AbstractDatabaseManager
    extends AbstractManager
    implements java.io.Flushable
    Manager that allows database appenders to have their configuration reloaded without losing events.
    • Constructor Detail

      • AbstractDatabaseManager

        protected AbstractDatabaseManager​(java.lang.String name,
                                          int bufferSize)
        Instantiates the base manager.
        Parameters:
        name - The manager name, which should include any configuration details that one might want to be able to reconfigure at runtime, such as database name, username, (hashed) password, etc.
        bufferSize - The size of the log event buffer.
      • AbstractDatabaseManager

        protected AbstractDatabaseManager​(java.lang.String name,
                                          int bufferSize,
                                          Layout<? extends java.io.Serializable> layout)
        Instantiates the base manager.
        Parameters:
        name - The manager name, which should include any configuration details that one might want to be able to reconfigure at runtime, such as database name, username, (hashed) password, etc.
        layout - the Appender-level layout.
        bufferSize - The size of the log event buffer.
    • Method Detail

      • getManager

        protected static <M extends AbstractDatabaseManager,​T extends AbstractDatabaseManager.AbstractFactoryData> M getManager​(java.lang.String name,
                                                                                                                                      T data,
                                                                                                                                      ManagerFactory<M,​T> factory)
        Implementations should define their own getManager method and call this method from that to create or get existing managers.
        Type Parameters:
        M - The concrete manager type.
        T - The concrete AbstractDatabaseManager.AbstractFactoryData type.
        Parameters:
        name - The manager name, which should include any configuration details that one might want to be able to reconfigure at runtime, such as database name, username, (hashed) password, etc.
        data - The concrete instance of AbstractDatabaseManager.AbstractFactoryData appropriate for the given manager.
        factory - A factory instance for creating the appropriate manager.
        Returns:
        a new or existing manager of the specified type and name.
      • buffer

        protected void buffer​(LogEvent event)
      • commitAndClose

        protected abstract boolean commitAndClose()
        Commits any active transaction (if applicable) and disconnects from the database (returns the connection to the connection pool). With buffering enabled, this is called when flushing the buffer completes, after the last call to writeInternal(org.apache.logging.log4j.core.LogEvent). With buffering disabled, this is called immediately after every invocation of writeInternal(org.apache.logging.log4j.core.LogEvent).
        Returns:
        true if all resources were closed normally, false otherwise.
      • flush

        public final void flush()
        This method is called automatically when the buffer size reaches its maximum or at the beginning of a call to shutdown(). It can also be called manually to flush events to the database.
        Specified by:
        flush in interface java.io.Flushable
      • isBuffered

        protected boolean isBuffered()
      • isRunning

        public final boolean isRunning()
        Indicates whether the manager is currently connected startup() has been called and shutdown() has not been called).
        Returns:
        true if the manager is connected.
      • releaseSub

        public final boolean releaseSub​(long timeout,
                                        java.util.concurrent.TimeUnit timeUnit)
        Description copied from class: AbstractManager
        May be overridden by managers to perform processing while the manager is being released and the lock is held. A timeout is passed for implementors to use as they see fit.
        Overrides:
        releaseSub in class AbstractManager
        Parameters:
        timeout - timeout
        timeUnit - timeout time unit
        Returns:
        true if all resources were closed normally, false otherwise.
      • shutdown

        public final boolean shutdown()
        This method is called from the AbstractManager.close() method when the appender is stopped or the appender's manager is replaced. If it has not already been called, it calls shutdownInternal() and catches any exceptions it might throw.
        Returns:
        true if all resources were closed normally, false otherwise.
      • shutdownInternal

        protected abstract boolean shutdownInternal()
                                             throws java.lang.Exception
        Implementations should implement this method to perform any proprietary disconnection / shutdown operations. This method will never be called twice on the same instance, and it will only be called after startupInternal(). It is safe to throw any exceptions from this method. This method does not necessarily disconnect from the database for the same reasons outlined in startupInternal().
        Returns:
        true if all resources were closed normally, false otherwise.
        Throws:
        java.lang.Exception
      • startup

        public final void startup()
        This method is called within the appender when the appender is started. If it has not already been called, it calls startupInternal() and catches any exceptions it might throw.
      • startupInternal

        protected abstract void startupInternal()
                                         throws java.lang.Exception
        Implementations should implement this method to perform any proprietary startup operations. This method will never be called twice on the same instance. It is safe to throw any exceptions from this method. This method does not necessarily connect to the database, as it is generally unreliable to connect once and use the same connection for hours.
        Throws:
        java.lang.Exception
      • toString

        public final java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • write

        @Deprecated
        public final void write​(LogEvent event)
        Deprecated.
        This method manages buffering and writing of events.
        Parameters:
        event - The event to write to the database.
      • write

        public final void write​(LogEvent event,
                                java.io.Serializable serializable)
        This method manages buffering and writing of events.
        Parameters:
        event - The event to write to the database.
        serializable - Serializable event
      • writeInternal

        @Deprecated
        protected void writeInternal​(LogEvent event)
        Performs the actual writing of the event in an implementation-specific way. This method is called immediately from write(LogEvent, Serializable) if buffering is off, or from flush() if the buffer has reached its limit.
        Parameters:
        event - The event to write to the database.
      • writeInternal

        protected abstract void writeInternal​(LogEvent event,
                                              java.io.Serializable serializable)
        Performs the actual writing of the event in an implementation-specific way. This method is called immediately from write(LogEvent, Serializable) if buffering is off, or from flush() if the buffer has reached its limit.
        Parameters:
        event - The event to write to the database.
      • writeThrough

        protected void writeThrough​(LogEvent event,
                                    java.io.Serializable serializable)