package com.backendsys.modules.common.listener;

import com.backendsys.modules.sse.entity.SseResponse;
import com.backendsys.modules.sse.entity.SseResponseEnum;
import com.backendsys.modules.sse.utils.SseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.stereotype.Component;

@Component
public class ShutdownListener implements ApplicationListener<ContextClosedEvent> {

    @Autowired
    private SseUtil sseUtil;

    @Override
    public void onApplicationEvent(ContextClosedEvent event) {
        System.out.println("(ShutdownListener) Application is shutting down...");
        // 发送 SSE 消息通知前端
        // sseUtil.sendToAll(new SseResponse(SseResponseEnum.DISCONNECT, "Application is shutting down.").toJsonStr());
        sseUtil.closeAllEmitters();
    }

}
