package com.engine.htsc.init.payment.impl;

import com.engine.htsc.payment.util.IpUtils;
import com.weaverboot.frame.ioc.anno.classAnno.WeaSysInitComponent;
import com.weaverboot.frame.ioc.anno.methodAnno.WeaSysInit;
import weaver.general.BaseBean;
import weaver.general.GCONST;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.htsc.payment.common.pdfprocess.PDFBizProcessorImpl;
import weaver.interfaces.htsc.payment.common.pdfprocess.PdfConsumer;
import weaver.interfaces.htsc.payment.common.pdfprocess.PdfProducer;

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Created on 2022/4/20.
 *
 * @author K1810012
 */
@WeaSysInitComponent("pdf同步生产者消费者线程")
public class PDFProcessorInitServcieImpl {
  private static BaseBean log = new BaseBean();

  @WeaSysInit(order = 1, description = "启动生产消费线程")
  public void init() {

    log.writeLog("===>>> pdf sync producer thread start....");
    log.writeLog("===>>> pdf sync consumer thread start....");

    try {

        Thread subThread = new Thread(new SubStart());
        subThread.start();
        log.writeLog("===>>> PDFProcessorInit 主线程结束");
    } catch (Exception e) {
      log.writeLog("===>>> pdf sync thread error:");
      log.writeLog("===>>> pdf sync thread error:" + e);
    }

  }

  private static boolean isMainNode(){
    String hostIP = new PropBean().getPropName("HostIP");
    log.writeLog("===>>> 配置的IP是："+hostIP);
    String nowIp = IpUtils.getHostIp();
    log.writeLog("===>>> 当前环境IP是："+nowIp);
    return hostIP.equals(nowIp);
  }
  /**
   * 子线程启动，join到方法上
   */
  static class SubStart implements Runnable {
    @Override
    public void run() {
      BaseBean log = new BaseBean();
      try {
        //五分钟休眠放在子线程中，防止影响正常启动时间
        //等待的目的是为了ecology充分加载完成，weaver.conn.ConnectionPool.createPools 初始化成功
        log.writeLog("PDFProcessorInit start to wait 5 MINUTES");
        TimeUnit.MINUTES.sleep(5);
        log.writeLog("PDFProcessorInit wait 5 MINUTES done");
        if (!isMainNode()){
          return;
        }
        log.writeLog("主节点执行 PDF 同步模型");
        PDFBizProcessorImpl pdfBizProcessor = new PDFBizProcessorImpl();
        LinkedBlockingDeque<Runnable> runnables = new LinkedBlockingDeque<>(10);
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
            10, 20, 5L, TimeUnit.SECONDS,
            new LinkedBlockingQueue<>(10),
            new ThreadPoolExecutor.CallerRunsPolicy());
        //2:1
        for (int i = 0; i < 5; i++) {
          try {
            runnables.put(new PdfConsumer(pdfBizProcessor, runnables));
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
        PdfProducer producer = new PdfProducer(pdfBizProcessor, runnables, threadPoolExecutor);
        new Thread(producer).start();

        log.writeLog("===>>> pdf sync start done...");
        log.writeLog("===>>> PDFProcessorInit 子线程结束");
      } catch (Exception e) {
        log.writeLog("===>>>pdf sync thread start error");
        log.writeLog("===>>>error: " + e);
      }

    }
  }


}