当前位置:首页 > 开发 > 系统架构 > 架构 > 正文

jetty实例

发表于: 2014-02-11   作者:blackproof   来源:转载   浏览次数:
摘要:   jetty实例 package com.jetty; import java.io.IOException; import java.util.Map; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concur

 

jetty实例

package com.jetty;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.continuation.Continuation;
import org.eclipse.jetty.continuation.ContinuationSupport;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.util.thread.QueuedThreadPool;

import com.annotation.HBean;
import com.annotation.annoBean.AnnotationBeanLoader;

public class JettyServer {
	
	private final ExecutorService es = Executors.newCachedThreadPool();
	
	public void start(String host,int port) throws Exception{
		Server server = new Server();//服务
		//TCP监听
		SelectChannelConnector appBeanConn = new SelectChannelConnector();
		appBeanConn.setUseDirectBuffers(false);
		
		appBeanConn.setHost(host);
		appBeanConn.setPort(port);
		appBeanConn.setThreadPool(new QueuedThreadPool(20));
		appBeanConn.setName("appBeans");
		
		server.addConnector(appBeanConn);
		//处理类
		server.setHandler(new HttpAppBeanHandler());
		server.start();
		server.join();
	}
	
	
	private class HttpAppBeanHandler extends AbstractHandler{

		private static final String CALLTYPE = "calltype";
		
		Map<String, Class<HBean>> map;
		
		public HttpAppBeanHandler() throws Exception{
			map = AnnotationBeanLoader.loadHandlerBean(); 
		}
		
		public void invokeBean(String id,HttpServletRequest request,HttpServletResponse response) throws InstantiationException, IllegalAccessException{
			HBean bean = map.get(id).newInstance();//获取bean
			bean.init(request,response);//初始化bean
			bean.response(request,response);//相应bean
		}
		
		@Override
		public void handle(String str, final Request baseRequest, final HttpServletRequest request,
				final HttpServletResponse response) throws IOException, ServletException {

//			String method = request.getMethod();
			final String callType = (String) request.getAttribute(CALLTYPE);
			
			final Continuation continuation = ContinuationSupport.getContinuation(request);
			if(continuation.isExpired()){
				response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,"http bean invocation timeout");
				Future<?> f = (Future<?>) request.getAttribute("KEY_INVOCATION_FUTURN");
				if(f != null)
					f.cancel(true);
			}else{
				continuation.setTimeout(1000*3);
				continuation.suspend(response);
				
				Future<?> f = es.submit(new Runnable() {
					@Override
					public void run() {
						try {
							invokeBean(callType, request, response);
						} catch (InstantiationException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (IllegalAccessException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						continuation.complete();
					}//run
				});//future
			}
			
		}
		
	}

}

 

server使用httphandler,在handler中根据请求id,分发到处理业务的实体bean接口上

/**
 * 处理实体类接口
 * @author y
 */
public interface HBean {
	/**
	 * 初始化bean
	 * @param request
	 * @param response
	 */
	void init(HttpServletRequest request,HttpServletResponse response);
	/**
	 * 相应
	 * @param request
	 * @param response
	 */
	void response(HttpServletRequest request,HttpServletResponse response);
}

 

实体bean用注释作为id区分,加载获取指定目录下的所有类,用注释“包名”-“id"作为实体bean的id

/**
 * 定义处理bean的ID注解
 * 包含ID前缀包,ID名称
 * ID前缀包默认值"com.dz"
 * @author y
 */
@Target(ElementType.TYPE)//放在哪
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface HBID {
	public String id();
	public String catalog() default "com.dz";
}

 

加载子类目录 http://blackproof.iteye.com/blog/2015036

jetty的continuation分析 http://blog.csdn.net/kobejayandy/article/details/11874353

 

 

 

 

jetty实例

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
Jetty6源码分析-启动器 / Jetty6 Source Code Analysis-Starter Jetty 采用纯Java编写,小巧,容易
Jetty源码学习 图源码:jetty.dia.tar.gz jetty启动 使用源码 源码 git查看 git检出地址: git://git
java -jar jetty-runner.jar test-clover-0.0.1-SNAPSHOT.war
1. 下载Jetty http://dist.codehaus.org/jetty/jetty-hightide-8.1.0/jetty-hightide-8.1.0.RC1.zip
在eclipse中使用jetty,网上找了找说是有2种方法:第一种是在eclipse中add jetty server adapter,
天生一对"Maven2+Jetty" -- Maven2创建并管理WebApp,并使用Maven Jetty Plugin在Eclipse中调试 Mav
Maven2代比1代改进很多,其中主要强调的是--它不仅仅是个依赖包管理器! 开始先要推荐一个专讲Maven2
jetty是一个设计很优秀的嵌入式的servlet引擎,精悍短小是其最大的特点;粗略的看了下源码,发现和t
http://www.jforum.net/confluence/display/integration/Overview HTTP ERROR: 404 NOT_FOUND Reque
http://www.jforum.net/confluence/display/integration/Overview HTTP ERROR: 404 NOT_FOUND Reque
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号