欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

A Simple Server3.0 Example

程序员文章站 2022-07-02 09:57:55
...

因为好多年没做web开发,所以也一直木有关注JEE新版本的特性了。虽然老早听说Servet3.0可以异步化了,但是一直米有写过例子来跑过。最近思考异步化问题偏向于jvm层面如何保存执行中的栈和恢复栈,对异步化整体缺乏整理的考虑。

现在想来,天天面对的系统主要是接受请求,处理,发起一堆远程调用,接受远程调用结果,处理,构造响应结果,输出,没有太多深层次的业务处理。细化下去就要考虑接受请求、发起远程调用、接受调用结果、构造响应结果这块了。先从接受请求这块开始,就先了解下Servlet的异步化吧。

昨天查了下Servlet3.0的资料,写了个例子,惊奇的发现,竟然不用配置web.xml。虽然从原理上考虑,Annotation这种方式完全可以完成web.xml要实现的功能,但是现在真是这样的时候,还是有点小惊讶的,当然这只是跟记忆中web应用结构的对比后产生的,而不是对其新的实现方式产生的---Spring也有强大的Annotation呢,虽然我的记忆还停留在当年配置文件上......

另外,之前大多用myEclipse开发web应用,所以一时间都不知道怎么写了。。。不能自动发布,没有jee包的导入....淡定中,自己写ant文件拷贝,拷贝到tomcat目录下的时候,拷成webapp的接口即可,jee包从tomcat下边拷!

从tomcat的lib目录下拷servlet-api.jar

写build.xml文件:

<?xml version="1.0" encoding="GB2312"?>
<!DOCTYPE project>
<project name="async" default="autoCopy" basedir=".">
	<property name="targetDir" value="J:\java_tools\apache-tomcat-7.0.28\webapps\async"/>
	<property name="autoCompileClassDir" value="bin"/>
	<property name="webappDir" value="web"/>
	
	<target name="autoCopy" depends="copyClass,copyWeb">
	</target>
	<target name="copyClass">
		<echo message="=====Copying Class Files====="/>  
		<copy todir="${targetDir}/WEB-INF/classes">
			<fileset dir="${autoCompileClassDir}">
				<include name="**/*.class"></include>
			</fileset>
		</copy>
	</target>
	<target name="copyWeb">
		<echo message="=====Copying Web Files====="/>
		<copy todir="${targetDir}">
			<fileset dir="${webappDir}">
				<include name="**/*.xml"></include>
				<include name="**/*.properties"></include>
				<include name="**/*.jsp"></include>
				<include name="**/*.html"></include> 
			</fileset>
		</copy> 
	</target> 
</project>

 拷到我tomcat/webapps目录下了。

Servlet定义:

package com.tmall.buy.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.AsyncContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(asyncSupported = true, name = "asyncServlet", urlPatterns = { "/asyncServlet" }, initParams = {
		@WebInitParam(name = "hello", value = "world"),
		@WebInitParam(name = "gun", value = "dan") })
public class AsyncServlet extends HttpServlet {
	private static final long serialVersionUID = 133332222222L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doAsync(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doAsync(req, resp);
	}
	
	private void doAsync(HttpServletRequest req,HttpServletResponse resp) throws IOException{
		resp.setContentType("text/html");
		System.out.println("req.isAsyncSupported = " + req.isAsyncSupported());
		final AsyncContext ctx = req.startAsync();
		new Thread(new Runnable(){
			@Override
			public void run() {
				PrintWriter out = null;
				try {
					out = ctx.getResponse().getWriter();
					Thread.sleep(1000);
					System.out.println("Async Thread~~");
					out.write("<br>Async Write ~~~<br>");
					out.flush();
				} catch (Exception e) {
					e.printStackTrace();
				}finally{
					ctx.complete();
				}
			}
			
		}).start();
	}	
}

 详细参考这里

另外蛋疼写了个Filter:

package com.tmall.buy.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

@WebFilter(asyncSupported = true, filterName = "commonFilter", urlPatterns = { "*" })
public class CommonFilter implements Filter {

	@Override
	public void destroy() {

	}

	@Override
	public void doFilter(ServletRequest arg0, ServletResponse arg1,
			FilterChain arg2) throws IOException, ServletException {
		System.out.println(arg0.getServerName());
		arg2.doFilter(arg0, arg1);
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {

	}
}

 然后,没了,整个应用就这么多东西了。

只是刚开始Filter的syncSupported属性没设置,于是乎异步就出错了,页面的异常堆栈:

java.lang.IllegalStateException: Not supported.
	org.apache.catalina.connector.Request.startAsync(Request.java:1664)
	org.apache.catalina.connector.Request.startAsync(Request.java:1657)
	org.apache.catalina.connector.RequestFacade.startAsync(RequestFacade.java:1023)
	com.tmall.buy.web.AsyncServlet.doAsync(AsyncServlet.java:35)
	com.tmall.buy.web.AsyncServlet.doGet(AsyncServlet.java:23)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
	com.tmall.buy.web.filter.CommonFilter.doFilter(CommonFilter.java:25)

在Filter的@WebFilter标注上加了asyncSupported=true之后,就ok了