学习 asychronize



package com.xxx.fortuna.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.log4j.Logger;

/**
* 更新数据
*
* @author
*
*/
public abstract class UpdateDBQueueBase<T> {

private static Logger log = Logger.getLogger(UpdateDBQueueBase.class);

private CopyOnWriteArrayList<LinkedBlockingQueue<T>> updateQueueList = new CopyOnWriteArrayList<LinkedBlockingQueue<T>>();

private int threadNum = 1;

private AtomicInteger count = new AtomicInteger(0);

private class updateDBThread implements Runnable {

public LinkedBlockingQueue<T> queue;

public updateDBThread(LinkedBlockingQueue<T> queue) {
this.queue = queue;
log.info("添加线程 updateDBThread!");
}

@Override
public void run() {
while (true) {
try {
T update = queue.take();
update(update);
} catch (Exception e) {
log.error("updateDao erro:", e);
} finally {
if (!updateQueueList.contains(queue) && queue.size() == 0) {
log.info("删除线程 updateDBThread!");
break;
}
}
}
}
}

/**
* 按用户ID取模加入队列
*
* @param splitId
* @param update
*/
public void addUpdateQueue(long splitId, T update) {
initThread();
int index = Math.abs((int) (splitId % 100 % updateQueueList.size()));
updateQueueList.get(index).add(update);
}

/**
* 加入当前队列最少等待的队列
*
* @param update
*/
public void addUpdateQueueFromMin(T update) {
initThread();
int index = 0;
int min = 0;
for (int i = 0; i < updateQueueList.size(); i++) {
if (updateQueueList.get(i).size() < min) {
min = updateQueueList.get(i).size();
index = i;
}
}
updateQueueList.get(index).add(update);
}

/**
* 轮询加入处理队列
*
* @param update
*/
public void addUpdateQueue(T update){
initThread();
int index = count.addAndGet(1);
if(index>=updateQueueList.size()){
index=0;
count.set(0);
}
updateQueueList.get(index).add(update);
}

private void initThread() {
int num = updateQueueList.size();
try {
if (num < threadNum) {
for (int i = num; i < threadNum; i++) {
LinkedBlockingQueue<T> queue = new LinkedBlockingQueue<T>();
updateQueueList.add(queue);
Thread thread = new Thread(new updateDBThread(queue));
thread.setName(getThreadName() + "-" + i);
thread.start();
}
} else if (num > threadNum) {
List<LinkedBlockingQueue<T>> delList = new ArrayList<LinkedBlockingQueue<T>>();
for (int i = threadNum; i < num; i++) {
delList.add(updateQueueList.get(i));
}
for (LinkedBlockingQueue<T> delQueue : delList) {
updateQueueList.remove(delQueue);
}
}
} catch (Exception e) {
log.error("UpdateDBQueueBase initThread ", e);
}

}

/**
* 线程名
* @return
*/
public abstract String getThreadName();

/**
* 更新逻辑
*
* @param update
*/
public abstract void update(T update) throws Exception;

public int getThreadNum() {
return threadNum;
}

public void setThreadNum(int threadNum) {
this.threadNum = threadNum;
}

}







package com.xxx.fortuna.activity;

import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import com.happyelements.fortuna.dao.UpdateDBQueueBase;
import com.happyelements.fortuna.mongo.MongoManger;

public class BaiHeDao {

private static BaiHeDao INSTANCE = null;

public static BaiHeDao getInstance() {
if (null == INSTANCE) {
INSTANCE = new BaiHeDao();
}
return INSTANCE;
}

private BaiHeDao() {
userNumUpdate = new UpdateUserNum();
scoresUpdate = new UpdateScores();
}

private UpdateUserNum userNumUpdate;

public void addUserNumQueue(BaiHeTeamDO update) {
userNumUpdate.addUpdateQueue(update);
}

private class UpdateUserNum extends UpdateDBQueueBase<BaiHeTeamDO> {

public UpdateUserNum() {
setThreadNum(1);
}

@Override
public void update(BaiHeTeamDO update) throws Exception {
Query query = Query.query(Criteria.where("id").is(update.getId()));
Update numUpdate = new Update();
numUpdate.inc("userNum", update.getUserNum());
MongoManger.getInstance().update(query, numUpdate,
BaiHeTeamDO.class);
}

@Override
public String getThreadName() {
return "BaiHeDao UpdateUserNum";
}

}

private UpdateScores scoresUpdate;

public void addScoresQueue(BaiHeTeamDO update) {
scoresUpdate.addUpdateQueue(update);
}

private class UpdateScores extends UpdateDBQueueBase<BaiHeTeamDO> {

public UpdateScores() {
setThreadNum(1);
}

@Override
public void update(BaiHeTeamDO update) throws Exception {
Query query = Query.query(Criteria.where("id").is(update.getId()));
Update numUpdate = new Update();
numUpdate.inc("scores", update.getScores());
MongoManger.getInstance().update(query, numUpdate,
BaiHeTeamDO.class);
}

@Override
public String getThreadName() {
return "BaiHeDao UpdateUserNum";
}

}
}






package com.xxx.fortuna.logic;

import javapns.Push;
import org.apache.log4j.Logger;

import com.happyelements.fortuna.dao.UpdateDBQueueBase;
import com.happyelements.fortuna.model.NotificationApalePO;

/**
* 给苹果推送消息的管理器
*
* @author
*/
public class ApaleNotificationMgr {

private static ApaleNotificationMgr INSTANCE = null;

public static ApaleNotificationMgr getInstance() {
if (null == INSTANCE) {
INSTANCE = new ApaleNotificationMgr();
}
return INSTANCE;
}

private ApaleNotificationMgr() {
sendNotificationThread = new SendNotificationThread();
}

private SendNotificationThread sendNotificationThread;

private static Logger log = Logger.getLogger(ApaleNotificationMgr.class);

public void addPushQueue(NotificationApalePO notification) {
boolean openApaleNotification = Boolean
.parseBoolean(GameConfigLogic.INSTANCE
.getGameConfigValue("isApaleNotification"));
if (null == notification || notification.getUid() <= 0
|| !openApaleNotification) {
return;
}

sendNotificationThread.addUpdateQueue(notification.getUid(),
notification);

}

private class SendNotificationThread extends
UpdateDBQueueBase<NotificationApalePO> {

@Override
public String getThreadName() {
return "SendAppaleNotificationThread";
}

@Override
public void update(NotificationApalePO notification) throws Exception {
if (!notification.isSend()) {
log.info(notification.getUid()
+ " 因发送条件没达到无法发送 Apale Notification "
+ notification.getMsg());
return;
}

Push.alert(notification.getMsg(), notification.getpFileName(),
GameConfigLogic.INSTANCE
.getGameConfigValue("notification_password"), true,
notification.getToken());

log.info(notification.getUid() + " 发送 Apale Notification 成功 "
+ notification.getMsg() + " " + notification.getpFileName()
+ " " + notification.getToken());
}

}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值