- 浏览: 369259 次
- 性别:
- 来自: 杭州
文章分类
最新评论
-
surpassno:
很不错,学习了
一个对象占用多少字节? -
ysyzww:
你这么牛逼,你父母知道吗
maven使用技巧 -
妖人不要跑:
JDK中反序列化对象的过程(ObjectInputStream#readObject) -
lanhz:
谢楼主,构建成功了
Mac OSX 10.9 上build openjdk8和openjdk7 -
zqb666kkk:
通过了吗 ?????
淘宝北京专场java面试题(2011-12-31)
因为好多年没做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了
发表评论
-
springboot程序错误排查
2016-12-12 10:37 11492.0.0版本的springboot程序,在eclipse中 ... -
debug Java进程的debug参数
2016-08-25 21:13 1550前几天给java应用设置debug参数,发现有两个参数:- ... -
Java NIO Socket通信需要考虑的问题(持续更新)
2016-04-27 19:57 281. NIO 多个线程同时往 ... -
Fastjson反序列化泛型类型时候的一个问题
2015-01-21 15:34 32202import static org.junit.Asser ... -
HTTP 50X code实例
2014-10-31 19:24 01、500 Internal Server Err ... -
一次Direct buffer memory引发的OutOfMemoryError问题排查
2014-10-28 17:22 0留坑位 -
netty3.6.2中写数据的过程,以及写数据写不出去后怎么处理
2014-08-11 17:37 3086netty写数据的时候,会先放到一个缓存队 ... -
在用Netty 3.6.2发数据,发现内核缓冲区满的时候.....
2014-08-11 16:06 2142用nettys收发网络数据的时候,一般不会注 ... -
JDK中反序列化对象的过程(ObjectInputStream#readObject)
2014-06-10 20:10 4090此处,对象描述信息即ObjectStre ... -
Mac OSX 10.9 上build openjdk8和openjdk7
2014-03-29 18:29 14178先分享下自己build出来的fastdeb ... -
内存充足情况下应用一直CMS GC的问题分析
2014-03-26 22:39 0前几天日常上线发布后,收到大量的CMS GC ... -
查看java对象在内存中的布局
2014-03-20 22:39 12960接着上篇《一个对象占用多少字节?》中遇到的 ... -
一个对象占用多少字节?
2014-03-18 21:56 34837老早之前写过一篇博客,是关于一个Integ ... -
maven使用技巧
2014-03-18 15:34 38501、pom打jar包的时候设置MANIFEST.MF的ke ... -
cpu字长、操作系统字长和jvm中各数据类型占用的字节数关系
2014-03-16 02:05 4648cpu字长是指cpu同时参与运算的二进制位 ... -
比反射更高效的修改字段值的方法
2014-03-13 20:49 1315开发过程中,不少情况下都会遇到需要通过反射 ... -
cache line对内存访问的影响
2014-03-12 20:48 1315cache line对内存访问的影响很早就 ... -
Java Web应用Web层异步化应该考虑的问题
2014-01-25 17:45 5772之前做了一 ... -
jvisualvm jmx方式远程监控tomcat
2013-10-10 20:38 19361、如果用jmx方式监控,不需运行服务器上的jstatd进 ... -
一些数据切分、缓存、rpc框架、nosql方案资料
2013-10-07 23:48 14291、数据切分 ...
相关推荐
Example: Simple analog clock...199 Chapter 6: Working with strings203 Basics of strings.. 204 Creating strings... 205 The length property. 207 Working with characters in strings208 Comparing strings....
During its relatively short but spectacularly successful life, Microsoft® Active Server Pages (ASP) has grown from a simple scripting environment for creating dynamic Web pages into a powerful and...
Hence, it becomes possible for the attacker to issue a command to all the nodes, that target a single node (for example, all nodes in the botnet might be commanded by the attacker to send a TCP SYN ...
Developed with a version 2.0b of WSockets and D3.0.9.3.1b- Changed the POP.login to a function that return the number of new msgs.- Added the event OnRetrieveProgress on the SakPOP, and fixed the ...
For example, you can perform operations with a group of objects as well as with a single object in Object Manager, sort, group and filter the database objects within Object Browser, copy an object ...
graphdb.zip This demonstrates how to graph information from a database to a picturebox.<END><br>5 , ClassBuilder1.zip Class Builder - reads SQL Server or Access database and creates a class of ...
Creating a Simple ASP.NET Web Service 757 Testing the Web Service 759 Implementing a Windows Client 761 Calling the Service Asynchronously 765 Implementing an ASP.NET Client 768 ...
PEP 3333: Python Web Server Gateway Interface v1.0.1 Other Language Changes New, Improved, and Deprecated Modules email elementtree functools itertools collections threading datetime and time ...
18)..Fixed: Possible "Unit XYZ was compiled with a different version of ABC" when using packages 19)..Fixed: FastMM shared MM compatibility 20)..Fixed: Minor bugs in stack tracing (which usually ...
The syntax of the file is extremely simple. Whitespace and lines ; beginning with a semicolon are silently ignored (as you probably guessed). ; Section headers (e.g. [Foo]) are also silently ignored,...
4.11.1 A Non-obvious Benefit of Using Checkpoints . . . . . . . . . . . . 5 25 26 28 28 29 29 30 31 31 34 36 37 Stopping and Continuing . . . . . . . . . . . . . . . . . . 39 5.1 Breakpoints, ...
Instantiating a container .................................................................................... 24 Composing XML-based configuration metadata .......................................... ...
Instantiating a container .................................................................................... 24 Composing XML-based configuration metadata .......................................... ...