一个简单的单节点内存队列

package hg.hjf.app.service.rule;

import hg.hjf.app.service.UserRuleService;
import hgtech.jfaccount.JfFlow;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 活动奖励精灵。接受流水并在后台计算
 * @author Administrator
 *
 */
@Component
public class CalDaemon {
	@Autowired
	JfRuleService jfRuleService;
	
	@Autowired
	UserRuleService userRuleService;
	
	public static Log log = LogFactory.getLog(CalDaemon.class);
	private  List<JfFlow> todoFlows= Collections.synchronizedList( new LinkedList <JfFlow>());
	private  List<JfFlow> doneFlows=  (new LinkedList <JfFlow>());
	
	//构造时候,启动一个计算线程。监视待积分队列并计算
	public CalDaemon(){
		 todoFlows= Collections.synchronizedList( new LinkedList <JfFlow>());
		 doneFlows=  (new LinkedList <JfFlow>());
		
		new Thread(){
			public void run() {
				{
					int i=0;
					while(true){
						try {
							Thread.sleep(1000);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}						
						i++;
						synchronized (todoFlows)
						{
							//log 
							if(i==10 )
							{	
								if( todoFlows.size()>0){
									log.warn("calDaemon 待积分大小:"+todoFlows.size());
								}
								i=0;
							}
							
							//计算并标记
							long currentTimeMillis = System.currentTimeMillis();
							for(JfFlow f:todoFlows){
								//到时间再计算
								if(currentTimeMillis >= f.toCalTimeAt){
									try {
										jfRuleService.jfRuleByFlow(f);
									} catch (Exception e) {
										log.error("=================流水号"+f.getFlowId().getS()+"的奖励积分计算失败,下面是错误日志:");
										log.error(e,e);
									}
									doneFlows.add(f);
								}
							}
	//					计算过的从队列移除
							if(doneFlows.size()>0){
								todoFlows.removeAll(doneFlows);
								log.warn("calDaemon 已计算大小"+doneFlows.size());
								doneFlows.clear();
							}
						}
						

					}
				}
			}
		}.start();
	}

	public List<JfFlow> getTodoFlows() {
		return todoFlows;
	}

	public void setTodoFlows(List<JfFlow> todoFlows) {
		this.todoFlows = todoFlows;
	}
	
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值