class SseEmitterHandler {
    private final SseEmitter emitter;
    private final Context ctx;
    private final AtomicBoolean stopped = new AtomicBoolean(false);
    private final ReentrantLock SYNC_LOCK = new ReentrantLock(true);

    public SseEmitterHandler(SseEmitter emitter) {
        this.ctx = Context.current();
        this.emitter = emitter;
    }

    /**
     * 开始
     */
    public void start() throws Throwable {
        ctx.contentType("text/event-stream;charset=utf-8");

        ctx.asyncStart(emitter.timeout, new AsyncListenerImpl(this));
        emitter.initialize(this);
    }


    /**
     * 发送事件内容
     *
     * @param event 事件数据
     */
    public void send(SseEvent event) throws IOException {
        if (event == null) {
            throw new IllegalArgumentException("SSE event cannot be null");
        }

        if (stopped.get()) {
            throw new IllegalStateException("SSE emitter was stopped");
        }

        SYNC_LOCK.lock();
        try {
            ctx.output(event.build());
            ctx.flush();
        } catch (IOException e) {
            stopOnError(e);

            throw e;
        } finally {
            SYNC_LOCK.unlock();
        }
    }

    /**
     * 完成（用于手动控制）
     */
    public void complete() throws IOException {
        stop();
    }

    /**
     * 因出错停目
     */
    protected void stopOnError(Throwable e) throws IOException {
        if (emitter.onError != null) {
            emitter.onError.accept(e);
        }

        stop();
    }

    /**
     * 因操时停止（异步操时）
     */
    protected void stopOnTimeout() throws IOException {
        if (emitter.onTimeout != null) {
            emitter.onTimeout.run();
        }

        stop();
    }

    /**
     * 停止
     */
    protected void stop() throws IOException {<FILL_FUNCTION_BODY>}
}

        if (stopped.get() == false) {
            stopped.set(true);

            SYNC_LOCK.lock();
            try {
                if (emitter.onCompletion != null) {
                    emitter.onCompletion.run();
                }

                ctx.asyncComplete();
            } finally {
                SYNC_LOCK.unlock();
            }
        }
    
<no_super_class>