import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import poseidon.web.muses.db.userlevel.service.LevelHornorQueueService;
/**
* 处理更新经验的定时任务
*
* @author hulufeng
*
*/
@Component
public class LevelHornorQueueHandlerTask {
private static Logger log = LoggerFactory.getLogger(LevelHornorQueueHandlerTask.class);
@Autowired
LevelHornorQueueService levelQueueService;
// 一分钟执行一次
@Scheduled(cron = "0/60 * * * * *")
public void execute() {
log.debug("execute LevelHonorQueueHandlerTask");
try {
levelQueueService.handleElements();
} catch (Exception e) {
log.error("LevelHonorQueueHandlerTask", e);
}
}
}
队列:
package poseidon.web.muses.db.userlevel.serviceImpl;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import poseidon.web.muses.db.act.model.ActModel;
import poseidon.web.muses.db.common.lucene.model.QueueElement;
import poseidon.web.muses.db.global.GlobalUserLevel;
import poseidon.web.muses.db.tribe.model.TribePostModel;
import poseidon.web.muses.db.user.model.UserModel;
import poseidon.web.muses.db.userhonor.service.AchieveHonorRuleService;
import poseidon.web.muses.db.userlevel.model.UserExperienceHisModel;
import poseidon.web.muses.db.userlevel.service.ActRuleService;
import poseidon.web.muses.db.userlevel.service.LevelHornorQueueService;
import poseidon.web.muses.db.userlevel.service.LoginRuleService;
import poseidon.web.muses.db.userlevel.service.TribeRuleService;
import poseidon.web.muses.db.userlevel.service.UserLevelService;
/**
* 经验更新队列实现类
*
* @author hulufeng 20160727
*
*/
@Service
public class LevelHonorQueueServiceImpl implements LevelHornorQueueService {
private final static Logger log = LoggerFactory
.getLogger(LevelHonorQueueServiceImpl.class);
@Autowired
private UserLevelService userLevelService;
@Autowired
private ActRuleService actRuleService;
@Autowired
private LoginRuleService loginRuleService;
@Autowired
private AchieveHonorRuleService achieveHonorRuleService;
@Autowired
private TribeRuleService tribeRuleService;
/**
* 同步队列
*/
private static final LinkedBlockingQueue<QueueElement> linkQueue = new LinkedBlockingQueue<QueueElement>();
@Override
public void addElement(Map<String,Object> map) throws Exception {
QueueElement el = new QueueElement();
el.setElement(map);
linkQueue.offer(el);
}
/**
* 处理经验更新队列中的每一个元素
*/
@Override
public void handleElements() {
try {
while (!linkQueue.isEmpty()) {
QueueElement el = linkQueue.poll();
Map<String, Object> map = (Map<String, Object>) el.getElement();
Integer kid = Integer.parseInt(map.get("kid").toString());
Long aid = Long.parseLong((map.get("aid") == null ? 0 : map.get("aid")).toString() );
Long uid = Long.parseLong((map.get("uid") == null ? 0 : map.get("uid")).toString() );
Long operuid = Long.parseLong((map.get("operuid") == null ? 0 : map.get("operuid")).toString() );
UserExperienceHisModel userExperienceHisModel = new UserExperienceHisModel();
if(map.get("model") != null){//如果传了UserExperienceHisModel历史经验表
userExperienceHisModel = (UserExperienceHisModel) map.get("model");
}
ActModel actModel = new ActModel();
UserModel userModel = new UserModel();
log.debug("kid = " + kid);
switch (kid) {
case GlobalUserLevel.ActOperationKeys.ACT_FAVORITE://收藏活动
userLevelService.favoriteAct(aid, uid);
break;
case GlobalUserLevel.ActOperationKeys.ACT_LIKE://点赞活动
userLevelService.likeAct(aid, uid);
achieveHonorRuleService.likeAct(aid, uid);
break;
case GlobalUserLevel.ActOperationKeys.COMMENT_ACT://评论活动
Integer score1 = Integer.parseInt(map.get("score1").toString());
Integer score2 = Integer.parseInt(map.get("score2").toString());
userLevelService.commentAct(aid, uid, score1, score2);
achieveHonorRuleService.commentAct(aid, uid, score1, score2);
break;
case GlobalUserLevel.ActOperationKeys.ACT_BE_JOIN://确认参加活动
userLevelService.joinAct(aid, uid);
achieveHonorRuleService.joinAct(uid);
break;
case GlobalUserLevel.ActOperationKeys.USER_BE_WATCH://被关注
userLevelService.watchUser(uid,operuid);
break;
case GlobalUserLevel.UserSettingKeys.ADD_MEMBER://加入部落
tribeRuleService.activity1(map);//更新经验
achieveHonorRuleService.achieve3(map);//更新勋章
break;
case GlobalUserLevel.UserSettingKeys.PUBLISH_POST://发表话题
TribePostModel tribePostModel = (TribePostModel) map.get("tribePost");
userLevelService.publishPost(tribePostModel);//新增用户经验
achieveHonorRuleService.achieve4(tribePostModel, tribePostModel.getUid());//新增用户勋章
break;
case GlobalUserLevel.UserSettingKeys.LIKE_POST://点赞话题
Long topicId = Long.parseLong(map.get("topicId").toString());
userLevelService.likeTribePost(topicId,uid);//新增用户经验
achieveHonorRuleService.likeTribePost(topicId, uid);
break;
case GlobalUserLevel.UserSettingKeys.REPLY_POST://评论话题
Long topicId1 = Long.parseLong(map.get("topicId").toString());
userLevelService.replyTribePost(topicId1,uid);//新增用户经验
break;
case GlobalUserLevel.UserSettingKeys.FAVORITE_POST://收藏话题
Long topicId2 = Long.parseLong(map.get("topicId").toString());
userLevelService.favoriteTribePost(topicId2,uid);//新增用户经验
break;
case GlobalUserLevel.PublishActKeys.PUBLISH_FREEACT://发布免费活动
actModel = (ActModel) map.get("actModel");
actRuleService.activity1(actModel, operuid);//新增用户经验
achieveHonorRuleService.achieve1(actModel, operuid);
break;
case GlobalUserLevel.PublishActKeys.PUBLISH_FEEACT://发布收费活动
actModel = (ActModel) map.get("actModel");
actRuleService.activity2(actModel, operuid);//新增用户经验
achieveHonorRuleService.achieve1(actModel, operuid);
break;
case GlobalUserLevel.LoginTimesKeys.ONETIME_FIRSTDAY://登录
userModel = (UserModel) map.get("userModel");
loginRuleService.activity1(userModel);//新增用户经验
break;
case GlobalUserLevel.UserCertifyKeys.CERTIFY_VIP://认证VIP
case GlobalUserLevel.UserCertifyKeys.CERTIFY_CAR_OWNER://认证车主
case GlobalUserLevel.UserCertifyKeys.CERTIFY_CHIEF://认证酋长
case GlobalUserLevel.UserCertifyKeys.CERTIFY_SERVICE_BUSINESS://认证线路服务商
userLevelService.saveUserExperienceHis(userExperienceHisModel);//新增用户经验
achieveHonorRuleService.userCertify(uid,operuid);//勋章
break;
case GlobalUserLevel.UserSettingKeys.BIND_MOBILE://绑定手机
case GlobalUserLevel.UserSettingKeys.BIND_QQ://绑定QQ
case GlobalUserLevel.UserSettingKeys.BIND_EMAIL://绑定邮箱
case GlobalUserLevel.UserSettingKeys.BIND_SINA://绑定新浪
case GlobalUserLevel.UserSettingKeys.BIND_WX://绑定微信
case GlobalUserLevel.UserSettingKeys.UPLOAD_COVER://上传封面
case GlobalUserLevel.UserSettingKeys.UPLOAD_HEAD://上传头像
case GlobalUserLevel.UserSettingKeys.USER_SIGN://个新签名
case GlobalUserLevel.UserSettingKeys.AUDIT_TRIBE://部落审核
userLevelService.saveUserExperienceHis(userExperienceHisModel);//新增用户经验
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
使用:
try {
Map<String,Object> paramMap = new HashMap<String,Object>();
paramMap.put("kid", GlobalUserLevel.ActOperationKeys.ACT_FAVORITE);
paramMap.put("aid", id);
paramMap.put("uid", uid);
levelQueueService.addElement(paramMap);
} catch (Exception e) {
log.debug("收藏经验添加失败");
e.printStackTrace();
}