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;
}
}