|
|
package com.yoho.search.consumer.index.increment;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.rabbitmq.client.Channel;
|
|
|
import com.yoho.error.event.SearchEvent;
|
|
|
import com.yoho.search.base.utils.ConvertUtils;
|
|
|
import com.yoho.search.base.utils.EventReportEnum;
|
|
|
import com.yoho.search.base.utils.ISearchConstants;
|
|
|
import com.yoho.search.consumer.service.base.ScoreRuleService;
|
|
|
import com.yoho.search.consumer.service.logic.ScoreRuleLogicService;
|
|
|
import com.yoho.search.core.es.utils.IgnoreSomeException;
|
|
|
import com.yoho.search.dal.model.ScoreRule;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.amqp.core.Message;
|
|
|
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* Created by gemingdan on 2017/3/15.
|
|
|
*/
|
|
|
@Component
|
|
|
public class ScoreRuleMqListener extends AbstractMqListener implements ChannelAwareMessageListener {
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(ScoreRuleMqListener.class);
|
|
|
|
|
|
@Autowired
|
|
|
private ScoreRuleService scoreRuleService;
|
|
|
|
|
|
@Autowired
|
|
|
private ScoreRuleLogicService scoreRuleLogicService;
|
|
|
|
|
|
@Override
|
|
|
public void onMessage(Message message, Channel channel) throws Exception {
|
|
|
try {
|
|
|
String messageStr = new String(message.getBody(), "UTF-8");
|
|
|
logger.info("[model=ScoreRuleMqListener] [message={}]", messageStr);
|
|
|
JSONObject json = JSONObject.parseObject(messageStr);
|
|
|
String action = json.getString("action");
|
|
|
if (ISearchConstants.ACTION_DELETE.equals(action)) {
|
|
|
deleteData(json.getString("data"));
|
|
|
} else {
|
|
|
updateData(json.getObject("data", Map.class));
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
publisher.publishEvent(new SearchEvent(EventReportEnum.SUGGESTWORDCUSTOMMQLISTENER_ONMESSAGE.getEventName(), EventReportEnum.SUGGESTWORDCUSTOMMQLISTENER_ONMESSAGE.getFunctionName(),
|
|
|
EventReportEnum.SUGGESTWORDCUSTOMMQLISTENER_ONMESSAGE.getMoudleName(), "exception", IgnoreSomeException.filterSomeException(e), null));
|
|
|
Thread.sleep(1000);
|
|
|
throw e;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@SuppressWarnings({"rawtypes", "unchecked"})
|
|
|
public void updateData(final Map data) throws Exception {
|
|
|
long begin = System.currentTimeMillis();
|
|
|
ScoreRule scoreRule = new ScoreRule();
|
|
|
scoreRule = (ScoreRule) ConvertUtils.toJavaBean(scoreRule, data);
|
|
|
if (scoreRule == null || scoreRule.getId() == null) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
// 先推送到ZK
|
|
|
scoreRuleLogicService.tryUpdateFunctionScoreRule(scoreRule);
|
|
|
|
|
|
// 更新数据库
|
|
|
scoreRuleService.saveOrUpdate(scoreRule);
|
|
|
logger.info("[func=updateData][step=success][tableName={}][id={}][cost={}ms]", "score_rule", scoreRule.getId(), (System.currentTimeMillis() - begin));
|
|
|
}
|
|
|
|
|
|
public void deleteData(final String id) throws Exception {
|
|
|
if (StringUtils.isBlank(id)) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
long begin = System.currentTimeMillis();
|
|
|
ScoreRule sourceData = scoreRuleService.selectByPrimaryKey(Integer.valueOf(id));
|
|
|
if (sourceData != null) {
|
|
|
// 先推送到ZK
|
|
|
scoreRuleLogicService.tryRemoveFunctionScoreRule(sourceData.getRuleType());
|
|
|
|
|
|
// 删除数据
|
|
|
scoreRuleService.delete(Integer.valueOf(id));
|
|
|
logger.info("[func=deleteData][step=success][tableName={}][id={}][cost={}ms]", "score_rule", id, System.currentTimeMillis() - begin);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
...
|
...
|
|