package cn.reghao.bnt.web.ws;

import cn.reghao.jutil.jdk.event.message.Event;
import cn.reghao.jutil.jdk.event.message.EventMessage;
import cn.reghao.jutil.jdk.event.router.EventDispatcher;
import cn.reghao.jutil.jdk.exception.ExceptionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author reghao
 * @date 2023-02-23 09:47:55
 */
@Slf4j
@Service
public class EventCenter {
    private final EventDispatcher dispatcher;

    public EventCenter(EventDispatcher eventDispatcher) {
        this.dispatcher = eventDispatcher;
    }

    public void dispatch(EventMessage eventMessage) {
        try {
            long sendTime = eventMessage.getSendTime();
            Event event = eventMessage.getEvent();
            dispatcher.dispatch(event);
        } catch (Exception e) {
            String errMsg = e.getMessage();
            if (e instanceof NullPointerException) {
                errMsg = ExceptionUtil.stackTrace(e);
            }

            log.error("处理消息发生异常: {}", errMsg);
        }
    }
}
