/**
 * 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.common.queue.impl;

import com.crunii.micro.common.queue.ObjectEvent;
import com.lmax.disruptor.EventTranslatorOneArg;

import lombok.extern.slf4j.Slf4j;

/**
 * @author 田平 create 2019年3月4日下午3:55:50
 */
@Slf4j
public abstract class FastQueueTranslatorOneArg<T> implements EventTranslatorOneArg<ObjectEvent<T>, T> {

	/**
	 * @see com.lmax.disruptor.EventTranslatorOneArg#translateTo(java.lang.Object, long, java.lang.Object)
	 */
	@Override
	public void translateTo(ObjectEvent<T> event, long sequence, T arg0) {
		try {
			translate(event, sequence, arg0);
		} catch (Exception e) {
			// 新建线程抛出异常避免队列阻塞
			new Thread(new Runnable() {
				@Override
				public void run() {
					onException(event, sequence, arg0, e);
				}
			}).start();
		}
	}

	public abstract void translate(ObjectEvent<T> event, long sequence, T arg0);

	public void onException(ObjectEvent<T> event, long sequence, T arg0, Exception e) {
		String val = arg0 == null ? null : arg0.toString();
		log.error("sequence:" + sequence + ",val:" + val + " error!", e);
	}
}
