eventsarchiver.java
来自「opennms得相关源码 请大家看看」· Java 代码 · 共 491 行 · 第 1/2 页
JAVA
491 行
} // // Make sure we can connect to the database // try { DatabaseConnectionFactory.init(); m_dbConn = DatabaseConnectionFactory.getInstance().getConnection(); } catch (IOException ie) { throw new UndeclaredThrowableException(ie); } catch (MarshalException me) { throw new UndeclaredThrowableException(me); } catch (ValidationException ve) { throw new UndeclaredThrowableException(ve); } catch (SQLException sqlE) { throw new UndeclaredThrowableException(sqlE); } catch (ClassNotFoundException cnfE) { throw new UndeclaredThrowableException(cnfE); } } /** * Remove event with eventID from events table. NOTE: Postgres does not have * the ResultSet.deleteRow() implemented! - so use the eventID to delete! */ private boolean removeEvent(String eventID) { boolean bRet; try { m_eventDeleteStmt.setString(1, eventID); m_eventDeleteStmt.executeUpdate(); bRet = true; } catch (SQLException sqle) { bRet = false; m_logCat.error("Unable to delete event \'" + eventID + "\': " + sqle.getMessage()); } // debug logs if (bRet && m_logCat.isDebugEnabled()) { m_logCat.debug("EventID: " + eventID + " removed from events table"); } return bRet; } /** * Select the events created before 'age', log them to the archive file if * required and delete these events. * * NOTE: Postgres does not have the ResultSet.deleteRow() implemented! - so * use the eventID to delete! */ private void archiveEvents() { // number of events sent to the archive file int archCount = 0; // number of events deleted from the events table int remCount = 0; ResultSet eventsRS = null; try { m_eventsGetStmt.setTimestamp(1, new Timestamp(m_archAge)); eventsRS = m_eventsGetStmt.executeQuery(); int colCount = eventsRS.getMetaData().getColumnCount(); String eventID; String eventUEI; String eventLog; String eventDisplay; String eventAckUser; boolean ret; while (eventsRS.next()) { // get the eventID for the event eventID = eventsRS.getString(EVENT_ID); // get uei for event eventUEI = eventsRS.getString("eventUei"); // get eventLog for this row eventLog = eventsRS.getString(EVENT_LOG); // get eventDisplay for this row eventDisplay = eventsRS.getString(EVENT_DISPLAY); // eventAckUser for this event eventAckUser = eventsRS.getString(EVENT_ACK_USER); m_logCat.debug("Event id: " + eventID + " uei: " + eventUEI + " log: " + eventLog + " display: " + eventDisplay + " eventAck: " + eventAckUser); // log = N, display = N, delete event if (eventLog.equals(MSG_NO) && eventDisplay.equals(MSG_NO)) { ret = removeEvent(eventID); if (ret) { remCount++; } } // log = Y, display = N, archive event, then delete else if (eventLog.equals(MSG_YES) && eventDisplay.equals(MSG_NO)) { ret = removeEvent(eventID); if (ret) { sendToArchive(eventsRS, colCount); if (m_logCat.isDebugEnabled()) m_logCat.debug("eventID " + eventID + " archived"); archCount++; remCount++; } } // log = N, display = Y, delete event only if event has been // acknowledged else if (eventLog.equals(MSG_NO) && eventDisplay.equals(MSG_YES)) { if (eventAckUser != null) { ret = removeEvent(eventID); if (ret) remCount++; } } // log = Y, display = Y, log and delete event only if event has // been acknowledged else { if (eventAckUser != null) { ret = removeEvent(eventID); if (ret) { sendToArchive(eventsRS, colCount); if (m_logCat.isDebugEnabled()) m_logCat.debug("eventID " + eventID + " archived"); archCount++; remCount++; } } } } m_logCat.info("Number of events removed from the event table: " + remCount); m_logCat.info("Number of events sent to the archive: " + archCount); } catch (Exception oe) { m_logCat.error("EventsArchiver: Error reading events for archival: "); m_logCat.error(oe.getMessage()); } finally { try { eventsRS.close(); } catch (Exception e) { m_logCat.info("EventsArchiver: Exception while events result set: message -> " + e.getMessage()); } } } /** * Archive the current row of the result set * * @exception SQLException * thrown if there is an error getting column values from the * result set */ private void sendToArchive(ResultSet eventsRS, int colCount) throws SQLException { StringBuffer outBuf = new StringBuffer(); for (int index = 1; index <= colCount; index++) { String colValue = eventsRS.getString(index); if (index == 1) { outBuf.append(colValue); } else { outBuf.append(m_archSeparator + colValue); } } String outBufStr = outBuf.toString(); m_archCat.fatal(outBufStr); } /** * Close the database statements and the connection and close log4j * Appenders and categories */ private void close() { // // database related // try { m_eventsGetStmt.close(); m_eventDeleteStmt.close(); m_dbConn.close(); } catch (SQLException sqle) { m_logCat.warn("Error while closing database statements and connection: message -> " + sqle.getMessage()); } // // log4j related - for now a shutdown() on the categories // will do - however if these categories are ever configured // with 'SocketAppender's or 'AsyncAppender's, these appenders // should be closed explictly before shutdown() // Category.shutdown(); // m_logCat.shutdown(); } /** * The events archiver constructor - reads required properties, initializes * the database connection and the prepared statements to select and delete * events */ public EventsArchiver() throws ArchiverException { // call init init(); // initialize the prepared statements try { m_eventsGetStmt = m_dbConn.prepareStatement(DB_SELECT_EVENTS_TO_ARCHIVE); m_eventDeleteStmt = m_dbConn.prepareStatement(DB_DELETE_EVENT); } catch (SQLException e) { m_logCat.error("EventsArchiver: Exception in opening the database connection or in the prepared statement for the get events"); m_logCat.error(e); throw new ArchiverException("EventsArchiver: " + e.getMessage()); } } public static void main(String[] args) { try { // create the archiver EventsArchiver ea = new EventsArchiver(); // remove events(this method sends removed events // to archive file if configured for archival) ea.archiveEvents(); // close the archiver ea.close(); } catch (ArchiverException ae) { System.err.println(ae.getMessage()); } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?