线程池

来源:互联网 发布:applem2引擎完整源码 编辑:程序博客网 时间:2024/05/17 00:46



import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;




/**
 * 构建一个固定大小的线程池处理充值请求
 * 
 * @since 2015-06-17
 */
@Service("arActiveExecutor")
public class ARActiveExecutor implements InitializingBean {
@Autowired
private ARActiveService arActiveService;
@Value("${executor.size}")
    private int executorSize = 100;


private ExecutorService executorService;
@Override
public void afterPropertiesSet() throws Exception {
executorService = Executors.newFixedThreadPool(executorSize);

for(int i=0;i<executorSize;i++){
executorService.execute(arActiveService);
}
}

}




@Service
@Slf4j
public class ARActiveService implements Runnable {


    @Autowired
    private AllRoundActiveOrderService allRoundActiveOrderService;
    
    @Autowired
    private ChannelTaskService channelTaskService;


    @Autowired
    Map<String, ARChannel> arChannelServiceMap;


    @Autowired
    private ActiveQuotaCallback activeQuotaCallback;
    
    @Autowired
    private ChannelChooseService channelChooseService;


    private int successState = 200;
    private int failState = 500;


    @Override
    public void run() {
        while (true) {
            String txNo = "ACTIVE_AR_" + UUID.randomUUID();
            try {
            ActiveOrderAllRoundDTO activeOrderDTO = allRoundActiveOrderService.getActiveOrder(txNo);
            log.info("activeAllround get,txNo:{},result:{}",txNo,activeOrderDTO);
           
                if (activeOrderDTO == null) {
                    sleep();
                } else {
                    handle(txNo,activeOrderDTO);
                }
            } catch (Exception e) {
                log.error("active for,txNo:{}, detail:{}", txNo,ExceptionUtils.getStackTrace(e));
                
                sleep();
            }
        }
    }


private void sleep() {
try {
   Thread.sleep(10*1000);
} catch (InterruptedException ex) {
}
}
    }

0 0