thomescai http://blog.csdn.net/thomescai(转载请保留)
介绍:
AsyncWeb是一个高性能,非阻塞(non-blocking),可内嵌在应用程序中使用的Java HTTP引擎。它始终围绕支持异步请求处理而设计。AsyncWeb还能与Spring框架集成。
使用:
AsyncWeb的使用非常简单,只要实现一个接口。这也是他的方便之处。
public interface HttpService {
void handleRequest( HttpServiceContext context ) throws Exception;
void start();
void stop();
}
启动:
结合spring,启动服务只需要加载2种配置文件:AsyncWeb.xml和HttpServiceDefinitions文件夹下的所有服务的配置文件。AsyncWeb.xml 配置如下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="mina-transport" class="org.apache.asyncweb.server.transport.mina.MinaTransport"> <property name="port"> <value>9013</value> </property> <property name="ioThreads"> <value>4</value> </property> <property name="eventThreads"> <value>16</value> </property> <property name="ioHandler"> <bean class="org.apache.asyncweb.server.transport.mina.DefaultHttpIoHandler" /> </property> </bean> <!-- The service container --> <bean id="container" class="org.apache.asyncweb.server.BasicServiceContainer"> <property name="serviceFilters"> <list> <ref bean="httpServiceHandler" /> </list> </property> <property name="transports"> <list> <ref bean="mina-transport" /> </list> </property> <property name="sessionAccessor"> <bean class="org.apache.asyncweb.server.session.DefaultSessionAccessor"> <property name="sessionStore"> <bean class="org.apache.asyncweb.server.session.BasicSessionStore"> <constructor-arg type="long"> <value>900000</value> </constructor-arg> <property name="sessionListeners"> <list> <bean class="org.apache.asyncweb.server.session.LoggingSessionListener" /> </list> </property> </bean> </property> </bean> </property> <property name="maxKeepAlives" value="1024" /> </bean> <!-- The HTTP Service Resolver --> <bean id="httpServiceHandler" class="org.apache.asyncweb.server.HttpServiceHandler"> <property name="serviceResolver"> <bean class="org.apache.asyncweb.server.resolver.CompositeResolver"> <property name="resolvers"> <list> <bean class="org.apache.asyncweb.server.resolver.SimplePrefixResolver"> <property name="uriPrefix"> <value>/service/</value> </property> </bean> <bean class="org.apache.asyncweb.server.resolver.PassThruResolver" /> </list> </property> </bean> </property> </bean> <bean id="httpServiceAutoload" class="org.apache.asyncweb.spring.HttpServiceLoader"> <property name="handler"> <ref bean="httpServiceHandler" /> </property> </bean> </beans>
AsyncWeb.xml几个重要的类:
1.org.apache.asyncweb.spring.HttpServiceLoader
加载所有httpService服务的配置文件到HttpServiceHandler
2.org.apache.asyncweb.server.HttpServiceHandler
httpService服务的处理器,实现了HttpServiceFilter的两个方法,对request和response进行处理。
void handleRequest(NextFilter next, HttpServiceContext context)
void handleResponse(NextFilternext, HttpServiceContext context)
3.org.apache.asyncweb.server.resolver.CompositeResolver
对服务名的解析,比如:http://localhost:9013/actionService,actionService匹配到com.cai.server.ActionService类
4.org.apache.asyncweb.server.session.DefaultSessionAccessor
session的访问器,里面有HttpSessionKeyFactory 和 HttpSessionStore 分别生成和存储session
5.org.apache.asyncweb.server.transport.mina.MinaTransport
采用mina的OrderedThreadPoolExecutor的线程池来处理 request 和response,对于同一个session来的请求,它能够按照请求到达的时间顺序的执行。OrderedThreadPoolExecutor的详细分析请看参考资料。
6.org.apache.asyncweb.server.BasicServiceContainer
主容器,负责启动sessions,handlers,transports。
启动顺序图:
参考资料:
《浅谈JAVA ThreadPoolExecutor》:http://blog.csdn.net/historyasamirror/article/details/5961368
《Mina的线程池实现分析(1)》:http://yanxuxin.iteye.com/blog/592903
《Mina的线程池实现分析(2)》:http://yanxuxin.iteye.com/blog/592908