类 ResponseBodyEmitter

  • 直接已知子类:
    SseEmitter

    public class ResponseBodyEmitter
    extends Object
    A controller method return value type for asynchronous request processing where one or more objects are written to the response.

    While DeferredResult is used to produce a single result, a ResponseBodyEmitter can be used to send multiple objects where each object is written with a compatible HttpMessageConverter.

    Supported as a return type on its own as well as within a ResponseEntity.

     @RequestMapping(value="/stream", method=RequestMethod.GET)
     public ResponseBodyEmitter handle() {
               ResponseBodyEmitter emitter = new ResponseBodyEmitter();
               // Pass the emitter to another component...
               return emitter;
     }
    
     // in another thread
     emitter.send(foo1);
    
     // and again
     emitter.send(foo2);
    
     // and done
     emitter.complete();
     
    从以下版本开始:
    4.2
    作者:
    Rossen Stoyanchev, Juergen Hoeller
    • 构造器详细资料

      • ResponseBodyEmitter

        public ResponseBodyEmitter​(Long timeout)
        Create a ResponseBodyEmitter with a custom timeout value.

        By default not set in which case the default configured in the MVC Java Config or the MVC namespace is used, or if that's not set, then the timeout depends on the default of the underlying server.

        参数:
        timeout - the timeout value in milliseconds
    • 方法详细资料

      • extendResponse

        protected void extendResponse​(ServerHttpResponse outputMessage)
        Invoked after the response is updated with the status code and headers, if the ResponseBodyEmitter is wrapped in a ResponseEntity, but before the response is committed, i.e. before the response body has been written to.

        The default implementation is empty.

      • send

        public void send​(Object object)
                  throws IOException
        Write the given object to the response.

        If any exception occurs a dispatch is made back to the app server where Spring MVC will pass the exception through its exception handling mechanism.

        Note: if the send fails with an IOException, you do not need to call completeWithError(Throwable) in order to clean up. Instead the Servlet container creates a notification that results in a dispatch where Spring MVC invokes exception resolvers and completes processing.

        参数:
        object - the object to write
        抛出:
        IOException - raised when an I/O error occurs
        IllegalStateException - wraps any other errors
      • complete

        public void complete()
        Complete request processing by performing a dispatch into the servlet container, where Spring MVC is invoked once more, and completes the request processing lifecycle.

        Note: this method should be called by the application to complete request processing. It should not be used after container related events such as an error while sending.

      • completeWithError

        public void completeWithError​(Throwable ex)
        Complete request processing with an error.

        A dispatch is made into the app server where Spring MVC will pass the exception through its exception handling mechanism. Note however that at this stage of request processing, the response is committed and the response status can no longer be changed.

        Note: this method should be called by the application to complete request processing with an error. It should not be used after container related events such as an error while sending.

      • onTimeout

        public void onTimeout​(Runnable callback)
        Register code to invoke when the async request times out. This method is called from a container thread when an async request times out.
      • onError

        public void onError​(Consumer<Throwable> callback)
        Register code to invoke for an error during async request processing. This method is called from a container thread when an error occurred while processing an async request.
        从以下版本开始:
        5.0
      • onCompletion

        public void onCompletion​(Runnable callback)
        Register code to invoke when the async request completes. This method is called from a container thread when an async request completed for any reason including timeout and network error. This method is useful for detecting that a ResponseBodyEmitter instance is no longer usable.