Spring HTTP Streaming (也就是对 SSE(Server-Sent Events) 得支持

参考: 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写到响应体中的。

说明

通常会被人们称为 SSE(Server-Sent Events),这个技术也是为了解决 后端给前端持续推送得问题,不过 WebSocket 是双通道得(可以后台发前台,也可以前台发后台), 但是 SSE 是单通道得(连接建立后,就只能后台发给前台)

实体:

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 接口,

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值