sseEmitter complete when refresh the page in browser

Discussão em 'Angular' iniciado por aya fathy, Fevereiro 9, 2025.

  1. aya fathy

    aya fathy Guest

    i am implementing notification system using sse emitter, 1-the client call for subscribe APi and the server create new ssEmtter and return it to the client

    SseEmitter sseEmitter = new SseEmitter(24 * 60 * 60 * 1000l);

    emitterService.addEmitter(sseEmitter);

    log.info("subscribed");
    return sseEmitter;
    }


    2- in publish the server use this sseEmiter and send the message

    my question is when i refresh the page in browser then the server try to send message i got this exception how could i keep the connection of sse emitter live even i refresh the page in the browser

    java.lang.IllegalStateException: ResponseBodyEmitter has already completed
    at org.springframework.util.Assert.state(Assert.java:76)
    at org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter.send(ResponseBodyEmitter.java:196)
    at org.springframework.web.servlet.mvc.method.annotation.SseEmitter.send(SseEmitter.java:126)
    at com.efinance.service.entity.service.EmitterServiceImpl.lambda$2(EmitterServiceImpl.java:102)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
    at com.efinance.service.entity.service.EmitterServiceImpl.pushNotification(EmitterServiceImpl.java:95)
    at com.efinance.service.entity.service.EmitterServiceImpl$$FastClassBySpringCGLIB$$9e4e84c6.invoke(\<generated\>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:687)
    at com.efinance.service.entity.service.EmitterServiceImpl$$EnhancerBySpringCGLIB$$c2c5d230.pushNotification(\<generated\>)
    at com.efinance.service.entity.service.EntityPansUnderThresholdJob.notifyEntityPansUnderThreshold(EntityPansUnderThresholdJob.java:57)
    at jdk.internal.reflect.GeneratedMethodAccessor136.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:774)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749)
    at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:88)
    at com.efinance.service.entity.tenant.TenantAspect.lambda$0(TenantAspect.java:36)
    at java.base/java.util.LinkedHashMap.forEach(LinkedHashMap.java:684)
    at com.efinance.service.entity.tenant.TenantAspect.handelTenantNames(TenantAspect.java:29)
    at jdk.internal.reflect.GeneratedMethodAccessor138.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:644)
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:633)
    at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:175)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:95)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:691)
    at com.efinance.service.entity.service.EntityPansUnderThresholdJob$$EnhancerBySpringCGLIB$$e85299cc.notifyEntityPansUnderThreshold(\<generated\>)
    at jdk.internal.reflect.GeneratedMethodAccessor136.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84)
    at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
    at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
    at java.base/java.util.concurrent.FutureTask.runAndReset(FutureTask.java:305)
    at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:305)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:834)


    for client i am using angular

    console.log('call subscripe'); const eventSource = new EventSourcePolyfill(('http://localhost:8587/subscription'), { headers: { 'Authorization': 'Bearer ' + sessionStorage.getItem("token") }, heartbeatTimeout: 1000000 });
    eventSource.onopen = (e) => console.log("open");
    eventSource.onmessage = (e) => { console.log(e)}
    eventSource.onerror = (e) => {
    if (e.readyState == EventSource.CLOSED) {
    console.log("close");
    } else {
    console.log(e);
    }


    how could i keep the connection of sse emitter live even i refresh the page in the browser?

    Continue reading...

Compartilhe esta Página