geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pra...@apache.org
Subject svn commit: r599210 - /geronimo/samples/trunk/samples/async-http/http-local-app/src/main/java/org/apache/geronimo/NIOServlet.java
Date Thu, 29 Nov 2007 00:01:40 GMT
Author: prasad
Date: Wed Nov 28 16:01:33 2007
New Revision: 599210

URL: http://svn.apache.org/viewvc?rev=599210&view=rev
Log:
* major fix with a single static BufferHandler

Modified:
    geronimo/samples/trunk/samples/async-http/http-local-app/src/main/java/org/apache/geronimo/NIOServlet.java

Modified: geronimo/samples/trunk/samples/async-http/http-local-app/src/main/java/org/apache/geronimo/NIOServlet.java
URL: http://svn.apache.org/viewvc/geronimo/samples/trunk/samples/async-http/http-local-app/src/main/java/org/apache/geronimo/NIOServlet.java?rev=599210&r1=599209&r2=599210&view=diff
==============================================================================
--- geronimo/samples/trunk/samples/async-http/http-local-app/src/main/java/org/apache/geronimo/NIOServlet.java
(original)
+++ geronimo/samples/trunk/samples/async-http/http-local-app/src/main/java/org/apache/geronimo/NIOServlet.java
Wed Nov 28 16:01:33 2007
@@ -15,6 +15,7 @@
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+import org.apache.http.nio.reactor.SessionRequestCallback;
 
 import java.io.InterruptedIOException;
 import java.net.InetSocketAddress;
@@ -63,11 +64,11 @@
 public class NIOServlet extends HttpServlet implements CometProcessor {
     static final long serialVersionUID = 1L;
 
-    private String remoteUrl;
-    private HttpParams httpParams = new BasicHttpParams(null);
-    //private BasicHttpProcessor httpproc = new BasicHttpProcessor();
+    String remoteUrl;
+    private static HttpParams httpParams = new BasicHttpParams(null);
+    private static BasicHttpProcessor httpproc = new BasicHttpProcessor();
     private static ConnectingIOReactor ioReactor;
-    private static ExecutorService threadPool = Executors.newFixedThreadPool(8);
+    private static IOEventDispatch ioEventDispatch;
 
     public void init() throws ServletException {
         getServletContext().log("getinit init");
@@ -82,17 +83,21 @@
         .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true)
         .setParameter(CoreProtocolPNames.USER_AGENT, "HttpComponents/1.1");
 
-
-        /*
         httpproc.addInterceptor(new RequestContent());
         httpproc.addInterceptor(new RequestTargetHost());
         httpproc.addInterceptor(new RequestConnControl());
         httpproc.addInterceptor(new RequestUserAgent());
         httpproc.addInterceptor(new RequestExpectContinue());
-        */
+
+        BufferingHttpClientHandler handler = new BufferingHttpClientHandler(httpproc, new
MyHttpRequestExecutionHandler(),
+                                                                                new DefaultConnectionReuseStrategy(),
httpParams);
+
+        handler.setEventListener(new EventLogger());
+
+        ioEventDispatch = new DefaultClientIOEventDispatch(handler, httpParams);
 
         try {
-            ioReactor = new DefaultConnectingIOReactor(1, httpParams);
+            ioReactor = new DefaultConnectingIOReactor(20, httpParams);
         }
         catch ( IOReactorException e ) {
             throw new ServletException(e);
@@ -100,7 +105,6 @@
 
     }
 
-    /*
     @Override
     public void destroy() {
         // TODO Auto-generated method stub
@@ -108,13 +112,11 @@
         System.out.println("Shutting down I/O reactor");
         try {
             ioReactor.shutdown();
-            threadPool.shutdown();
         }
         catch ( Exception e ) {
             e.printStackTrace();
         }
     }
-    */
 
     /**
      * Process the given Comet event.
@@ -152,29 +154,7 @@
 
             String url_string = remoteUri + "?sessionId=" + sessionId + "&delay=" + params[0]
+ "&code=" + params[1];       
 
-            BasicHttpProcessor httpproc = new BasicHttpProcessor();
-            httpproc.addInterceptor(new RequestContent());
-            httpproc.addInterceptor(new RequestTargetHost());
-            httpproc.addInterceptor(new RequestConnControl());
-            httpproc.addInterceptor(new RequestUserAgent());
-            httpproc.addInterceptor(new RequestExpectContinue());
-
-            /*
-            try {
-                ioReactor = new DefaultConnectingIOReactor(8, httpParams);
-            }
-            catch ( IOReactorException e ) {
-                throw new ServletException(e);
-            }
-            */
-
-            BufferingHttpClientHandler handler = new BufferingHttpClientHandler(httpproc,
new MyHttpRequestExecutionHandler(event, sessionId, url_string),
-                                                                                new DefaultConnectionReuseStrategy(),
httpParams);
-
-            handler.setEventListener(new EventLogger());
-
-            final IOEventDispatch ioEventDispatch = new DefaultClientIOEventDispatch(handler,
httpParams);
-
+            
             Thread t = new Thread(new Runnable() {
 
                                       public void run() {
@@ -194,35 +174,15 @@
                                   });
             t.start();
             
-            /*
-            threadPool.execute(new Runnable() {
-
-                                      public void run() {
-                                          try {
-                                              System.out.println("Running...");
-                                              ioReactor.execute(ioEventDispatch);
-                                          }
-                                          catch ( InterruptedIOException ex ) {
-                                              System.err.println("Interrupted");
-                                          }
-                                          catch ( IOException e ) {
-                                              System.err.println("I/O error: " + e.getMessage());
-                                          }
-                                          System.out.println("Shutdown");
-                                      }
-
-                                  });
-            */
-
-            SessionRequest reqs = ioReactor.connect(new InetSocketAddress(remoteUrl, 8080),
null, new HttpHost(remoteUrl), null);
+            
+            SessionRequest reqs = ioReactor.connect(new InetSocketAddress(remoteUrl, 8080),
null, new MyAttachment(event, sessionId, url_string), null);
 
+            System.out.println("event BEGIN processed");
         }
         else if ( (event.getEventType() == CometEvent.EventType.ERROR)  || (event.getEventType()
== CometEvent.EventType.END) ) {
             event.close();
-            //ioReactor.shutdown();
         }
 
-        System.out.println("doGet processed");
     }
 
     protected Integer[] generateRandomParams() {
@@ -244,26 +204,21 @@
 
     static class MyHttpRequestExecutionHandler implements HttpRequestExecutionHandler {
 
+        private final static String TIME               = "time";
+        private final static String COMET              = "comet-event";
+        private final static String SESSION_ID         = "session-id";
+        private final static String REMOTE_URI         = "remote-uri";
         private final static String REQUEST_SENT       = "request-sent";
         private final static String RESPONSE_RECEIVED  = "response-received";
-        private long time;
-        private String sessionId;
-        private String uri;
-        private CometEvent event;
 
 
-        public MyHttpRequestExecutionHandler(CometEvent event, String sessionId, String uri)
{
-            super();
-            this.sessionId = sessionId;
-            this.uri = uri;
-            this.event=event;
-        }
-
-        public void initalizeContext(final HttpContext context, final Object attachment)
{
+        public void initalizeContext(final HttpContext context, final Object attach) {
             System.out.println("Initializing Context");
-            HttpHost targetHost = (HttpHost) attachment;
-            context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, targetHost);
-            this.time=new Date().getTime();
+            MyAttachment attachment = (MyAttachment)attach;
+            context.setAttribute(REMOTE_URI, attachment.getUri());
+            context.setAttribute(COMET, attachment.getCometEvent());
+            context.setAttribute(SESSION_ID, attachment.getSessionId());
+            context.setAttribute(TIME,new Date().getTime());
         }
 
         public void finalizeContext(final HttpContext context) {
@@ -272,14 +227,10 @@
             if ( flag == null ) {
                 // Signal completion of the request execution
                 /*
-                synchronized (this.requestCount) {
-                    this.requestCount.decrement();
-                    this.requestCount.notifyAll();
-                }
                 try {
-                    event.close();
+                    ((CometEvent)context.getAttribute(COMET)).close();
                 }
-                catch (IOException e) {
+                catch (Exception e) {
                     e.printStackTrace();
                 }
                 */
@@ -288,17 +239,16 @@
 
         public HttpRequest submitRequest(final HttpContext context) {
             System.out.println("Submitting request");
-            HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
             Object flag = context.getAttribute(REQUEST_SENT);
             if ( flag == null ) {
                 // Stick some object into the context
                 context.setAttribute(REQUEST_SENT, Boolean.TRUE);
 
                 System.out.println("--------------");
-                System.out.println("Sending request to " + targetHost + " with sessionId:
" + this.sessionId);
+                System.out.println("Sending request with sessionId: " + (String)context.getAttribute(SESSION_ID));
                 System.out.println("--------------");
 
-                return new BasicHttpRequest("GET", uri);
+                return new BasicHttpRequest("GET", (String)context.getAttribute(REMOTE_URI));
             }
             else {
                 // No new request to submit
@@ -308,7 +258,6 @@
 
         public void handleResponse(final HttpResponse res, final HttpContext context) {
             System.out.println("Handle response");
-            HttpServletResponse response = event.getHttpServletResponse();
             HttpEntity entity = res.getEntity();
             try {
                 String content = EntityUtils.toString(entity);
@@ -319,6 +268,8 @@
                 System.out.println("Document length: " + content.length());
                 System.out.println("--------------");
 
+                CometEvent event = (CometEvent)context.getAttribute(COMET);
+                HttpServletResponse response = event.getHttpServletResponse();
                 response.setContentType("text/html");
                 if ( content.length() > 0 ) {
                     response.setContentLength(content.length());
@@ -334,7 +285,8 @@
                         e.printStackTrace();
                     }
                 }
-                System.out.println("  Responded to " + this.sessionId + " in " + (new Date().getTime()
- this.time));
+                //System.out.println("  Responded to " + (String)context.getAttribute(SESSION_ID)
+ " in " + (new Date().getTime() - (long)context.getAttribute(TIME)));
+                System.out.println("  Responded to " + (String)context.getAttribute(SESSION_ID));
 
                 //event.close();
             }
@@ -344,13 +296,6 @@
 
             context.setAttribute(RESPONSE_RECEIVED, Boolean.TRUE);
 
-            // Signal completion of the request execution
-            /*
-            synchronized (this.requestCount) {
-                this.requestCount.decrement();
-                this.requestCount.notifyAll();
-            }
-            */
         }
 
     }
@@ -380,4 +325,30 @@
     }
 
 
+    static class MyAttachment {
+        private String sessionId;
+        private String uri;
+        private CometEvent event;
+
+        public MyAttachment(CometEvent event, String sessionId, String uri) {
+            this.sessionId = sessionId;
+            this.uri = uri;
+            this.event=event;
+        }
+
+        public String getSessionId(){
+            return this.sessionId;
+        }
+
+        public CometEvent getCometEvent(){
+            return this.event;
+        }
+
+        public String getUri() {
+            return this.uri;
+        }
+    }
+  
+    
+      
 }



Mime
View raw message