package com.zjty.autotest.mq;

import com.zjty.autotest.service.AsyncService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
@Component
public class PushBlockQueue extends LinkedBlockingQueue<Object> {
    @Autowired
    private AsyncService asyncService;

    //状态标识位
    private boolean flag = false;
    @Async("asyncServiceExecutor")
    public void zx(){
        try {
            while (flag) {
                //从队列中取消息
                Object obj = take();
                //线程池派出线程来消费取出的消息
                asyncService.executeAsync((String) obj);
//                        es.execute(new PushBlockQueueHandler(obj));
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    /**
     * 队列监听启动
     */
    public void start() {
        if (!this.flag) {
            flag = true;
        } else {
            throw new IllegalArgumentException("队列已启动，不可重复启动！");
        }
        zx();
    }

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

}
