|
|
@@ -1,17 +1,22 @@
|
|
|
package com.uas.search.jms;
|
|
|
|
|
|
+import com.uas.search.constant.SearchConstants;
|
|
|
import com.uas.search.constant.model.SPage;
|
|
|
import com.uas.search.schedule.TaskInformation;
|
|
|
import com.uas.search.schedule.Executable;
|
|
|
import com.uas.search.schedule.TaskService;
|
|
|
import com.uas.search.service.IndexService;
|
|
|
import com.uas.search.util.CollectionUtils;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.util.List;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
|
|
|
/**对数据库的消息队列进行实时监听
|
|
|
*
|
|
|
@@ -30,6 +35,29 @@ public class JmsListener {
|
|
|
*/
|
|
|
private static final long INTERVAL = 1000;
|
|
|
|
|
|
+ /**
|
|
|
+ * 基础索引任务
|
|
|
+ * STANDARD_INDEX_TASK 标准器件类索引任务,
|
|
|
+ * GOODS_INDEX_TASK 在售商品类索引任务,
|
|
|
+ * PRODUCTS_INDEX_TASK 物料索引任务,
|
|
|
+ * ORDERS_INDEX_TASK 订单类索引任务
|
|
|
+ */
|
|
|
+ private static final String[] BASKTASKS= {SearchConstants.STANDARD_INDEX_TASK, SearchConstants.GOODS_INDEX_TASK,
|
|
|
+ SearchConstants.PRODUCTS_INDEX_TASK, SearchConstants.ORDERS_INDEX_TASK};
|
|
|
+
|
|
|
+ // 物料类包含的索引名称
|
|
|
+ private static final String[] STANDARD_INDEX_TABLES = {SearchConstants.KIND_TABLE_NAME, SearchConstants.BRAND_TABLE_NAME, SearchConstants.COMPONENT_TABLE_NAME};
|
|
|
+
|
|
|
+ // 在售商品类包含的索引名称
|
|
|
+ private static final String[] GOODS_INDEX_TABLES = {SearchConstants.GOODS_TABLE_NAME, SearchConstants.PCB_GOODS_TABLE_NAME};
|
|
|
+
|
|
|
+ // 物料类包含的索引名称
|
|
|
+ private static final String[] PRODUCTS_INDEX_TABLES = {SearchConstants.PRODUCTS_PRIVATE_TABLE_NAME};
|
|
|
+
|
|
|
+ // 物料类包含的索引名称
|
|
|
+ private static final String[] ORDERS_INDEX_TABLES = {SearchConstants.ORDER_TABLE_NAME, SearchConstants.ORDER_INVOICE_TABLE_NAME,
|
|
|
+ SearchConstants.PURCHASE_TABLE_NAME, SearchConstants.PURCHASE_INVOICE_TABLE_NAME};
|
|
|
+
|
|
|
@Autowired
|
|
|
private LuceneMessageDao luceneMessageDao;
|
|
|
|
|
|
@@ -49,14 +77,17 @@ public class JmsListener {
|
|
|
|
|
|
private Logger logger = LoggerFactory.getLogger(getClass());
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
* 开启实时更新索引
|
|
|
*
|
|
|
* @param interval
|
|
|
* 每次接收到jms消息后等待的时间(秒)
|
|
|
+ *
|
|
|
+ * @param taskName 启动任务名称
|
|
|
* @return 开启成功与否的提示信息
|
|
|
*/
|
|
|
- public void start(Long interval) {
|
|
|
+ public void start(Long interval, String taskName) {
|
|
|
if (interval == null){
|
|
|
interval = INTERVAL;
|
|
|
} else {
|
|
|
@@ -66,58 +97,95 @@ public class JmsListener {
|
|
|
throw new IllegalArgumentException("interval 不合法:" + interval);
|
|
|
}
|
|
|
|
|
|
- if (isRunning()) {
|
|
|
- throw new IllegalStateException("已存在运行的索引实时更新服务");
|
|
|
+ if (isRunning(null)) {
|
|
|
+ throw new IllegalStateException("已存在运行的索引" + taskInformation.getTitle() + "服务");
|
|
|
}
|
|
|
|
|
|
- try {
|
|
|
- String title = "实时更新";
|
|
|
- Executable command = new Executable() {
|
|
|
- @Override
|
|
|
- public String execute() {
|
|
|
- long startTime = System.currentTimeMillis();
|
|
|
- SPage<LuceneMessage> sPage = luceneMessageService.findAll(1, 100);
|
|
|
- List<LuceneMessage> luceneMessages = sPage.getContent();
|
|
|
- if (CollectionUtils.isEmpty(luceneMessages)) {
|
|
|
- return "无消息";
|
|
|
- }
|
|
|
- logger.info("获取luceneMessages" + luceneMessages.size() + "条,耗时:" + (System.currentTimeMillis() - startTime));
|
|
|
- for (LuceneMessage luceneMessage : luceneMessages){
|
|
|
- try {
|
|
|
- process(luceneMessage);
|
|
|
- } catch (Throwable e) {
|
|
|
- logger.error("", e);
|
|
|
- }
|
|
|
- }
|
|
|
- logger.info("处理luceneMessages" + luceneMessages.size() + "条,耗时:" + (System.currentTimeMillis() - startTime));
|
|
|
- // 如果消息不止一页,立即消费下一页
|
|
|
- if (sPage.getTotalPage() > 1){
|
|
|
- execute();
|
|
|
- }
|
|
|
- return "正常";
|
|
|
+ // 启动默认索引更新任务
|
|
|
+ if (StringUtils.isEmpty(taskName)) {
|
|
|
+ for (String baseTask : BASKTASKS) {
|
|
|
+ if (isRunning(baseTask)) {
|
|
|
+ throw new IllegalStateException("已存在运行的" + baseTask + "索引实时更新服务");
|
|
|
+ } else {
|
|
|
+ createTask(interval, baseTask);
|
|
|
}
|
|
|
- };
|
|
|
- taskInformation = new TaskInformation(title, command, INITIAL_DELAY, interval, TaskInformation.ScheduleType.FixedDelay);
|
|
|
- taskService.newTask(taskInformation);
|
|
|
- if (!taskService.isStopped()) {
|
|
|
- taskService.stop();
|
|
|
}
|
|
|
- taskService.start();
|
|
|
- } catch (Throwable e) {
|
|
|
- if (taskInformation != null) {
|
|
|
- taskInformation = null;
|
|
|
- }
|
|
|
- throw new IllegalStateException("开启失败", e);
|
|
|
+ // 启动指定索引更新任务
|
|
|
+ } else {
|
|
|
+
|
|
|
}
|
|
|
+
|
|
|
+// try {
|
|
|
+// createTask(interval, "实时更新");
|
|
|
+// } catch (Throwable e) {
|
|
|
+// if (taskInformation != null) {
|
|
|
+// taskInformation = null;
|
|
|
+// }
|
|
|
+// throw new IllegalStateException("开启失败", e);
|
|
|
+// }
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 创建索引更新任务
|
|
|
+ * @param interval 任务间隔时间
|
|
|
+ * @param title 任务名称
|
|
|
+ */
|
|
|
+ private void createTask(Long interval, String title) {
|
|
|
+ Executable command = new Executable() {
|
|
|
+ @Override
|
|
|
+ public String execute() {
|
|
|
+ long startTime = System.currentTimeMillis();
|
|
|
+ String[] tablenames = null;
|
|
|
+ switch (title) {
|
|
|
+ case SearchConstants.STANDARD_INDEX_TASK:
|
|
|
+ tablenames = STANDARD_INDEX_TABLES;
|
|
|
+ break;
|
|
|
+ case SearchConstants.GOODS_INDEX_TASK:
|
|
|
+ tablenames = GOODS_INDEX_TABLES;
|
|
|
+ break;
|
|
|
+ case SearchConstants.PRODUCTS_INDEX_TASK:
|
|
|
+ tablenames = PRODUCTS_INDEX_TABLES;
|
|
|
+ break;
|
|
|
+ case SearchConstants.ORDERS_INDEX_TASK:
|
|
|
+ tablenames = ORDERS_INDEX_TABLES;
|
|
|
+ break;
|
|
|
+ default: break;
|
|
|
+ }
|
|
|
+ SPage<LuceneMessage> sPage = luceneMessageService.findAll(1, 100, tablenames);
|
|
|
+ List<LuceneMessage> luceneMessages = sPage.getContent();
|
|
|
+ if (CollectionUtils.isEmpty(luceneMessages)) {
|
|
|
+ return "无消息";
|
|
|
+ }
|
|
|
+ logger.info("获取luceneMessages" + luceneMessages.size() + "条,耗时:" + (System.currentTimeMillis() - startTime));
|
|
|
+ for (LuceneMessage luceneMessage : luceneMessages){
|
|
|
+ try {
|
|
|
+ process(luceneMessage);
|
|
|
+ } catch (Throwable e) {
|
|
|
+ logger.error("", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ logger.info("处理luceneMessages" + luceneMessages.size() + "条,耗时:" + (System.currentTimeMillis() - startTime));
|
|
|
+ // 如果消息不止一页,立即消费下一页
|
|
|
+ if (sPage.getTotalPage() > 1){
|
|
|
+ execute();
|
|
|
+ }
|
|
|
+ return "正常";
|
|
|
+ }
|
|
|
+ };
|
|
|
+ taskInformation = new TaskInformation(title, command, INITIAL_DELAY, interval, TaskInformation.ScheduleType.FixedDelay);
|
|
|
+ taskService.newTask(taskInformation);
|
|
|
+// if (!taskService.isStopped()) {
|
|
|
+// taskService.stop();
|
|
|
+// }
|
|
|
+ taskService.start();
|
|
|
+ }
|
|
|
/**
|
|
|
* 关闭实时更新索引服务
|
|
|
*
|
|
|
* @return 关闭成功与否的提示信息
|
|
|
*/
|
|
|
public void stop() {
|
|
|
- if (!isRunning()) {
|
|
|
+ if (!isRunning(null)) {
|
|
|
throw new IllegalStateException("索引实时更新服务未开启或已关闭");
|
|
|
} else {
|
|
|
taskService.remove(taskInformation.getCode());
|
|
|
@@ -130,10 +198,11 @@ public class JmsListener {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * @param title 索引名称
|
|
|
* @return 索引实时更新服务是否正在运行
|
|
|
*/
|
|
|
- public boolean isRunning() {
|
|
|
- return taskInformation != null && taskService.exist(taskInformation.getCode());
|
|
|
+ public boolean isRunning(String title) {
|
|
|
+ return taskInformation != null && taskService.exist(StringUtils.isEmpty(title) ? taskInformation.getCode() : title);
|
|
|
}
|
|
|
|
|
|
/**
|