|
|
1
|
+package com.yoho.search.consumer.index.increment;
|
|
|
2
|
+
|
|
|
3
|
+import java.util.Map;
|
|
|
4
|
+
|
|
|
5
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
6
|
+import org.slf4j.Logger;
|
|
|
7
|
+import org.slf4j.LoggerFactory;
|
|
|
8
|
+import org.springframework.amqp.core.Message;
|
|
|
9
|
+import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;
|
|
|
10
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
11
|
+import org.springframework.stereotype.Component;
|
|
|
12
|
+
|
|
|
13
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
14
|
+import com.rabbitmq.client.Channel;
|
|
|
15
|
+import com.yoho.error.event.SearchEvent;
|
|
|
16
|
+import com.yoho.search.base.utils.ConvertUtils;
|
|
|
17
|
+import com.yoho.search.base.utils.EventReportEnum;
|
|
|
18
|
+import com.yoho.search.base.utils.ISearchConstants;
|
|
|
19
|
+import com.yoho.search.consumer.service.base.SuggestConversionCustomService;
|
|
|
20
|
+import com.yoho.search.core.es.utils.IgnoreSomeException;
|
|
|
21
|
+import com.yoho.search.dal.model.SuggestConversionCustom;
|
|
|
22
|
+
|
|
|
23
|
+@Component
|
|
|
24
|
+public class SuggestConversionCustomMqListener extends AbstractMqListener implements ChannelAwareMessageListener{
|
|
|
25
|
+
|
|
|
26
|
+ private static final Logger logger = LoggerFactory.getLogger(SuggestConversionCustomMqListener.class);
|
|
|
27
|
+
|
|
|
28
|
+ @Autowired
|
|
|
29
|
+ SuggestConversionCustomService suggestConversionCustomService;
|
|
|
30
|
+
|
|
|
31
|
+ @Override
|
|
|
32
|
+ public void onMessage(Message message, Channel channel) throws Exception {
|
|
|
33
|
+ try {
|
|
|
34
|
+ String messageStr = new String(message.getBody(), "UTF-8");
|
|
|
35
|
+ logger.info("[model=ProductAttributeMqListener] [message={}]", messageStr);
|
|
|
36
|
+ // 如果在重建索引等待
|
|
|
37
|
+ this.waitingRebuildingIndex();
|
|
|
38
|
+ JSONObject json = JSONObject.parseObject(messageStr);
|
|
|
39
|
+ String tableName = ISearchConstants.TABLE_NAME_TBL_SUGGEST_CONVERSION_CUSTOM;
|
|
|
40
|
+ String action = json.getString("action");
|
|
|
41
|
+ if (ISearchConstants.ACTION_DELETE.equals(action)) {
|
|
|
42
|
+ deleteData(json.getString("data"), tableName);
|
|
|
43
|
+ } else if (ISearchConstants.ACTION_UPDATE.equals(action)) {
|
|
|
44
|
+ updateData(json.getObject("data", Map.class), tableName);
|
|
|
45
|
+ } else {
|
|
|
46
|
+ updateData(json.getObject("data", Map.class), tableName);
|
|
|
47
|
+ }
|
|
|
48
|
+ } catch (Exception e) {
|
|
|
49
|
+ publisher.publishEvent(new SearchEvent(EventReportEnum.SUGGESTCONVERSIONCUSTOMMQLISTENER_ONMESSAGE.getEventName(), EventReportEnum.SUGGESTCONVERSIONCUSTOMMQLISTENER_ONMESSAGE.getFunctionName(),
|
|
|
50
|
+ EventReportEnum.SUGGESTCONVERSIONCUSTOMMQLISTENER_ONMESSAGE.getMoudleName(), "exception", IgnoreSomeException.filterSomeException(e), null));
|
|
|
51
|
+ Thread.sleep(1000);
|
|
|
52
|
+ throw e;
|
|
|
53
|
+ }
|
|
|
54
|
+ }
|
|
|
55
|
+
|
|
|
56
|
+ @SuppressWarnings({ "rawtypes", "unchecked" })
|
|
|
57
|
+ public void updateData(final Map data, final String tableName) throws Exception {
|
|
|
58
|
+ long begin = System.currentTimeMillis();
|
|
|
59
|
+ SuggestConversionCustom suggestConversionCustom = new SuggestConversionCustom();
|
|
|
60
|
+ suggestConversionCustom = (SuggestConversionCustom) ConvertUtils.toJavaBean(suggestConversionCustom, data);
|
|
|
61
|
+ if (suggestConversionCustom == null || suggestConversionCustom.getId() == null) {
|
|
|
62
|
+ return;
|
|
|
63
|
+ }
|
|
|
64
|
+ // 更新数据库
|
|
|
65
|
+ suggestConversionCustomService.saveOrUpdate(suggestConversionCustom);
|
|
|
66
|
+ logger.info("[func=updateData][step=success][tableName={}][id={}][cost={}ms]", tableName, suggestConversionCustom.getId(), (System.currentTimeMillis() - begin));
|
|
|
67
|
+ }
|
|
|
68
|
+
|
|
|
69
|
+ public void deleteData(final String id, final String tableName) throws Exception {
|
|
|
70
|
+ if (StringUtils.isBlank(id)) {
|
|
|
71
|
+ return;
|
|
|
72
|
+ }
|
|
|
73
|
+ long begin = System.currentTimeMillis();
|
|
|
74
|
+ // 删除数据
|
|
|
75
|
+ suggestConversionCustomService.delete(Integer.valueOf(id));
|
|
|
76
|
+ logger.info("[func=deleteData][step=success][tableName={}][id={}][cost={}ms]", tableName, id, (System.currentTimeMillis() - begin));
|
|
|
77
|
+ }
|
|
|
78
|
+} |