package com.kiisoo.ic.job.count;

import com.kiisoo.ic.employee.mapper.PrivilageCpUserDOMapper;
import com.lmax.disruptor.RingBuffer;

public class CountDTOMessageEventProducer {

    private final RingBuffer<CountDTOMessage> ringBuffer;

    private PrivilageCpUserDOMapper privilageCpUserDOMapper;


    public CountDTOMessageEventProducer(RingBuffer<CountDTOMessage> ringBuffer, PrivilageCpUserDOMapper privilageCpUserDOMapper) {
        this.ringBuffer = ringBuffer;
        this.privilageCpUserDOMapper =privilageCpUserDOMapper;
    }

    public void produceData(int index, String cpUserId) {
        // 获得下一个Event槽的下标
        long seq = ringBuffer.next();
        try {
            // 给Event填充数据
            //给这个区块放入 数据  如果此处不理解，想想RingBuffer的结构图
            ringBuffer.get(seq).setIndex(index);
            ringBuffer.get(seq).setCpUserId(cpUserId);
            ringBuffer.get(seq).setPrivilageCpUserDOMapper(privilageCpUserDOMapper);
        } finally {
            // 发布Event，激活观察者去消费， 将sequence传递给该消费者
            // 注意，最后的 ringBuffer.publish() 方法必须包含在 finally 中以确保必须得到调用；如果某个请求的 sequence 未被提交，将会堵塞后续的发布操作或者其它的 producer。
            ringBuffer.publish(seq);
        }
    }
}
