ソースを参照

新增开启、停止、重启实时更新索引服务的接口

sunyj 9 年 前
コミット
dade62c547

+ 35 - 58
search-console/src/main/java/com/uas/search/console/controller/IndexController.java

@@ -1,20 +1,12 @@
 package com.uas.search.console.controller;
 
-import java.util.List;
-
 import org.apache.log4j.Logger;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.ResponseBody;
 
-import com.uas.search.console.dao.BrandSimpleInfoDao;
-import com.uas.search.console.dao.ComponentSimpleInfoDao;
-import com.uas.search.console.dao.KindSimpleInfoDao;
 import com.uas.search.console.dcn.DCNRegistrationManager;
-import com.uas.search.console.model.BrandSimpleInfo;
-import com.uas.search.console.model.ComponentSimpleInfo;
-import com.uas.search.console.model.KindSimpleInfo;
 import com.uas.search.console.service.IndexService;
 
 /**
@@ -38,14 +30,14 @@ public class IndexController {
 	/**
 	 * 创建索引
 	 * 
-	 * @param componentDataSource
-	 *            器件数据的来源,为1表示从数据库获取,为null或其他值表示从本地文件获取
+	 * @param fromDB
+	 *            器件数据的来源是否为数据库,为1表示从数据库获取,为null或其他值表示从本地文件获取
 	 * @return 所用时间 ms
 	 */
 	@RequestMapping("/create")
 	@ResponseBody
-	public String createIndexes(Integer componentDataSource) {
-		return "Indexes created success in " + indexService.createIndexes(componentDataSource) + " ms.";
+	public String createIndexes(Integer fromDB) {
+		return "Indexes created success in " + indexService.createIndexes(fromDB) + " ms.";
 	}
 
 	/**
@@ -56,63 +48,48 @@ public class IndexController {
 	@RequestMapping("/realTimeUpdate")
 	@ResponseBody
 	public String registerDatabaseChangeNotification() {
-		dcnRegistrationManager.registerDataChangeNotification();
-		return "Started real-time updating for lucene index...";
-	}
-
-	@RequestMapping("/downloadComponentData")
-	@ResponseBody
-	public String downloadComponentData() {
-		Long size = indexService.downloadComponentData();
-		logger.info("Downloading finished, components' size " + size);
-		return "Components' size: " + size;
-	}
-
-	@Autowired
-	private ComponentSimpleInfoDao componentDao;
-
-	@RequestMapping("/rowTest/component")
-	@ResponseBody
-	public String testROWIDComponent() {
-		System.out.println(componentDao.findByRowid("AACu7GAAEAAOZvTAAA"));
-		return "created";
-	}
-
-	@Autowired
-	private BrandSimpleInfoDao brandDao;
-
-	@RequestMapping("/rowTest/brand")
-	@ResponseBody
-	public String testROWIDBrand() {
-		System.out.println(brandDao.findByRowid("AACoH2AAEAANmqSAAB"));
-		return "created";
+		return dcnRegistrationManager.registerDataChangeNotification();
 	}
 
-	@Autowired
-	private KindSimpleInfoDao kindDao;
-
-	@RequestMapping("/rowTest/kind")
+	/**
+	 * 开启实时更新索引的程序
+	 * 
+	 * @return
+	 */
+	@RequestMapping("/realTimeUpdate/start")
 	@ResponseBody
-	public String testROWIDKind() {
-		System.out.println(kindDao.findByRowid("AACmtMAAEAAFmG8AAY"));
-		return "created";
+	public String startRegisterDatabaseChangeNotification() {
+		return dcnRegistrationManager.registerDataChangeNotification();
 	}
 
-	@RequestMapping("/findAll/kind")
+	/**
+	 * 关闭实时更新索引的程序
+	 * 
+	 * @return
+	 */
+	@RequestMapping("/realTimeUpdate/stop")
 	@ResponseBody
-	public List<KindSimpleInfo> findAllKinds() {
-		return kindDao.findAll();
+	public String unregisterDatabaseChangeNotification() {
+		return dcnRegistrationManager.unregisterDataChangeNotification();
 	}
 
-	@RequestMapping("/findAll/brand")
+	/**
+	 * 重启实时更新索引的程序
+	 * 
+	 * @return
+	 */
+	@RequestMapping("/realTimeUpdate/restart")
 	@ResponseBody
-	public List<BrandSimpleInfo> findAllBrands() {
-		return brandDao.findAll();
+	public String restartRegisterDatabaseChangeNotification() {
+		dcnRegistrationManager.unregisterDataChangeNotification();
+		return dcnRegistrationManager.registerDataChangeNotification();
 	}
 
-	@RequestMapping("/findAll/component")
+	@RequestMapping("/downloadComponentData")
 	@ResponseBody
-	public List<ComponentSimpleInfo> findAllComponents() {
-		return componentDao.findAll();
+	public String downloadComponentData() {
+		Long size = indexService.downloadComponentData();
+		logger.info("Downloading finished, components' size " + size);
+		return "Components' size: " + size;
 	}
 }

+ 55 - 3
search-console/src/main/java/com/uas/search/console/dcn/DCNRegistrationManager.java

@@ -32,17 +32,26 @@ public class DCNRegistrationManager {
 	/**
 	 * 注册DatabaseChangeNotification
 	 */
-	public void registerDataChangeNotification() {
+	public String registerDataChangeNotification() {
+		String message = "";
+		if (dcnRegistration != null) {
+			message = "已存在注册的DCN";
+			logger.info(message);
+			return message;
+		}
+
 		OracleConnection connection = null;
 		Statement statement = null;
 		try {
 			connection = getConnection();
 
+			// 根据配置注册DatabaseChangeNotification
 			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 dcnListener = new DCNListener();
 			dcnRegistration.addListener(dcnListener);
 
@@ -54,18 +63,25 @@ public class DCNRegistrationManager {
 					+ SearchConstants.COMPONENT_TABLE_NAME);
 			statement.execute("select ki_id, ki_name, ki_level, ki_isleaf from " + SearchConstants.KIND_TABLE_NAME);
 			statement.execute("select br_id, br_uuid, br_name_cn, br_name_en from " + SearchConstants.BRAND_TABLE_NAME);
-			statement.execute("select pv_id, pv_componentid, pv_value from " + SearchConstants.PROPERTYVALUE_TABLE_NAME);
+			statement
+					.execute("select pv_id, pv_componentid, pv_value from " + SearchConstants.PROPERTYVALUE_TABLE_NAME);
 
+			message = "成功注册DCN: " + dcnRegistration.getRegId();
+			logger.info(message);
 			String[] tables = dcnRegistration.getTables();
 			for (String table : tables) {
-				logger.info("Registered DCN for table " + table);
+				logger.info("注册DCN: " + table);
 			}
 		} catch (SQLException e) {
 			// 出现异常,取消注册DCN
 			if (connection != null && dcnRegistration != null) {
 				try {
+					message = "出现SQLException,取消注册DCN: " + dcnRegistration.getRegId();
+					logger.error(message);
 					connection.unregisterDatabaseChangeNotification(dcnRegistration);
 				} catch (SQLException e1) {
+					message = "取消注册DCN失败: " + dcnRegistration.getRegId();
+					logger.error(message);
 					e1.printStackTrace();
 				}
 			}
@@ -86,6 +102,42 @@ public class DCNRegistrationManager {
 				}
 			}
 		}
+		return message;
+	}
+
+	/**
+	 * 取消注册DatabaseChangeNotification
+	 */
+	public String unregisterDataChangeNotification() {
+		String message = "";
+		if (dcnRegistration == null) {
+			message = "DCN未注册或已被取消注册";
+			logger.info(message);
+			return message;
+		}
+		OracleConnection connection = null;
+		try {
+			connection = getConnection();
+			if (connection != null) {
+				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 {
+			if (connection != null) {
+				try {
+					connection.close();
+				} catch (SQLException e) {
+					e.printStackTrace();
+				}
+			}
+		}
+		return message;
 	}
 
 	/**

+ 3 - 3
search-console/src/main/java/com/uas/search/console/service/IndexService.java

@@ -11,11 +11,11 @@ public interface IndexService {
 	/**
 	 * 创建索引
 	 * 
-	 * @param componentDataSource
-	 *            器件数据的来源,为1表示从数据库获取,为null或其他值表示从本地文件获取
+	 * @param fromDB
+	 *            器件数据的来源是否为数据库,为1表示从数据库获取,为null或其他值表示从本地文件获取
 	 * @return 创建的索引花费总时间 ms
 	 */
-	public Long createIndexes(Integer componentDataSource);
+	public Long createIndexes(Integer fromDB);
 
 	/**
 	 * 下载component数据至本地

+ 2 - 2
search-console/src/main/java/com/uas/search/console/service/impl/IndexServiceImpl.java

@@ -82,7 +82,7 @@ public class IndexServiceImpl implements IndexService {
 	}
 
 	@Override
-	public Long createIndexes(Integer componentDataSource) {
+	public Long createIndexes(Integer fromDB) {
 		try {
 			if (IndexWriter.isLocked(directory)) {
 				logger.warn("已有线程正在创建索引!");
@@ -111,7 +111,7 @@ public class IndexServiceImpl implements IndexService {
 			logger.info("创建品牌索引: " + brandSize + "条,耗时 " + (brandTime - kindTime) + " ms\n");
 
 			Long componentSize = 0L;
-			if (componentDataSource != null && componentDataSource == 1) {
+			if (fromDB != null && fromDB == 1) {
 				componentSize = createComponentIndexesFromDatabase();
 			} else {
 				componentSize = createComponentIndexesFromFiles();

+ 5 - 0
search-console/src/main/webapp/WEB-INF/webmvc.xml

@@ -20,6 +20,11 @@
 					</list>
 				</property>
 			</bean>
+			<!-- 解决Controller返回String时中文乱码的问题 -->
+			<bean class="org.springframework.http.converter.StringHttpMessageConverter">
+				<!-- 不可为text/plain -->
+				<property name="supportedMediaTypes" value="text/html;charset=UTF-8" />
+			</bean>
 		</mvc:message-converters>
 	</mvc:annotation-driven>