Browse Source

防止SQLRecoverableException导致应用终止

sunyj 9 years ago
parent
commit
92a9fad818

+ 4 - 2
search-console-b2b/src/main/java/com/uas/search/console/b2b/dao/LuceneQueueMessageDao.java

@@ -4,6 +4,7 @@ import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
+import java.sql.SQLRecoverableException;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -52,7 +53,8 @@ public class LuceneQueueMessageDao {
 	 *            对消息内容进行搜索
 	 * @return 消息(实时更新情况的详细信息)
 	 */
-	public SPage<LuceneQueueMessage> findAll(Integer page, Integer size, String searchContent) {
+	public SPage<LuceneQueueMessage> findAll(Integer page, Integer size, String searchContent)
+			throws SQLRecoverableException {
 		if (StringUtils.isEmpty(searchContent)) {
 			searchContent = "";
 		}
@@ -164,7 +166,7 @@ public class LuceneQueueMessageDao {
 	 *            所要出队的消息的id
 	 * @return 是否出队成功
 	 */
-	public boolean dequeueLuceneQueueMessage(String messageId) {
+	public boolean dequeueLuceneQueueMessage(String messageId) throws SQLRecoverableException {
 		if (StringUtils.isEmpty(messageId)) {
 			return false;
 		} // 通过dbms_aq.dequeue出队,无法指定所出队的消息,所以直接通过删除表的数据间接达到出队的效果

+ 7 - 1
search-console-b2b/src/main/java/com/uas/search/console/b2b/jms/QueueMessageParser.java

@@ -12,6 +12,7 @@ import org.springframework.util.StringUtils;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONException;
 import com.alibaba.fastjson.JSONObject;
+import com.uas.search.b2b.exception.SearchException;
 import com.uas.search.b2b.service.SearchService.Table_name;
 import com.uas.search.console.b2b.model.ParsedQueueMessage;
 import com.uas.search.console.b2b.util.ClassAndTableNameUtils;
@@ -93,7 +94,12 @@ public class QueueMessageParser {
 				list.add(ClassAndTableNameUtils.createObject(clazz, id));
 			}
 		} else {
-			list = dao.findAll(ids);
+			try {
+				list = dao.findAll(ids);
+			} catch (Exception e) {
+				// 防止SQLRecoverableException导致应用终止
+				throw new SearchException(e).setDetailedMessage(e);
+			}
 		}
 
 		return list;

+ 9 - 5
search-console-b2b/src/main/java/com/uas/search/console/b2b/schedule/service/impl/TaskServiceImpl.java

@@ -149,11 +149,15 @@ public class TaskServiceImpl implements TaskService {
 		return new Runnable() {
 			@Override
 			public void run() {
-				logger.info("Task run...");
-				Executable command = taskInformation.getCommand();
-				String result = command.execute();
-				TaskLog taskLog = new TaskLog(taskInformation, new Date(), result);
-				saveLog(taskLog);
+				try {
+					logger.info("Task run...");
+					Executable command = taskInformation.getCommand();
+					String result = command.execute();
+					TaskLog taskLog = new TaskLog(taskInformation, new Date(), result);
+					saveLog(taskLog);
+				} catch (Exception e) {
+					e.printStackTrace();
+				}
 			}
 		};
 	}

+ 23 - 9
search-console-b2b/src/main/java/com/uas/search/console/b2b/service/impl/IndexServiceImpl.java

@@ -6,6 +6,7 @@ import java.io.FileNotFoundException;
 import java.io.FileReader;
 import java.io.IOException;
 import java.io.PrintWriter;
+import java.sql.SQLRecoverableException;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.HashMap;
@@ -111,6 +112,9 @@ public class IndexServiceImpl implements IndexService {
 					createIndexesFromFiles(ClassAndTableNameUtils.toClass(tableName));
 				}
 			}
+		} catch (Exception e) {
+			// 防止SQLRecoverableException导致应用终止
+			throw new SearchException(e).setDetailedMessage(e);
 		} finally {
 			creatingIndex = false;
 		}
@@ -270,10 +274,15 @@ public class IndexServiceImpl implements IndexService {
 			SearchUtils.deleteDir(new File(luceneProperties.getDataDir()));
 		}
 		Long size = 0L;
-		for (Table_name tableName : tableNames) {
-			// 指定的表数目为1时才起作用
-			size += downloadDataFromDatabase(ClassAndTableNameUtils.toClass(tableName),
-					tableNames.size() == 1 ? startFileIndex : null);
+		try {
+			for (Table_name tableName : tableNames) {
+				// 指定的表数目为1时才起作用
+				size += downloadDataFromDatabase(ClassAndTableNameUtils.toClass(tableName),
+						tableNames.size() == 1 ? startFileIndex : null);
+			}
+		} catch (Exception e) {
+			// 防止SQLRecoverableException导致应用终止
+			throw new SearchException(e).setDetailedMessage(e);
 		}
 		Long endTime = new Date().getTime();
 		logger.info(size + "条,耗时 " + (endTime - startTime) + " ms\n");
@@ -366,7 +375,6 @@ public class IndexServiceImpl implements IndexService {
 					indexWriter = indexWriterManager.get(ClassAndTableNameUtils.toTableName(obj.getClass()));
 					indexWriter.addDocument(document);
 					indexWriter.commit();
-					logger.info("Saved... " + obj + "\n");
 					return obj;
 				} catch (IOException | InterruptedException e) {
 					e.printStackTrace();
@@ -390,7 +398,6 @@ public class IndexServiceImpl implements IndexService {
 					indexWriter.updateDocument(new Term(ClassAndTableNameUtils.getIdField(obj.getClass()),
 							String.valueOf(ClassAndTableNameUtils.getId(obj))), document);
 					indexWriter.commit();
-					logger.info("Updated... " + obj + "\n");
 					return obj;
 				} catch (IOException | InterruptedException e) {
 					e.printStackTrace();
@@ -412,7 +419,6 @@ public class IndexServiceImpl implements IndexService {
 				indexWriter.deleteDocuments(new Term(ClassAndTableNameUtils.getIdField(obj.getClass()),
 						String.valueOf(ClassAndTableNameUtils.getId(obj))));
 				indexWriter.commit();
-				logger.info("Deleted... " + obj + "\n");
 				return obj;
 			} catch (IOException e) {
 				e.printStackTrace();
@@ -484,12 +490,20 @@ public class IndexServiceImpl implements IndexService {
 
 	@Override
 	public SPage<LuceneQueueMessage> getListenDetails(Integer page, Integer size, String searchContent) {
-		return luceneQueueMessageDao.findAll(page, size, searchContent);
+		try {
+			return luceneQueueMessageDao.findAll(page, size, searchContent);
+		} catch (SQLRecoverableException e) {
+			throw new SearchException(e).setDetailedMessage(e);
+		}
 	}
 
 	@Override
 	public boolean dequeueLuceneQueueMessage(String messageId) {
-		return luceneQueueMessageDao.dequeueLuceneQueueMessage(messageId);
+		try {
+			return luceneQueueMessageDao.dequeueLuceneQueueMessage(messageId);
+		} catch (SQLRecoverableException e) {
+			throw new SearchException(e).setDetailedMessage(e);
+		}
 	}
 
 }

+ 16 - 9
search-console-b2b/src/main/java/com/uas/search/console/b2b/service/impl/RealTimeUpdateMonitorServiceImpl.java

@@ -2,6 +2,7 @@ package com.uas.search.console.b2b.service.impl;
 
 import java.net.Inet4Address;
 import java.net.UnknownHostException;
+import java.sql.SQLRecoverableException;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -67,15 +68,20 @@ public class RealTimeUpdateMonitorServiceImpl implements RealTimeUpdateMonitorSe
 			@Override
 			public String execute() {
 				// 如果实时更新未正常运行,发送警告短信并自动重启实时更新服务
-				if (!workWell()) {
-					logger.error("实时更新未正常运行");
-					logger.info("发送警告短信...");
-					sendWarnSms();
-					if (aqListener.isRunning()) {
-						aqListener.stop();
+				try {
+					if (!workWell()) {
+						logger.error("实时更新未正常运行");
+						logger.info("发送警告短信...");
+						sendWarnSms();
+						if (aqListener.isRunning()) {
+							aqListener.stop();
+						}
+						aqListener.start(null);
+						return "异常";
 					}
-					aqListener.start(null);
-					return "异常";
+				} catch (SQLRecoverableException e) {
+					e.printStackTrace();
+					return "数据库连接错误";
 				}
 				return "正常";
 			}
@@ -89,8 +95,9 @@ public class RealTimeUpdateMonitorServiceImpl implements RealTimeUpdateMonitorSe
 	 * 实时更新服务是否运行良好
 	 * 
 	 * @return true 如果运行良好
+	 * @throws SQLRecoverableException
 	 */
-	private boolean workWell() {
+	private boolean workWell() throws SQLRecoverableException {
 		SPage<LuceneQueueMessage> sPage = luceneQueueMessageDao.findAll(1, 1, null);
 		// 如果消息队列中没有消息,说明没有等待更新的消息
 		if (sPage.getTotalElement() == 0) {