Jelajahi Sumber

防止SQLRecoverableException导致应用终止

sunyj 9 tahun lalu
induk
melakukan
7ce145a866

+ 8 - 1
search-console/src/main/java/com/uas/search/console/controller/IndexController.java

@@ -13,6 +13,7 @@ import org.springframework.web.bind.annotation.ResponseBody;
 import com.uas.search.console.jms.AQListener;
 import com.uas.search.console.model.LuceneQueueMessage;
 import com.uas.search.console.service.IndexService;
+import com.uas.search.exception.SearchException;
 import com.uas.search.model.SPage;
 
 /**
@@ -48,7 +49,13 @@ public class IndexController {
 	@RequestMapping("/downloadComponentData")
 	@ResponseBody
 	public String downloadComponentDataFromDatabase(Integer startFileIndex) {
-		return "Data downloaded success in " + indexService.downloadComponentDataFromDatabase(startFileIndex) + " ms.";
+		try {
+			return "Data downloaded success in " + indexService.downloadComponentDataFromDatabase(startFileIndex)
+					+ " ms.";
+		} catch (Exception e) {
+			// 防止SQLRecoverableException导致应用终止
+			throw new SearchException(e).setDetailedMessage(e);
+		}
 	}
 
 	@RequestMapping("/listen/start")

+ 4 - 2
search-console/src/main/java/com/uas/search/console/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出队,无法指定所出队的消息,所以直接通过删除表的数据间接达到出队的效果

+ 21 - 15
search-console/src/main/java/com/uas/search/console/jms/QueueMessageParser.java

@@ -93,21 +93,27 @@ public class QueueMessageParser {
 		// 解析哪个表有更改
 		Object[] objects = null;
 		String table = jsonObject.getString("table");
-		if (table.equals(SearchConstants.KIND_TABLE_NAME)) {
-			objects = parseKind(jsonObject);
-		} else if (table.equals(SearchConstants.BRAND_TABLE_NAME)) {
-			objects = parseBrand(jsonObject);
-		} else if (table.equals(SearchConstants.COMPONENT_TABLE_NAME)) {
-			objects = parseComponent(jsonObject);
-		} else if (table.equals(SearchConstants.ORDER_TABLE_NAME)) {
-			objects = parseOrder(jsonObject);
-		} else if (table.equals(SearchConstants.ORDER_INVOICE_TABLE_NAME)) {
-			objects = parseOrderInvoice(jsonObject);
-		} else if (table.equals(SearchConstants.PURCHASE_TABLE_NAME)) {
-			objects = parsePurchase(jsonObject);
-		} else if (table.equals(SearchConstants.PURCHASE_INVOICE_TABLE_NAME)) {
-			objects = parsePurchaseInvoice(jsonObject);
-		} else {
+		try {
+			if (table.equals(SearchConstants.KIND_TABLE_NAME)) {
+				objects = parseKind(jsonObject);
+			} else if (table.equals(SearchConstants.BRAND_TABLE_NAME)) {
+				objects = parseBrand(jsonObject);
+			} else if (table.equals(SearchConstants.COMPONENT_TABLE_NAME)) {
+				objects = parseComponent(jsonObject);
+			} else if (table.equals(SearchConstants.ORDER_TABLE_NAME)) {
+				objects = parseOrder(jsonObject);
+			} else if (table.equals(SearchConstants.ORDER_INVOICE_TABLE_NAME)) {
+				objects = parseOrderInvoice(jsonObject);
+			} else if (table.equals(SearchConstants.PURCHASE_TABLE_NAME)) {
+				objects = parsePurchase(jsonObject);
+			} else if (table.equals(SearchConstants.PURCHASE_INVOICE_TABLE_NAME)) {
+				objects = parsePurchaseInvoice(jsonObject);
+			} else {
+				return null;
+			}
+		} catch (Exception e) {
+			// 防止SQLRecoverableException导致应用终止
+			e.printStackTrace();
 			return null;
 		}
 

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

@@ -148,11 +148,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();
+				}
 			}
 		};
 	}

+ 37 - 33
search-console/src/main/java/com/uas/search/console/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;
@@ -171,14 +172,12 @@ public class IndexServiceImpl implements IndexService {
 			Long endTime = new Date().getTime();
 			logger.info(String.format("索引创建成功, 共用时间%.2fs\n", (endTime - startTime) / 1000.0));
 			return endTime - startTime;
-		} catch (IOException e) {
-			e.printStackTrace();
-		} catch (InterruptedException e) {
-			e.printStackTrace();
+		} catch (Exception e) {
+			// 防止SQLRecoverableException导致应用终止
+			throw new SearchException(e).setDetailedMessage(e);
 		} finally {
 			creatingIndex = false;
 		}
-		return null;
 	}
 
 	/**
@@ -369,30 +368,6 @@ public class IndexServiceImpl implements IndexService {
 		return count;
 	}
 
-	@Override
-	public Object save(Object obj) {
-		if (obj != null) {
-			String tableName = SearchUtils.getTableName(obj.getClass());
-			Document document = ObjectToDocumentUtils.toDocument(obj);
-			if (document != null) {
-				try {
-					indexWriter = indexWriterManager.get(tableName);
-					indexWriter.addDocument(document);
-					indexWriter.commit();
-					logger.info("Saved... " + obj + "\n");
-					return obj;
-				} catch (IOException | InterruptedException e) {
-					e.printStackTrace();
-				} finally {
-					indexWriterManager.release(tableName);
-				}
-			} else {
-				logger.info("对象转为Document时为null:" + obj);
-			}
-		}
-		return null;
-	}
-
 	@Override
 	public Long downloadComponentDataFromDatabase(Integer startFileIndex) {
 		int fileIndex = 1;
@@ -463,6 +438,29 @@ public class IndexServiceImpl implements IndexService {
 		return endTime - startTime;
 	}
 
+	@Override
+	public Object save(Object obj) {
+		if (obj != null) {
+			String tableName = SearchUtils.getTableName(obj.getClass());
+			Document document = ObjectToDocumentUtils.toDocument(obj);
+			if (document != null) {
+				try {
+					indexWriter = indexWriterManager.get(tableName);
+					indexWriter.addDocument(document);
+					indexWriter.commit();
+					return obj;
+				} catch (IOException | InterruptedException e) {
+					e.printStackTrace();
+				} finally {
+					indexWriterManager.release(tableName);
+				}
+			} else {
+				logger.info("对象转为Document时为null:" + obj);
+			}
+		}
+		return null;
+	}
+
 	@Override
 	public Object update(Object obj) {
 		if (obj != null) {
@@ -497,7 +495,6 @@ public class IndexServiceImpl implements IndexService {
 						throw new SearchException("Message parsing failed!");
 					}
 					indexWriter.commit();
-					logger.info("Updated... " + obj + "\n");
 					return obj;
 				} catch (IOException | InterruptedException e) {
 					e.printStackTrace();
@@ -542,7 +539,6 @@ public class IndexServiceImpl implements IndexService {
 					throw new SearchException("Message parsing failed!");
 				}
 				indexWriter.commit();
-				logger.info("Deleted... " + obj + "\n");
 				return obj;
 			} catch (IOException e) {
 				e.printStackTrace();
@@ -614,12 +610,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);
+		}
 	}
 
 }

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

@@ -2,6 +2,7 @@ package com.uas.search.console.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 "正常";
 			}
@@ -90,7 +96,7 @@ public class RealTimeUpdateMonitorServiceImpl implements RealTimeUpdateMonitorSe
 	 * 
 	 * @return true 如果运行良好
 	 */
-	private boolean workWell() {
+	private boolean workWell()  throws SQLRecoverableException {
 		SPage<LuceneQueueMessage> sPage = luceneQueueMessageDao.findAll(1, 1, null);
 		// 如果消息队列中没有消息,说明没有等待更新的消息
 		if (sPage.getTotalElement() == 0) {