|
|
@@ -0,0 +1,105 @@
|
|
|
+package com.uas.search.console.dcn;
|
|
|
+
|
|
|
+import java.sql.SQLException;
|
|
|
+import java.sql.Statement;
|
|
|
+import java.util.Properties;
|
|
|
+
|
|
|
+import org.apache.commons.dbcp.BasicDataSource;
|
|
|
+import org.apache.log4j.Logger;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import com.uas.search.console.core.util.ContextUtils;
|
|
|
+import com.uas.search.console.util.SearchConstants;
|
|
|
+
|
|
|
+import oracle.jdbc.OracleConnection;
|
|
|
+import oracle.jdbc.OracleDriver;
|
|
|
+import oracle.jdbc.OracleStatement;
|
|
|
+import oracle.jdbc.dcn.DatabaseChangeRegistration;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 注册DatabaseChangeNotification,以便获取Oracle数据变化的通知
|
|
|
+ *
|
|
|
+ * @author sunyj
|
|
|
+ * @since 2016年9月14日 上午9:52:20
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class DCNRegistrationManager {
|
|
|
+
|
|
|
+ private DatabaseChangeRegistration dcnRegistration;
|
|
|
+
|
|
|
+ private Logger logger = Logger.getLogger(DCNRegistrationManager.class);
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 注册DatabaseChangeNotification
|
|
|
+ */
|
|
|
+ public void registerDataChangeNotification() {
|
|
|
+ OracleConnection connection = null;
|
|
|
+ Statement statement = null;
|
|
|
+ try {
|
|
|
+ connection = getConnection();
|
|
|
+
|
|
|
+ Properties properties = new Properties();
|
|
|
+ properties.setProperty(OracleConnection.DCN_NOTIFY_ROWIDS, "true");
|
|
|
+ properties.setProperty(OracleConnection.DCN_QUERY_CHANGE_NOTIFICATION, "true");
|
|
|
+ dcnRegistration = connection.registerDatabaseChangeNotification(properties);
|
|
|
+
|
|
|
+ DCNListener dcnListener = new DCNListener();
|
|
|
+ dcnRegistration.addListener(dcnListener);
|
|
|
+
|
|
|
+ statement = connection.createStatement();
|
|
|
+ ((OracleStatement) statement).setDatabaseChangeRegistration(dcnRegistration);
|
|
|
+
|
|
|
+ // 监测component、kind、brand、propertyvalue表变化
|
|
|
+ statement.execute(
|
|
|
+ "select cmp_uuid, cmp_code, cmp_kiid, cmp_brid from " + SearchConstants.COMPONENT_TABLE_NAME);
|
|
|
+ statement.execute("select ki_name, ki_level, ki_isleaf from " + SearchConstants.KIND_TABLE_NAME);
|
|
|
+ statement.execute("select br_uuid, br_name_cn, br_name_en from " + SearchConstants.BRAND_TABLE_NAME);
|
|
|
+ statement.execute("select pv_value from " + SearchConstants.PROPERTYVALUE_TABLE_NAME);
|
|
|
+
|
|
|
+ String[] tables = dcnRegistration.getTables();
|
|
|
+ for (String table : tables) {
|
|
|
+ logger.info("Registered DCN for table " + table);
|
|
|
+ }
|
|
|
+ } catch (SQLException e) {
|
|
|
+ // 出现异常,取消注册DCN
|
|
|
+ if (connection != null && dcnRegistration != null) {
|
|
|
+ try {
|
|
|
+ connection.unregisterDatabaseChangeNotification(dcnRegistration);
|
|
|
+ } catch (SQLException e1) {
|
|
|
+ e1.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ 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();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取数据库连接
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ * @throws SQLException
|
|
|
+ */
|
|
|
+ private OracleConnection getConnection() throws SQLException {
|
|
|
+ BasicDataSource dataSource = ContextUtils.getApplicationContext().getBean("dataSource", BasicDataSource.class);
|
|
|
+ Properties oracleProperties = new Properties();
|
|
|
+ oracleProperties.setProperty(OracleConnection.CONNECTION_PROPERTY_USER_NAME, dataSource.getUsername());
|
|
|
+ oracleProperties.setProperty(OracleConnection.CONNECTION_PROPERTY_PASSWORD, dataSource.getPassword());
|
|
|
+ return (OracleConnection) new OracleDriver().connect(dataSource.getUrl(), oracleProperties);
|
|
|
+ }
|
|
|
+}
|