Servlet3.0异步请求
工程目录结构
MyServletContainerInitializer.class
public class MyServletContainerInitializer implements ServletContainerInitializer { public void onStartup(Set<Class<?>> set, ServletContext servletContext) throws ServletException {
}
}
HelloAsynServlet.class
@WebServlet(value = "/async", asyncSupported = true)
public class HelloAsynServlet extends HttpServlet {
@Override
protected void doGet(final HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1. 支持异步处理asyncSupported = true
//2. 开启异步模式
//3. 业务逻辑进行异步处理
System.out.println("主线程开始..........." + Thread.currentThread());
final AsyncContext startAsync = req.startAsync();
startAsync.start(new Runnable() {
public void run() {
try {
System.out.println("副线程开始..........." + Thread.currentThread());
say();
startAsync.complete();
//4. 获取到异步上下文
ServletResponse response = startAsync.getResponse();
response.getWriter().write("async Hello");
System.out.println("副线程结束..........." + Thread.currentThread());
} catch (Exception e) {
e.printStackTrace();
}
}
});
System.out.println("主线程结束..........." + Thread.currentThread());
}
private void say() throws Exception {
System.out.println(Thread.currentThread() + "processing........");
Thread.sleep(3000);
}
}
javax.servlet.ServletContainerInitializer
com.lun.servlet.MyServletContainerInitializer
pom.xml
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.lun</groupId>
<artifactId>servlet</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>nexus-aliyun</id>
<name>Nexus aliyun</name>
<layout>default</layout>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository>
<repository>
<id>jboss-repository</id>
<name>Jboss Repository for Maven</name>
<url>http://repository.jboss.org/maven2</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
<layout>default</layout>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.ServletContainerInitializer</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
以上是 Servlet3.0异步请求 的全部内容, 来源链接: utcz.com/z/517159.html