📄 simplehttpdemo.java
字号:
/* Copyright (c) 2003, The Regents of the University of California, through Lawrence Berkeley National Laboratory (subject to receipt of any required approvals 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.StageManager;import gov.lbl.dsd.sea.event.IllegalEventException;import org.apache.commons.httpclient.HttpMethod;import org.apache.commons.httpclient.methods.GetMethod;import EDU.oswego.cs.dl.util.concurrent.CountDown;/** * Demonstrates how to use the HTTP framework with a very basic example. In this * example there are two stages. The first stage handles the HTTP transport * requests and responses. It hands the responses to the second stage, which * simply prints them. * <p> * Example usage: java gov.lbl.dsd.seda.demo.SimpleHTTPDemo 2 http://www.google.com * * @author whoschek@lbl.gov * @author $Author: gegles $ * @version $Revision: 1.5 $, $Date: 2004/09/16 16:57:15 $ */public class SimpleHTTPDemo { public static void main(String[] args) { int k=0; int runs = 2; if (args.length > k) runs = Integer.parseInt(args[k++]); String url = "http://www.google.com"; //String url = "http://dsd.lbl.gov/firefish/usages/fire-grep-usage.txt"; //String url = "http://localhost:8080/firefish/HelloWorldServlet"; //String url = "http://localhost:8080/firefish/index.html"; if (args.length > k) url = args[k++]; final CountDown barrier = new CountDown(runs); // a barrier to later wait until all responses have arrived // an event handler that simply prints the HTTP response EventHandler handler = new EventHandler() { public void handle(Object object) { System.out.println("**************** RESPONSE *****************************"); if (object instanceof ByteArrayEvent) { ByteArrayEvent ev = (ByteArrayEvent) object; String str = new String(ev.getBytes()); System.out.println(str); barrier.release(); // signal that a response has arrived } else { throw new IllegalEventException(object, this.getStage()); } } }; StageManager manager = new StageManager(); final Stage s2 = manager.createStage(handler).start(); final Stage s1 = manager.createStage(new SimpleHTTPEventHandler(s2)).start(); long start = System.currentTimeMillis(); try { // s1.enqueue(new Integer(-1)); // try this to get IllegalEventExceptions for (int i = 0; i < runs; i++) { HttpMethod method = new GetMethod(url); s1.enqueue(method); } try { // wait until all HTTP responses have arrived [via // barrier.release()]; and only then shutdown barrier.acquire(); } catch (InterruptedException e) {} long end = System.currentTimeMillis(); System.out.println("total time (ms): " + (end - start)); System.out.println("time (ms) per request: " + ((end - start) / (runs * 1.0f))); } finally { // cleanly shut down all stages and threads System.out.println("now shutting down..."); manager.stopAll(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -