package com.netease.mobileMq.task;import java.util.Date;import java.util.List;import javax.jms.JMSException;import javax.jms.Message;import javax.jms.MessageListener;import javax.jms.TextMessage;import org.apache.commons.lang3.StringUtils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jms.core.JmsTemplate;import com.alibaba.fastjson.JSON;import com.netease.commonBean.FlushDeviceCacheBean;import com.netease.device.constant.EquipmentConstants;import com.netease.device.dao.EquipmentMapper;import com.netease.device.dao.FingerUserMapper;import com.netease.device.entity.EquipmentInfo;import com.netease.device.entity.FingerUserInfo;import com.netease.mobile.common.RedisUtil;/** * @author 作者 E-mail:ruanjianlxm@sina.com * @version 创建时间:2015年8月4日 下午4:44:39 * 类说明 */public class deviceCacheFlushTask implements MessageListener{ private static Logger logger = LoggerFactory.getLogger("equipmentErrorLog"); @Autowired FingerUserMapper fingerUserMapper; @Autowired EquipmentMapper equipmentMapper; @Autowired JmsTemplate jmsTemplate; public void onMessage(Message message){ TextMessage textMsg = (TextMessage) message; String receiveMsg =null; try { receiveMsg = textMsg.getText(); } catch (JMSException e1) { // TODO Auto-generated catch block e1.printStackTrace(); return ; } System.out.println("receiveMsg:"+receiveMsg); if (StringUtils.isBlank(receiveMsg)) { logger.error("deviceCacheFlushTask receiveMsg is null Time is " + new Date()); return ; } else { logger.info("deviceCacheFlushTask receiveMsg " + receiveMsg);//日志中记录每个刷新的数据 } FlushDeviceCacheBean flushBean = JSON.parseObject(receiveMsg, FlushDeviceCacheBean.class);//将传过来的刷新对象进行格式化。 String mainssn = flushBean.getMainssn(); String[] refIds = flushBean.getUserIds(); /*---------------先更新用户缓存----------*/ if (mainssn != null) {//有主账号就更新主账号信息 ListfingerUserInfos = null; try { fingerUserInfos = fingerUserMapper.getAllEqUserInfoByName(mainssn); } catch (Exception e) { // TODO: handle exception logger.error("EquipmentServiceImpl flushCache error", e); return ; } if (fingerUserInfos == null || fingerUserInfos.size() == 0) {// 如果有一个都没有 RedisUtil.delete(EquipmentConstants.EQUIPMENT_FINGER_USER_PRE + mainssn); } else { RedisUtil.set(EquipmentConstants.EQUIPMENT_FINGER_USER_PRE + mainssn, JSON.toJSONString(fingerUserInfos));// 新数据直接替换到缓存中 } } /*---------------再更新设备缓存----------*/ List equipmentInfos = null; if (refIds!=null&&refIds.length!=0) {//有ID就刷新ID信息 for (String refId : refIds) { try { equipmentInfos = equipmentMapper.getAllEquipmentInfoById(refId); } catch (Exception e) { // TODO: handle exception logger.error("EquipmentServiceImpl flushCache error", e); return ; } if (equipmentInfos == null || equipmentInfos.size() == 0) {// 如果有一个没有,说明查询数据出错失败了 RedisUtil.delete(EquipmentConstants.EQUIPMENT_EQUINFO_PRE + refId); } else { RedisUtil.set(EquipmentConstants.EQUIPMENT_EQUINFO_PRE + refId, JSON.toJSONString(equipmentInfos));// 新数据直接替换到缓存中 } } } }}