/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights reserved. <a>http://www.crunii.com</a>
 */
package com.crunii.micro.platform.log.impl;

import com.crunii.micro.common.queue.FastQueue;
import com.crunii.micro.common.queue.impl.FastQueueImpl;
import com.crunii.micro.common.utils.CopyUtil;
import com.crunii.micro.domain.entitysql.main.TBusiLogS;
import com.crunii.micro.platform.log.BusiLogHandler;
import com.lmax.disruptor.dsl.ProducerType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.Map;

/**
 * @author 田平 create 2019年5月15日下午4:58:21
 */
@Component
public class BusiLogHandlerImpl implements BusiLogHandler {

	@Autowired
	private BusiLogFastQueueHandler busiLogFastQueueHandler;

	private FastQueue<TBusiLogS> fastQueue;

	@Override
	public void doBusiLog(TBusiLogS busiLog) {
		fastQueue.add(busiLog);
	}

	@Override
	public void doBusiLogByMap(Map<String, Object> map) {
		TBusiLogS busiLog = new TBusiLogS();
		CopyUtil.copyMapToBean(map, busiLog);
		fastQueue.add(busiLog);
	}

	@PostConstruct
	public void init() {
		fastQueue = new FastQueueImpl<>(1024 * 128, ProducerType.MULTI, busiLogFastQueueHandler);
		fastQueue.init();
	}

	@PreDestroy
	public void destroy() {
		if (fastQueue != null) {
			fastQueue.destroy();
		}
	}
}
