⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simplehttpeventhandler.java

📁 sea是一个基于seda模式的实现。这个设计模式将系统分为很多stage。每个stage分布不同的任务(基于线程池)。通过任务流的方式提高系统的效率。
💻 JAVA
字号:
/*Copyright (c) 2003, The Regents of the University of California, throughLawrence Berkeley National Laboratory (subject to receipt of any requiredapprovals from the U.S. Dept. of Energy).  All rights reserved.*/package gov.lbl.dsd.sea.demo;import gov.lbl.dsd.sea.EventHandler;import gov.lbl.dsd.sea.Stage;import gov.lbl.dsd.sea.event.ExceptionEvent;import gov.lbl.dsd.sea.event.IllegalEventException;import java.io.IOException;import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.HttpMethod;import org.apache.commons.httpclient.HttpRecoverableException;import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;//import org.apache.commons.httpclient.SimpleHttpConnectionManager;/** * Simple HTTP 1.1 handler with limited functionality; * Internally uses the raw commons-httpclient library. * * @author whoschek@lbl.gov * @author $Author: hoschek3 $ * @version $Revision: 1.1 $, $Date: 2004/05/29 19:24:57 $ */public class SimpleHTTPEventHandler extends EventHandler {	protected HttpClient client;	protected Stage sink;	private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(SimpleHTTPEventHandler.class);	/** Creates a handler that enqueues HTTP responses onto the given sink	 *	 * @param sink the sink to enqueue responses onto	 */	public SimpleHTTPEventHandler(Stage sink)  {		this(sink, createHttpClient());	}	public SimpleHTTPEventHandler(Stage sink, HttpClient client)  {		this.sink = sink;		this.client = client;	}	public void handle(Object object) {		if (object instanceof HttpMethod) {			HttpMethod method = (HttpMethod) object;			Object rsp = null;			try {				int statusCode = this.executeMethod(method);				byte[] body = method.getResponseBody();				rsp = new ByteArrayEvent(this.getStage(), body, new Integer(statusCode));			} catch (IOException exc) {				rsp = new ExceptionEvent(exc, object, this.getStage());			}			finally {				method.releaseConnection();			}			this.sink.enqueue(rsp);		}		else {			throw new IllegalEventException(object, this.getStage());			//this.sink.enqueue(new IllegalEventException(event, this.getStage()));		}	}	protected int executeMethod(HttpMethod method) throws IOException {		int attempts = 3; // We will retry up to 3 times.		int statusCode = -1;		while (statusCode == -1 && attempts-- > 0) {			try {				statusCode = this.client.executeMethod(method); // execute the method.			} catch (HttpRecoverableException e) {				log.warn("A recoverable HTTP exception occurred, retrying: ", e);				if (attempts <= 0) throw e; // rethrow			}		}		return statusCode;	}	protected static HttpClient createHttpClient() {		//SimpleHttpConnectionManager connectionMgr = new SimpleHttpConnectionManager();		MultiThreadedHttpConnectionManager connectionMgr = new MultiThreadedHttpConnectionManager();		connectionMgr.setConnectionStaleCheckingEnabled(false); // improves performance by a factor of 50!		return new HttpClient(connectionMgr);	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -