001 /* 002 Copyright (c) 2003, The Regents of the University of California, through 003 Lawrence Berkeley National Laboratory (subject to receipt of any required 004 approvals from the U.S. Dept. of Energy). All rights reserved. 005 */ 006 package gov.lbl.dsd.sea.demo; 007 008 import gov.lbl.dsd.sea.EventHandler; 009 import gov.lbl.dsd.sea.Stage; 010 import gov.lbl.dsd.sea.StageManager; 011 import gov.lbl.dsd.sea.event.IllegalEventException; 012 013 import org.apache.commons.httpclient.HttpMethod; 014 import org.apache.commons.httpclient.methods.GetMethod; 015 016 import EDU.oswego.cs.dl.util.concurrent.CountDown; 017 018 /** 019 * Demonstrates how to use the HTTP framework with a very basic example. In this 020 * example there are two stages. The first stage handles the HTTP transport 021 * requests and responses. It hands the responses to the second stage, which 022 * simply prints them. 023 * <p> 024 * Example usage: java gov.lbl.dsd.seda.demo.SimpleHTTPDemo 2 http://www.google.com 025 * 026 * @author whoschek@lbl.gov 027 * @author $Author: gegles $ 028 * @version $Revision: 1.5 $, $Date: 2004/09/16 16:57:15 $ 029 */ 030 public class SimpleHTTPDemo { 031 032 public static void main(String[] args) { 033 int k=0; 034 035 int runs = 2; 036 if (args.length > k) runs = Integer.parseInt(args[k++]); 037 038 String url = "http://www.google.com"; 039 //String url = "http://dsd.lbl.gov/firefish/usages/fire-grep-usage.txt"; 040 //String url = "http://localhost:8080/firefish/HelloWorldServlet"; 041 //String url = "http://localhost:8080/firefish/index.html"; 042 if (args.length > k) url = args[k++]; 043 044 final CountDown barrier = new CountDown(runs); // a barrier to later wait until all responses have arrived 045 046 // an event handler that simply prints the HTTP response 047 EventHandler handler = new EventHandler() { 048 public void handle(Object object) { 049 System.out.println("**************** RESPONSE *****************************"); 050 if (object instanceof ByteArrayEvent) { 051 ByteArrayEvent ev = (ByteArrayEvent) object; 052 String str = new String(ev.getBytes()); 053 System.out.println(str); 054 barrier.release(); // signal that a response has arrived 055 } 056 else { 057 throw new IllegalEventException(object, this.getStage()); 058 } 059 } 060 }; 061 StageManager manager = new StageManager(); 062 final Stage s2 = manager.createStage(handler).start(); 063 064 final Stage s1 = manager.createStage(new SimpleHTTPEventHandler(s2)).start(); 065 066 long start = System.currentTimeMillis(); 067 try { 068 // s1.enqueue(new Integer(-1)); // try this to get IllegalEventExceptions 069 for (int i = 0; i < runs; i++) { 070 HttpMethod method = new GetMethod(url); 071 s1.enqueue(method); 072 } 073 074 try { 075 // wait until all HTTP responses have arrived [via 076 // barrier.release()]; and only then shutdown 077 barrier.acquire(); 078 } catch (InterruptedException e) {} 079 080 long end = System.currentTimeMillis(); 081 System.out.println("total time (ms): " + (end - start)); 082 System.out.println("time (ms) per request: " + ((end - start) / (runs * 1.0f))); 083 084 } finally { 085 // cleanly shut down all stages and threads 086 System.out.println("now shutting down..."); 087 manager.stopAll(); 088 } 089 } 090 }