geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pra...@apache.org
Subject svn commit: r579443 - in /geronimo/samples/trunk/samples/async-http: http-local-app/src/main/java/org/apache/geronimo/ http-local-app/src/main/webapp/WEB-INF/ http-remote-app/src/main/java/org/apache/geronimo/
Date Wed, 26 Sep 2007 03:24:11 GMT
Author: prasad
Date: Tue Sep 25 20:24:10 2007
New Revision: 579443

URL: http://svn.apache.org/viewvc?rev=579443&view=rev
Log:
* changes to accomodate redesigned AHC

Modified:
    geronimo/samples/trunk/samples/async-http/http-local-app/src/main/java/org/apache/geronimo/AsyncServlet.java
    geronimo/samples/trunk/samples/async-http/http-local-app/src/main/java/org/apache/geronimo/BlockingServlet.java
    geronimo/samples/trunk/samples/async-http/http-local-app/src/main/java/org/apache/geronimo/Callback.java
    geronimo/samples/trunk/samples/async-http/http-local-app/src/main/webapp/WEB-INF/web.xml
    geronimo/samples/trunk/samples/async-http/http-remote-app/src/main/java/org/apache/geronimo/PageServlet.java

Modified: geronimo/samples/trunk/samples/async-http/http-local-app/src/main/java/org/apache/geronimo/AsyncServlet.java
URL: http://svn.apache.org/viewvc/geronimo/samples/trunk/samples/async-http/http-local-app/src/main/java/org/apache/geronimo/AsyncServlet.java?rev=579443&r1=579442&r2=579443&view=diff
==============================================================================
--- geronimo/samples/trunk/samples/async-http/http-local-app/src/main/java/org/apache/geronimo/AsyncServlet.java
(original)
+++ geronimo/samples/trunk/samples/async-http/http-local-app/src/main/java/org/apache/geronimo/AsyncServlet.java
Tue Sep 25 20:24:10 2007
@@ -2,7 +2,10 @@
 
 import java.io.IOException;
 import java.net.URL;
+import java.util.Date;
 import java.util.Random;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
 
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
@@ -22,14 +25,23 @@
  * 
  */
 public class AsyncServlet extends HttpServlet implements CometProcessor {
+	@Override
+	public void destroy() {
+		// TODO Auto-generated method stub
+		super.destroy();
+		ahc.destroyAll();
+	}
+
 	static final long serialVersionUID = 1L;
-	
+
 	private String remoteUrl;
-	
+	private static AsyncHttpClient ahc;
+
 	public void init() throws ServletException {
-        getServletContext().log("getinit init");
-        // Get the value of an initialization parameter
-        remoteUrl = getServletConfig().getInitParameter("remoteUrl");
+		remoteUrl = getServletConfig().getInitParameter("remoteUrl");
+
+		ExecutorService threadPool = Executors.newFixedThreadPool(8);
+    ahc = new AsyncHttpClient(threadPool);
 	}
 
 	/**
@@ -49,50 +61,62 @@
 				+ event.getEventType().toString());
 
 		if (event.getEventType() == CometEvent.EventType.BEGIN) {
-			Callback callback = new Callback(event);
+			event.getHttpServletRequest().setAttribute(
+					"org.apache.tomcat.comet.timeout", new Integer(100 * 1000));
+			event.setTimeout(100 * 1000);
 
-			if (request.getParameter("remoteUrl") != null){
-				remoteUrl = request.getParameter("remoteUrl");
+			if (request.getParameter("remoteUrl") != null) {
+				remoteUrl = request.getParameter("remoteUrl");        
 			}
-			
+
 			String remoteUri = request.getParameter("remoteUri");
-			if ( remoteUri == null){
-				remoteUri =  "/remoteApp/page";
+			if (remoteUri == null) {
+				remoteUri = "/remoteApp/page";
 			}
-			HttpRequestMessage msgRequest = new HttpRequestMessage(remoteUri);
 			
-			// If the file being served has other URIs in it, like say images, 
+			URL url_connect = new URL(remoteUrl + remoteUri);			
+			Callback callback = new Callback(event.getHttpServletResponse(), cometId);
+			//Callback callback = new Callback(event, cometId);
+			HttpRequestMessage msgRequest = new HttpRequestMessage(url_connect, callback);
+
+			// If the file being served has other URIs in it, like say images,
 			// subsequent calls are made to the remote app to retrieve those.
 			// We don't want to set new delay times and status codes then.
 			if (remoteUri.endsWith("page")) {
 				Integer[] params = generateRandomParams();
 				msgRequest.setParameter("sessionId", cometId);
-				msgRequest.setParameter("delay", params[0].toString());				
-   			msgRequest.setParameter("code", params[1].toString());
-								
-        System.out.println("sessionId:" + cometId + "; delay:"
+				msgRequest.setParameter("delay", params[0].toString());
+				msgRequest.setParameter("code", params[1].toString());
+				msgRequest.setTimeOut(1500);
+
+				System.out.println("sessionId:" + cometId + "; delay:"
 						+ params[0] + "; code:" + params[1]);
 			}
-			else {
+      else {
 				System.out.println("msg request is " + msgRequest.getPath());
 			}
-			
-			msgRequest.setRequestMethod(HttpRequestMessage.REQUEST_GET);
 
-			URL url_connect = new URL(remoteUrl);
-
-			AsyncHttpClient ahc = new AsyncHttpClient(url_connect, callback, 2400, 1000);
+			msgRequest.setRequestMethod(HttpRequestMessage.REQUEST_GET);			
+			
 			try {
-				ahc.connect();
-				ahc.sendRequest(msgRequest);				
+				callback.setTime(new Date().getTime());
+				ahc.sendRequest(msgRequest);
 			} catch (Exception e) {
 				// TODO Auto-generated catch block
 				e.printStackTrace();
 			}
 		}
-		System.out.println("Event processed");
+    else if (event.getEventType() == CometEvent.EventType.ERROR) {
+			event.close();
+		}
+    else if (event.getEventType() == CometEvent.EventType.END) {
+			event.close();
+		}
 	}
 
+	/**
+	 * @return
+	 */
 	protected Integer[] generateRandomParams() {
 		Integer[] params = new Integer[2];
 		final int[] codes = new int[] { 200, 302, 403, 404, 500 };
@@ -100,7 +124,7 @@
 		Random generator = new Random();
 
 		// generate a random delay between 10ms and 250 ms
-		int delay = generator.nextInt(2500) + 10;
+		int delay = generator.nextInt(2000) + 10;
 		params[0] = new Integer(delay);
 
 		// generate randomly one of http status codes.

Modified: geronimo/samples/trunk/samples/async-http/http-local-app/src/main/java/org/apache/geronimo/BlockingServlet.java
URL: http://svn.apache.org/viewvc/geronimo/samples/trunk/samples/async-http/http-local-app/src/main/java/org/apache/geronimo/BlockingServlet.java?rev=579443&r1=579442&r2=579443&view=diff
==============================================================================
--- geronimo/samples/trunk/samples/async-http/http-local-app/src/main/java/org/apache/geronimo/BlockingServlet.java
(original)
+++ geronimo/samples/trunk/samples/async-http/http-local-app/src/main/java/org/apache/geronimo/BlockingServlet.java
Tue Sep 25 20:24:10 2007
@@ -90,7 +90,7 @@
 		Random generator = new Random();
 
 		// generate a random delay between 10ms and 250 ms
-		int delay = generator.nextInt(2500) + 10;
+		int delay = generator.nextInt(2000) + 10;
 		params[0] = new Integer(delay);
 
 		// generate randomly one of http status codes.

Modified: geronimo/samples/trunk/samples/async-http/http-local-app/src/main/java/org/apache/geronimo/Callback.java
URL: http://svn.apache.org/viewvc/geronimo/samples/trunk/samples/async-http/http-local-app/src/main/java/org/apache/geronimo/Callback.java?rev=579443&r1=579442&r2=579443&view=diff
==============================================================================
--- geronimo/samples/trunk/samples/async-http/http-local-app/src/main/java/org/apache/geronimo/Callback.java
(original)
+++ geronimo/samples/trunk/samples/async-http/http-local-app/src/main/java/org/apache/geronimo/Callback.java
Tue Sep 25 20:24:10 2007
@@ -12,57 +12,74 @@
 
 import org.apache.ahc.AsyncHttpClientCallback;
 import org.apache.ahc.codec.HttpResponseMessage;
-import org.apache.catalina.CometEvent;
 
 public class Callback implements AsyncHttpClientCallback {
 	private boolean timeout = false;
 	private boolean closed = false;
 	private boolean exception = false;
 	private Throwable throwable = null;
-	private CometEvent event = null;
+  private HttpServletResponse response;
 	private HttpResponseMessage message;
+	private long time;
+	private String sessionId;
 
-	public Callback(CometEvent event) {
-		this.event = event;
+	public Callback(HttpServletResponse response, String sessionId) {
+		this.response = response;
+		this.sessionId = sessionId;
 		clear();
 	}
-
-	public void onResponse(HttpResponseMessage message) {
-		this.message = message;
-
-		HttpServletResponse response = event.getHttpServletResponse();
-		HttpResponseMessage msg = getMessage();
-		response.setStatus(msg.getStatusCode());
-		response.setContentType(msg.getContentType());
-		response.setContentLength(msg.getContentLength());
-		OutputStream out = null;
-		try {
-			out = response.getOutputStream();
-			out.write(msg.getContent());
-			out.flush();
-		} catch (IOException e) {
-			// TODO Auto-generated catch block
-			e.printStackTrace();
+	
+	/**
+	 * @param time
+	 */
+	public void setTime(long time){
+		this.time = time;
+	}
+	
+	
+	/* (non-Javadoc)
+	 * @see org.apache.ahc.AsyncHttpClientCallback#onResponse(org.apache.ahc.codec.HttpResponseMessage)
+	 */
+	public void onResponse(HttpResponseMessage msg) {		
+    System.out.println("  onResponse()" + " - Status:" + msg.getStatusCode() + " - Length:"
+ msg.getContentLength() + " " + sessionId);
+
+    if (timeout) {
+        System.out.println("  Already responded to timeout " + sessionId);
+        return;
+    }
+    
+    response.setStatus(msg.getStatusCode());
+    response.setContentType(msg.getContentType());
+		if (msg.getContentLength() > 0) {
+			response.setContentLength(msg.getContentLength());
+			OutputStream out = null;
+			try {
+				out = response.getOutputStream();
+        out.write(msg.getContent());
+				out.flush();
+				out.close();
+			} catch (IOException e) {
+				// TODO Auto-generated catch block
+				e.printStackTrace();
+			}
 		}
-
-		closeEvent();
+		System.out.println("  Responded to " + sessionId + " in " + (new Date().getTime() - this.time));
 	}
 
 	public void onException(Throwable cause) {
 		throwable = cause;
 		exception = true;
-		// closeEvent();
+		System.out.println("  Exception by " + sessionId + " in " + (new Date().getTime() - this.time));
	
+		cause.printStackTrace();		
 	}
 
-	public void onClosed() {		
-		closed = true;
-		closeEvent();
+	public void onClosed() {
+      closed = true;
 	}
 
-	public void onTimeout() {		
+	public void onTimeout() {
 		timeout = true;
-		HttpServletResponse response = event.getHttpServletResponse();
-		try {
+    try {
 			response.setContentType("text/plain");
 			response.setStatus(HttpServletResponse.SC_REQUEST_TIMEOUT);
 			PrintWriter out = response.getWriter();
@@ -73,17 +90,9 @@
 			// TODO Auto-generated catch block
 			e.printStackTrace();
 		}
-		closeEvent();
+		System.out.println("  Timed out " + sessionId + " in " + (new Date().getTime() - this.time));
 	}
 
-	protected void closeEvent() {
-		try {
-			event.close();
-		} catch (IOException e) {
-			// TODO Auto-generated catch block
-			e.printStackTrace();
-		}
-	}
 
 	public Throwable getThrowable() {
 		return throwable;

Modified: geronimo/samples/trunk/samples/async-http/http-local-app/src/main/webapp/WEB-INF/web.xml
URL: http://svn.apache.org/viewvc/geronimo/samples/trunk/samples/async-http/http-local-app/src/main/webapp/WEB-INF/web.xml?rev=579443&r1=579442&r2=579443&view=diff
==============================================================================
--- geronimo/samples/trunk/samples/async-http/http-local-app/src/main/webapp/WEB-INF/web.xml
(original)
+++ geronimo/samples/trunk/samples/async-http/http-local-app/src/main/webapp/WEB-INF/web.xml
Tue Sep 25 20:24:10 2007
@@ -13,7 +13,7 @@
 		<servlet-class>org.apache.geronimo.AsyncServlet</servlet-class>
         <init-param>
 			<param-name>remoteUrl</param-name>
-			<param-value>http://puffyshirt:8080</param-value>
+			<param-value>http://festivus:8080</param-value>
   	    </init-param>      
 	</servlet>
 
@@ -29,7 +29,7 @@
 		<servlet-class>org.apache.geronimo.BlockingServlet</servlet-class>
         <init-param>
 			<param-name>remoteUrl</param-name>
-			<param-value>http://puffyshirt:8080</param-value>
+			<param-value>http://festivus:8080</param-value>
   	    </init-param>      
 	</servlet>
 

Modified: geronimo/samples/trunk/samples/async-http/http-remote-app/src/main/java/org/apache/geronimo/PageServlet.java
URL: http://svn.apache.org/viewvc/geronimo/samples/trunk/samples/async-http/http-remote-app/src/main/java/org/apache/geronimo/PageServlet.java?rev=579443&r1=579442&r2=579443&view=diff
==============================================================================
--- geronimo/samples/trunk/samples/async-http/http-remote-app/src/main/java/org/apache/geronimo/PageServlet.java
(original)
+++ geronimo/samples/trunk/samples/async-http/http-remote-app/src/main/java/org/apache/geronimo/PageServlet.java
Tue Sep 25 20:24:10 2007
@@ -18,8 +18,11 @@
 public class PageServlet extends HttpServlet {
 	static final long serialVersionUID = 1L;
 
+	/* (non-Javadoc)
+	 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
+	 */
 	protected void doGet(HttpServletRequest request,
-			HttpServletResponse response) throws ServletException, IOException {
+		HttpServletResponse response) throws ServletException, IOException {
 		
 		String cometId = request.getParameter("sessionId");
 		String delay = request.getParameter("delay");
@@ -36,20 +39,20 @@
 			e.printStackTrace();
 		}
 
+
 		String uri = "dummy.html";	
-		if (codeStr.equals("200")) {
-			
-		}
+
 		int code = Integer.parseInt(codeStr);
 		switch (code) {
 		case 200: request.getRequestDispatcher(uri).forward(request, response); break;
 		
-		case 302: response.sendRedirect(uri);
-		     break;
+		case 302: 
+        response.sendRedirect("http://" + request.getServerName() + ":" + request.getLocalPort()
+ request.getContextPath() + "/" + uri);
+		    break;
 		
 		case 403:
 		case 404:
-		case 500: response.sendError(code);
+		case 500: response.sendError(code, codeStr);
 			/*try  { 
 				int test = 1/0;
 			} 



Mime
View raw message