Browse Source

设置接收DCN通知的服务器的ip;清理数据库中旧的DCN注册

sunyj 9 years ago
parent
commit
9101bbcd4f

+ 58 - 5
search-console/src/main/java/com/uas/search/console/dcn/DCNRegistrationManager.java

@@ -1,5 +1,8 @@
 package com.uas.search.console.dcn;
 
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Statement;
 import java.util.Properties;
@@ -25,7 +28,7 @@ import oracle.jdbc.dcn.DatabaseChangeRegistration;
 @Service
 public class DCNRegistrationManager {
 
-	private DatabaseChangeRegistration dcnRegistration;
+	private static DatabaseChangeRegistration dcnRegistration;
 
 	private Logger logger = Logger.getLogger(DCNRegistrationManager.class);
 
@@ -40,15 +43,23 @@ public class DCNRegistrationManager {
 			return message;
 		}
 
+		unregisterOldDCNsInDatabase();
+
 		OracleConnection connection = null;
 		Statement statement = null;
 		try {
 			connection = getConnection();
 
+			String localHost = InetAddress.getLocalHost().getHostAddress();
+
 			// 根据配置注册DatabaseChangeNotification
 			Properties properties = new Properties();
 			properties.setProperty(OracleConnection.DCN_NOTIFY_ROWIDS, "true");
 			properties.setProperty(OracleConnection.DCN_QUERY_CHANGE_NOTIFICATION, "true");
+			// 设置监听程序的ip
+			properties.setProperty(OracleConnection.NTF_LOCAL_HOST, localHost);
+			// 设置超时为永不过期
+			properties.setProperty(OracleConnection.NTF_TIMEOUT, "0");
 			dcnRegistration = connection.registerDatabaseChangeNotification(properties);
 
 			// 绑定DCNListener
@@ -66,23 +77,25 @@ public class DCNRegistrationManager {
 			statement
 					.execute("select pv_id, pv_componentid, pv_value from " + SearchConstants.PROPERTYVALUE_TABLE_NAME);
 
-			message = "成功注册DCN: " + dcnRegistration.getRegId();
+			message = "成功注册DCN: id=" + dcnRegistration.getRegId() + ", ip=" + localHost;
 			logger.info(message);
 			String[] tables = dcnRegistration.getTables();
 			for (String table : tables) {
 				logger.info("注册DCN: " + table);
 			}
-		} catch (SQLException e) {
+		} catch (SQLException | UnknownHostException e) {
 			// 出现异常,取消注册DCN
 			if (connection != null && dcnRegistration != null) {
 				try {
-					message = "出现SQLException,取消注册DCN: " + dcnRegistration.getRegId();
+					message = "出现SQLException或UnknownHostException,取消注册DCN: " + dcnRegistration.getRegId();
 					logger.error(message);
 					connection.unregisterDatabaseChangeNotification(dcnRegistration);
 				} catch (SQLException e1) {
 					message = "取消注册DCN失败: " + dcnRegistration.getRegId();
 					logger.error(message);
 					e1.printStackTrace();
+				} finally {
+					dcnRegistration = null;
 				}
 			}
 			e.printStackTrace();
@@ -122,13 +135,13 @@ public class DCNRegistrationManager {
 				connection.unregisterDatabaseChangeNotification(dcnRegistration);
 				message = "成功取消注册DCN: " + dcnRegistration.getRegId();
 				logger.info(message);
-				dcnRegistration = null;
 			}
 		} catch (SQLException e) {
 			message = "取消注册DCN失败: " + dcnRegistration.getRegId();
 			logger.error(message);
 			e.printStackTrace();
 		} finally {
+			dcnRegistration = null;
 			if (connection != null) {
 				try {
 					connection.close();
@@ -153,4 +166,44 @@ public class DCNRegistrationManager {
 		oracleProperties.setProperty(OracleConnection.CONNECTION_PROPERTY_PASSWORD, dataSource.getPassword());
 		return (OracleConnection) new OracleDriver().connect(dataSource.getUrl(), oracleProperties);
 	}
+
+	/**
+	 * 清理数据库中的DCN注册(因程序未正确关闭)
+	 */
+	private void unregisterOldDCNsInDatabase() {
+		OracleConnection connection = null;
+		Statement statement = null;
+		try {
+			connection = getConnection();
+			statement = connection.createStatement();
+			String localHost = InetAddress.getLocalHost().getHostAddress();
+			ResultSet resultSet = statement
+					.executeQuery("select regid, callback from user_change_notification_regs where callback like '%"
+							+ localHost + "%'");
+			while (resultSet.next()) {
+				Long regId = resultSet.getLong(1);
+				String callback = resultSet.getString(2);
+				connection.unregisterDatabaseChangeNotification(regId, callback);
+				logger.info("成功清理数据库中的DCN: regId=" + regId + " callback=" + callback);
+			}
+		} catch (SQLException | UnknownHostException e) {
+			logger.info("清理数据库DCN失败");
+			e.printStackTrace();
+		} finally {
+			if (statement != null) {
+				try {
+					statement.close();
+				} catch (SQLException e) {
+					e.printStackTrace();
+				}
+			}
+			if (connection != null) {
+				try {
+					connection.close();
+				} catch (SQLException e) {
+					e.printStackTrace();
+				}
+			}
+		}
+	}
 }