package com.zjty.autotest.mq;

import com.zjty.autotest.dao.AutoResultSetDao;
import com.zjty.autotest.service.TestReportService;
import org.springframework.util.StringUtils;

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

public class PushBlockQueue extends LinkedBlockingQueue<Object> {
    private AutoResultSetDao autoResultSetDao;
    private TestReportService testReportService;
        //多线程执行，采用线程池
        private static ExecutorService es = Executors.newFixedThreadPool(10);
        //状态标识位
        private boolean flag = false;

        public PushBlockQueue(AutoResultSetDao autoResultSetDao,TestReportService testReportService) {
           this.autoResultSetDao=autoResultSetDao;
            this.testReportService=testReportService;
        }

        /**
         * 队列监听启动
         */
        public void start() {
            if (!this.flag) {
                flag = true;
            } else {
                throw new IllegalArgumentException("队列已启动，不可重复启动！");
            }
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        while (flag) {
                            //从队列中取消息
//                            Object obj = take();
                            String take = QueueManager.take();
                            //线程池派出线程来消费取出的消息
                            System.out.println("取出消息:"+take);
                            if(!StringUtils.isEmpty(take)) {
                                es.execute(new PushBlockThread(autoResultSetDao, testReportService, take));
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }

        /**
         * 停止队列监听
         */
        public void stop(){
            this.flag=false;
        }

}
