Skip to content

Commit 056d51d

Browse files
committed
add async servlet for lesson 13
1 parent 4815b50 commit 056d51d

File tree

6 files changed

+191
-0
lines changed

6 files changed

+191
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
4+
### STS ###
5+
.apt_generated
6+
.classpath
7+
.factorypath
8+
.project
9+
.settings
10+
.springBeans
11+
12+
### IntelliJ IDEA ###
13+
.idea
14+
*.iws
15+
*.iml
16+
*.ipr
17+
18+
### NetBeans ###
19+
nbproject/private/
20+
build/
21+
nbbuild/
22+
dist/
23+
nbdist/
24+
.nb-gradle/
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.segmentfault</groupId>
7+
<artifactId>spring-boot-lesson-13-async</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>spring-boot-lesson-13-async</name>
12+
<description>Demo project for Spring Boot</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.5.6.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
</properties>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-web</artifactId>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-test</artifactId>
36+
<scope>test</scope>
37+
</dependency>
38+
</dependencies>
39+
40+
<build>
41+
<plugins>
42+
<plugin>
43+
<groupId>org.springframework.boot</groupId>
44+
<artifactId>spring-boot-maven-plugin</artifactId>
45+
</plugin>
46+
</plugins>
47+
</build>
48+
49+
50+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.segmentfault.springbootlesson13async;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.boot.web.servlet.ServletComponentScan;
6+
7+
@SpringBootApplication
8+
@ServletComponentScan(basePackages = "com.segmentfault.springbootlesson13async.servlet")
9+
public class SpringBootLesson13AsyncApplication {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(SpringBootLesson13AsyncApplication.class, args);
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.segmentfault.springbootlesson13async.servlet;
2+
3+
import javax.servlet.*;
4+
import javax.servlet.annotation.WebServlet;
5+
import javax.servlet.http.HttpServlet;
6+
import javax.servlet.http.HttpServletRequest;
7+
import javax.servlet.http.HttpServletResponse;
8+
import java.io.IOException;
9+
import java.io.PrintWriter;
10+
11+
/**
12+
* TODO
13+
*
14+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
15+
* @see
16+
* @since 2017.08.16
17+
*/
18+
@WebServlet(asyncSupported = true, urlPatterns = "/async")
19+
public class AsyncServlet extends HttpServlet {
20+
21+
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
22+
23+
AsyncContext asyncContext = request.startAsync();
24+
25+
response.setContentType("text/html;charset=UTF-8");
26+
27+
final ServletContext servletContext = request.getServletContext();
28+
29+
PrintWriter writer = response.getWriter();
30+
31+
writer.write(threadInfo(request));
32+
33+
asyncContext.addListener(new AsyncListener() {
34+
35+
@Override
36+
public void onComplete(AsyncEvent event) throws IOException {
37+
38+
HttpServletResponse response = (HttpServletResponse) asyncContext.getResponse();
39+
40+
PrintWriter writer = response.getWriter();
41+
42+
writer.write("onComplete : " + threadInfo((HttpServletRequest) event.getSuppliedRequest()));
43+
44+
}
45+
46+
@Override
47+
public void onTimeout(AsyncEvent event) throws IOException {
48+
49+
servletContext.log("onTimeout : " + threadInfo((HttpServletRequest) event.getSuppliedRequest()));
50+
51+
}
52+
53+
@Override
54+
public void onError(AsyncEvent event) throws IOException {
55+
56+
servletContext.log("onError : " + threadInfo((HttpServletRequest) event.getSuppliedRequest()));
57+
58+
}
59+
60+
@Override
61+
public void onStartAsync(AsyncEvent event) throws IOException {
62+
63+
servletContext.log("onStartAsync : " + threadInfo((HttpServletRequest) event.getSuppliedRequest()));
64+
65+
}
66+
});
67+
68+
asyncContext.start(new Runnable() {
69+
@Override
70+
public void run() {
71+
HttpServletResponse response = (HttpServletResponse) asyncContext.getResponse();
72+
try {
73+
response.getWriter().write(threadInfo(request));
74+
asyncContext.complete();
75+
} catch (IOException e) {
76+
e.printStackTrace();
77+
}
78+
}
79+
});
80+
81+
}
82+
83+
private static String threadInfo(HttpServletRequest request) {
84+
return "Current Thread [" + Thread.currentThread().getName() + "] on request URI[" + request.getRequestURI() + "] was executed! <br />";
85+
}
86+
87+
}

spring-boot/lesson-13/spring-boot-lesson-13-async/src/main/resources/application.properties

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.segmentfault.springbootlesson13async;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.boot.test.context.SpringBootTest;
6+
import org.springframework.test.context.junit4.SpringRunner;
7+
8+
@RunWith(SpringRunner.class)
9+
@SpringBootTest
10+
public class SpringBootLesson13AsyncApplicationTests {
11+
12+
@Test
13+
public void contextLoads() {
14+
}
15+
16+
}

0 commit comments

Comments
 (0)