Skip to content

Commit 20a884a

Browse files
committed
test SseEmitter
1 parent 2649edb commit 20a884a

File tree

3 files changed

+61
-7
lines changed

3 files changed

+61
-7
lines changed

README.md

+20-6
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
# spring-boot-demo
2+
23
![](https://img.shields.io/static/v1?label=java&message=1.8&color=blue)
34
![](https://img.shields.io/static/v1?label=spring-boot&message=2.4.5.RELEASE&color=blue)
45
![](https://img.shields.io/static/v1?label=spring-restdocs&message=2.0.4.RELEASE&color=blue)
56
![](https://img.shields.io/static/v1?label=junit&message=4.13.2&color=black)
67
![](https://img.shields.io/static/v1?label=hibernate-validator&message=6.1.7.Final&color=blue)
8+
79
* use spring-boot 2.x
810
* https://shields.io/
911
* [github source](https://github.com/netbuffer/spring-boot-demo)
1012
* [gitee source](https://gitee.com/netbuffer/spring-boot-demo)
1113
* /actuator/health
14+
* [spring-boot-2.4.5-reference](https://docs.spring.io/spring-boot/docs/2.4.5/reference/htmlsingle/)
1215
* [@ControllerAdvice](https://docs.spring.io/spring-framework/docs/5.3.6/reference/html/web.html#mvc-ann-controller-advice)
1316
* [Exceptions Process](https://docs.spring.io/spring-framework/docs/5.3.6/reference/html/web.html#mvc-ann-exceptionhandler)
1417
* [ResponseEntityExceptionHandler](https://docs.spring.io/spring-framework/docs/5.3.6/javadoc-api/org/springframework/web/servlet/mvc/method/annotation/ResponseEntityExceptionHandler.html)
1518
* [requestmapping](https://docs.spring.io/spring-framework/docs/5.3.6/reference/html/web.html#mvc-ann-requestmapping-registration)
19+
* [mvc-sse](https://docs.spring.io/spring-framework/docs/5.3.6/reference/html/web.html#mvc-ann-async-sse)
1620
* [hibernate_validator_reference](https://docs.jboss.org/hibernate/validator/6.2/reference/en-US/pdf/hibernate_validator_reference.pdf#validator-gettingstarted)
1721
* https://docs.docker.com/compose/compose-file/compose-file-v3/#build
1822
* https://hub.docker.com/_/openjdk
1923

2024
### server.servlet.session.timeout
25+
2126
```html
2227
ns for nanoseconds
2328
us for microseconds
@@ -29,14 +34,17 @@ d for days
2934
```
3035

3136
### mvn build test
32-
> `mvn -Dmvn-arg=your param -DskipTests clean package -P test`
33-
-Dparam,-Pyour profile
37+
38+
> `mvn -Dmvn-arg=your param -DskipTests clean package -P test`
39+
> -Dparam,-Pyour profile
3440
3541
### maven-help-plugin use
42+
3643
* `mvn help:system` Displays a list of the platform details like system properties and environment variables
3744
* `mvn help:help` Display help information on maven-help-plugin.Call mvn help:help -Ddetail=true -Dgoal=<goal-name> to display parameter details
3845

3946
### branch
47+
4048
* javamelody
4149
* spring-restdocs
4250
* templating-maven-plugin
@@ -46,27 +54,33 @@ d for days
4654
### docker镜像制作
4755

4856
#### 构建
57+
4958
1. 先通过maven打包到target目录下
50-
2. 执行构建指令:`cd target;docker build -f ../Dockerfile -t netbuffer/spring-boot-demo:1.0 ../` (`docker build -t netbuffer/spring-boot-demo:1.0 .`)
59+
2. 执行构建指令:`cd target;docker build -f ../Dockerfile -t netbuffer/spring-boot-demo:1.0 ../` (`docker build -t netbuffer/spring-boot-demo:1.0 .`)
5160

5261
#### 运行
62+
5363
* docker run --rm -it -p 9100:9100 netbuffer/spring-boot-demo:1.0
5464
* docker-compose up (使用docker-compose启动服务)
5565

5666
#### 发布到DockerHub
67+
5768
> 需要注册DockerHub身份,再docker login登录对应的Registry
69+
5870
1. docker images 找到镜像ID
5971
2. docker tag 镜像ID 你的DockerHub账户名/spring-boot-demo:1.0 打tag
6072
3. docker push 你的DockerHub账户名/spring-boot-demo:1.0 推送到远程
6173

6274
#### 远程镜像
63-
> 已发布到DockerHub,可以直接运行以下指令测试
64-
https://hub.docker.com/r/javawiki/spring-boot-demo
75+
76+
> 已发布到DockerHub,可以直接运行以下指令测试
77+
> https://hub.docker.com/r/javawiki/spring-boot-demo
6578
6679
* docker pull javawiki/spring-boot-demo:1.0 拉取远程镜像
6780
* docker run -it --rm javawiki/spring-boot-demo:1.0 运行远程镜像
6881

6982
### articles
83+
7084
* [spring-boot中统一修改http响应体内容的方法](https://www.toutiao.com/i7014375995448820231)
7185
* [spring-boot中异常的处理](https://www.toutiao.com/i7014369292791579148)
7286
* [关于spring-boot中logback日志配置引用spring环境变量的使用方式](https://www.toutiao.com/i7014368145578230304)
@@ -76,4 +90,4 @@ https://hub.docker.com/r/javawiki/spring-boot-demo
7690
* [获取SpringBoot工程运行时的操作系统进程ID方法](https://www.toutiao.com/i7015456367477522977)
7791
* [SpringBoot使用基于json格式的配置方法](https://www.toutiao.com/i7016209186543469069)
7892
* [怎么查看SpringBoot工程中每个过滤器的执行顺序](https://www.toutiao.com/i7016666337527382532)
79-
* [Spring中出现NoUniqueBeanDefinitionException的解决方法](https://www.toutiao.com/i7018897444583113247)
93+
* [Spring中出现NoUniqueBeanDefinitionException的解决方法](https://www.toutiao.com/i7018897444583113247)

help/http-requests.http

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,7 @@ GET http://localhost:9100/async/dotask?sleep=3
1717
GET http://localhost:9100/retry/test
1818

1919
### /param/defaultValue
20-
GET http://localhost:9100/param/defaultValue
20+
GET http://localhost:9100/param/defaultValue
21+
22+
### /sse/sendById
23+
GET http://localhost:9100/sse/sendById?id=uitest&message=hello
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package cn.netbuffer.springboot.demo.controller;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.springframework.http.MediaType;
5+
import org.springframework.web.bind.annotation.CrossOrigin;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.bind.annotation.RestController;
9+
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
10+
import java.io.IOException;
11+
import java.util.HashMap;
12+
import java.util.Map;
13+
14+
@Slf4j
15+
@RestController
16+
@RequestMapping("/sse")
17+
public class SSEController {
18+
19+
private Map<String, SseEmitter> sseEmitterMap = new HashMap<>();
20+
21+
//https://www.w3.org/TR/eventsource/
22+
@GetMapping(path = "/events", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
23+
@CrossOrigin
24+
public SseEmitter handle(String id) {
25+
log.debug("new SseEmitter for id={}", id);
26+
SseEmitter sseEmitter = new SseEmitter(30 * 1000L);
27+
sseEmitterMap.put(id, sseEmitter);
28+
return sseEmitter;
29+
}
30+
31+
@GetMapping("sendById")
32+
public void sendById(String id, String message) throws IOException {
33+
log.debug("send message with SseEmitter id={}", id);
34+
sseEmitterMap.get(id).send(message);
35+
}
36+
37+
}

0 commit comments

Comments
 (0)