Authored by gemingdan

SuggestConversionCustom Mq

  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 +}
@@ -28,4 +28,20 @@ public class SuggestConversionCustomService { @@ -28,4 +28,20 @@ public class SuggestConversionCustomService {
28 public SuggestConversionCustom selectBySource(String source){ 28 public SuggestConversionCustom selectBySource(String source){
29 return suggestConversionCustomMapper.selectBySource(source); 29 return suggestConversionCustomMapper.selectBySource(source);
30 } 30 }
  31 + public void saveOrUpdate(SuggestConversionCustom suggestConversionCustom) {
  32 + if(suggestConversionCustom==null||suggestConversionCustom.getId()==null){
  33 + return;
  34 + }
  35 + if(suggestConversionCustomMapper.selectByPrimaryKey(suggestConversionCustom.getId())==null){
  36 + //插入
  37 + suggestConversionCustomMapper.insert(suggestConversionCustom);
  38 + }else{
  39 + //更新
  40 + suggestConversionCustomMapper.updateByPrimaryKey(suggestConversionCustom);
  41 + }
  42 + }
  43 +
  44 + public void delete(Integer id) {
  45 + suggestConversionCustomMapper.deleteByPrimaryKey(id);
  46 + }
31 } 47 }
@@ -79,6 +79,7 @@ @@ -79,6 +79,7 @@
79 <rabbit:queue durable="true" exclusive="false" name="data_update_channel_productext" /> 79 <rabbit:queue durable="true" exclusive="false" name="data_update_channel_productext" />
80 <rabbit:queue durable="true" exclusive="false" name="data_update_channel_productattributepropertyvalues" /> 80 <rabbit:queue durable="true" exclusive="false" name="data_update_channel_productattributepropertyvalues" />
81 <rabbit:queue durable="true" exclusive="false" name="data_update_channel_productattribute" /> 81 <rabbit:queue durable="true" exclusive="false" name="data_update_channel_productattribute" />
  82 + <rabbit:queue durable="true" exclusive="false" name="data_update_channel_suggestconversioncustom" />
82 83
83 <rabbit:template exchange="${search.mq.exchange}" id="amqpTemplate" 84 <rabbit:template exchange="${search.mq.exchange}" id="amqpTemplate"
84 connection-factory="connectionFactory" message-converter="jsonMessageConverter" /> 85 connection-factory="connectionFactory" message-converter="jsonMessageConverter" />
@@ -160,6 +161,7 @@ @@ -160,6 +161,7 @@
160 <rabbit:listener queue-names="data_update_channel_productext" ref="productExtMqListener" /> 161 <rabbit:listener queue-names="data_update_channel_productext" ref="productExtMqListener" />
161 <rabbit:listener queue-names="data_update_channel_productattributepropertyvalues" ref="productAttributePropertyValuesMqListener" /> 162 <rabbit:listener queue-names="data_update_channel_productattributepropertyvalues" ref="productAttributePropertyValuesMqListener" />
162 <rabbit:listener queue-names="data_update_channel_productattribute" ref="productAttributeMqListener" /> 163 <rabbit:listener queue-names="data_update_channel_productattribute" ref="productAttributeMqListener" />
  164 + <rabbit:listener queue-names="data_update_channel_suggestconversioncustom" ref="suggestConversionCustomMqListener" />
163 165
164 </rabbit:listener-container> 166 </rabbit:listener-container>
165 </beans> 167 </beans>