eventsarchiver.java

来自「opennms得相关源码 请大家看看」· Java 代码 · 共 491 行 · 第 1/2 页

JAVA
491
字号
//// This file is part of the OpenNMS(R) Application.//// OpenNMS(R) is Copyright (C) 2002-2003 The OpenNMS Group, Inc.  All rights reserved.// OpenNMS(R) is a derivative work, containing both original code, included code and modified// code that was published under the GNU General Public License. Copyrights for modified // and included code are below.//// OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.//// Modifications://// 31 Jan 2003: Cleaned up some unused imports.//// Original code base Copyright (C) 1999-2001 Oculan Corp.  All rights reserved.//// This program is free software; you can redistribute it and/or modify// it under the terms of the GNU General Public License as published by// the Free Software Foundation; either version 2 of the License, or// (at your option) any later version.//// This program is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the// GNU General Public License for more details.//// You should have received a copy of the GNU General Public License// along with this program; if not, write to the Free Software// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.//// For more information contact://      OpenNMS Licensing       <license@opennms.org>//      http://www.opennms.org///      http://www.opennms.com///// Tab Stop = 8//package org.opennms.netmgt.archive;import java.io.IOException;import java.lang.reflect.UndeclaredThrowableException;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Timestamp;import org.apache.log4j.Category;import org.exolab.castor.xml.MarshalException;import org.exolab.castor.xml.ValidationException;import org.opennms.core.utils.ThreadCategory;import org.opennms.core.utils.TimeConverter;import org.opennms.netmgt.config.DatabaseConnectionFactory;import org.opennms.netmgt.config.EventsArchiverConfigFactory;/** * <pre> *  *  The EventsArchiver is responsible for archiving and removing events *  from the 'events' table.  *  *  The archival/deletion depends on the 'eventLog' and the 'eventDisplay' *  values for the event - *  *  If the 'eventLog == N and the eventDisplay == N', *  the event is simply deleted from the events table *  *  If the 'eventLog == N and the eventDisplay == Y', *  the event is deleted ONLY if the event has been acknowledged *  *  If the 'eventLog == Y and the eventDisplay == N', *  the event is sent to the archive file and deleted from the table *  *  If the 'eventLog == Y and the eventDisplay == Y', *  the event is sent to the archive file and deleted from the table *  ONLY if the event has been acknowledged *  *  An event is considered acknowledged if the 'eventAckUser' column has *  a non-null value *  *  An EventsArchiver run depends on attributes in the events archiver *  configuration file. The following are the properties that govern a run - *  *  - archiveAge *    This determines which events are to be removed - i.e events older *    than current time minus this time are removed *  *  - separator *    This is the separator used in between event table column values when an *    event is written to the archive file *  *  The EventsArchiver uses Apache's log4j both for its output logs and for *  the actual archiving itself - the set up for the log4j appenders for *  this archiver are all doneexclusively in the 'events.archiver.properties' *  property file *  *  A {@link org.apache.log4j.RollingFileAppender RollingFileAppender} is used *  for the archive file with the defaults for this being to roll when the *  size is 100KB with the number of backups set to 4. *  *  @author &lt;A HREF=&quot;mailto:sowmya@opennms.org&quot;&gt;Sowmya Nataraj&lt;/A&gt; *  @author &lt;A HREF=&quot;http://www.opennms.org&quot;&gt;OpenNMS&lt;/A&gt; *  */public class EventsArchiver {    /**     * The SQL statement to select events that have their eventCreateTime     * earlier than a specified age     */    private static final String DB_SELECT_EVENTS_TO_ARCHIVE = "SELECT * from events where (eventcreatetime < ?)";    /**     * The SQL statement to delete events based on their eventID     */    private static final String DB_DELETE_EVENT = "DELETE from events where (eventID = ?)";    /**     * The column name for eventID in the events table     */    private static final String EVENT_ID = "eventID";    /**     * The column name for 'eventLog' in the events table     */    private static final String EVENT_LOG = "eventLog";    /**     * The column name for 'eventDisplay' in the events table     */    private static final String EVENT_DISPLAY = "eventDisplay";    /**     * The column name for 'eventAckUser' in the events table     */    private static final String EVENT_ACK_USER = "eventAckUser";    /**     * The value for the event log or display field if set to true     */    private static final String MSG_YES = "Y";    /**     * The value for the event log or display field if set to false     */    private static final String MSG_NO = "N";    /**     * The log4j category for this class' output logs     */    private Category m_logCat;    /**     * The log4j category for the archive file     */    private Category m_archCat;    /**     * The archive age in milliseconds. Events created before this are     * archived/deleted.     */    private long m_archAge;    /**     * The separator to be used when writing events into the archive     */    private String m_archSeparator;    /**     * The database connection     */    private Connection m_dbConn;    /**     * The prepared statement to select the events     */    private PreparedStatement m_eventsGetStmt;    /**     * The prepared statement to delete the events     */    private PreparedStatement m_eventDeleteStmt;    /**     * Read the required properties and set up the logs, the archive etc.     *      * @exception ArchiverException     *                thrown if a required property is not specified or is     *                incorrect     */    private void init() throws ArchiverException {        // The general logs from the events archiver go to this category        ThreadCategory.setPrefix("OpenNMS.Archiver.Events");        m_logCat = ThreadCategory.getInstance("OpenNMS.Archiver.Events");        // The archive logs go to this category        m_archCat = Category.getInstance("EventsArchiver");        // set additivity for this to false so that logs from here        // do not go to the root category        m_archCat.setAdditivity(false);        try {            EventsArchiverConfigFactory.init();            EventsArchiverConfigFactory eaFactory = EventsArchiverConfigFactory.getInstance();            // get archive age            String archAgeStr = eaFactory.getArchiveAge();            long archAge;            try {                archAge = TimeConverter.convertToMillis(archAgeStr);            } catch (NumberFormatException nfe) {                throw new ArchiverException("Archive age: " + archAgeStr + "- Incorrect format " + nfe.getMessage());            }            // set actual time that is to be used for the select from the            // database            m_archAge = System.currentTimeMillis() - archAge;            //            // get the separator to be used between column names in the archive            //            String separator = eaFactory.getSeparator();            if (separator == null) {                m_archSeparator = "#";            } else {                m_archSeparator = separator;            }            // info logs            if (m_logCat.isInfoEnabled()) {                // get this in readable format                archAgeStr = (new java.util.Date(m_archAge)).toString();                m_logCat.info("Events archive age specified = " + archAgeStr);                m_logCat.info("Events archive age in millisconds = " + archAge);                m_logCat.info("Events created before \'" + archAgeStr + " \' will be deleted");                m_logCat.info("Separator to be used in archive: " + m_archSeparator);            }        } catch (MarshalException ex) {            throw new UndeclaredThrowableException(ex);        } catch (ValidationException ex) {            throw new UndeclaredThrowableException(ex);        } catch (IOException ex) {            throw new UndeclaredThrowableException(ex);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?