Spring HTTP Streaming

参考: https://www.jianshu.com/p/ecc6f5168aef

参考: https://www.logicbig.com/tutorials/spring-framework/spring-web-mvc/http_streaming.html

简介:

控制器可以使用DeferredResultCallable对象来异步地计算其返回值,这可以用于实现一些有用的技术,比如 long polling技术,让服务器可以尽可能快地向客户端推送事件。

如果你想在一个HTTP响应中同时推送多个事件,怎么办?这样的技术已经存在,与"Long Polling"相关,叫"HTTP Streaming"。

Spring MVC支持这项技术,你可以通过让方法返回一个ResponseBodyEmitter类型对象来实现,该对象可被用于发送多个对象。通常我们所使用的@ResponseBody只能返回一个对象,它是通过HttpMessageConverter写到响应体中的。

实体:

1. ResponseBodyEmitter

2. SseEmitter

例子:

@RequestMapping("/test")
    public ResponseBodyEmitter test() {
        ResponseBodyEmitter emitter = new ResponseBodyEmitter();
        ExecutorService service = Executors.newSingleThreadExecutor();
//        ExecutorService service = Executors.newFixedThreadPool(10);
        service.execute(()-> {
            for (int i = 0; i < 100 ; i++) {
                try {
                    emitter.send(i+"-", MediaType.ALL);
                    Thread.sleep(100L);
                } catch (IOException | InterruptedException e) {
                    e.printStackTrace();
                    emitter.completeWithError(e);
                    return;
                }
            }
            emitter.complete();
        });

        return emitter;
    }

2. SseEmitter 和对应的HTML5 中的Server-Sent Events 特性,互相对应,sse 通道才能实现 服务器端主动向客户端推送数据

参考: https://www.cnblogs.com/elonlee/p/3914704.html

例子:

package com.kzcm.controller;

import com.kzcm.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("index")
public class IndexController {


    @RequestMapping("testSse")
    public String testSse(ModelMap model) {
        String path = "index/ssedemo";
//        model.addAttribute("name", name);
        return path;
    }
}

跳转到 index/ssedemo 目录下

如图示,EventSource 访问路径为 /async/testSse

package com.kzcm.controller;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.async.DeferredResult;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.io.IOException;
import java.time.LocalTime;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Controller
@RequestMapping("/async")
public class AsyncController {


    

    @RequestMapping("/testSse")
    public SseEmitter testSse() {
        SseEmitter emitter = new SseEmitter();
        ExecutorService service = Executors.newSingleThreadExecutor();
//        ExecutorService service = Executors.newFixedThreadPool(10);
        service.execute(()-> {
            for (int i = 0; i < 5 ; i++) {
                try {
                    emitter.send(LocalTime.now().toString(), MediaType.ALL);
                    Thread.sleep(10);
                } catch (IOException | InterruptedException e) {
                    e.printStackTrace();
                    emitter.completeWithError(e);
                    return;
                }
            }
            emitter.complete();
        });

        return emitter;
    }
}

最后结果就像轮询一样,隔一段时间访问一下 event 接口,

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章