Author: mheath
Date: Tue Feb 5 13:43:04 2008
New Revision: 618785
URL: http://svn.apache.org/viewvc?rev=618785&view=rev
Log:
Refactored to use MINA idle mechanism for timeout check.
Modified:
mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/AsyncHttpClient.java
mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/codec/HttpIoHandler.java
mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/codec/HttpRequestMessage.java
Modified:
mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/AsyncHttpClient.java
URL:
http://svn.apache.org/viewvc/mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/AsyncHttpClient.java?rev=618785&r1=618784&r2=618785&view=diff
==============================================================================
---
mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/AsyncHttpClient.java
(original)
+++
mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/AsyncHttpClient.java
Tue Feb 5 13:43:04 2008
@@ -26,7 +26,6 @@
import java.security.GeneralSecurityException;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executor;
-import java.util.concurrent.ScheduledExecutorService;
import javax.net.ssl.SSLContext;
@@ -336,7 +335,7 @@
* use in one-off connections.
*/
public AsyncHttpClient() {
- this(DEFAULT_CONNECTION_TIMEOUT, null, null);
+ this(DEFAULT_CONNECTION_TIMEOUT, null);
}
/**
@@ -344,10 +343,9 @@
* for processing connections.
*
* @param executor the executor
- * @param scheduler the scheduler to use to track timeouts
*/
- public AsyncHttpClient(Executor executor, ScheduledExecutorService
scheduler) {
- this(DEFAULT_CONNECTION_TIMEOUT, executor, scheduler);
+ public AsyncHttpClient(Executor executor) {
+ this(DEFAULT_CONNECTION_TIMEOUT, executor);
}
/**
@@ -357,7 +355,7 @@
* @param connectionTimeout the connection timeout in milliseconds.
*/
public AsyncHttpClient(int connectionTimeout) {
- this(connectionTimeout, null, null);
+ this(connectionTimeout, null);
}
/**
@@ -365,19 +363,13 @@
*
* @param connectionTimeout the connection timeout in milliseconds.
* @param executor the ExceutorService to use to process
connections.
- * @param scheduler the scheduler to use to track timeouts
*/
- public AsyncHttpClient(int connectionTimeout, Executor executor,
ScheduledExecutorService scheduler) {
+ public AsyncHttpClient(int connectionTimeout, Executor executor) {
this.connectionTimeout = connectionTimeout;
threadPool = executor;
- if (scheduler == null) {
- handler = new HttpIoHandler();
- }
- else {
- handler = new HttpIoHandler(scheduler);
- }
+ handler = new HttpIoHandler();
if (threadPool == null) {
connector = new NioSocketConnector();
@@ -588,8 +580,6 @@
* done using the object or a hang can occur.
*/
public void destroyAll() {
- handler.destroy();
-
if (connector != null) {
connector.dispose();
//connector.setWorkerTimeout(0);
Modified:
mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/codec/HttpIoHandler.java
URL:
http://svn.apache.org/viewvc/mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/codec/HttpIoHandler.java?rev=618785&r1=618784&r2=618785&view=diff
==============================================================================
---
mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/codec/HttpIoHandler.java
(original)
+++
mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/codec/HttpIoHandler.java
Tue Feb 5 13:43:04 2008
@@ -20,10 +20,6 @@
package org.apache.ahc.codec;
import java.net.URL;
-import java.util.concurrent.Executors;
-import java.util.concurrent.ScheduledExecutorService;
-import java.util.concurrent.ScheduledFuture;
-import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.apache.ahc.AsyncHttpClient;
@@ -34,6 +30,7 @@
import org.apache.ahc.auth.AuthState;
import org.apache.ahc.util.MonitoringEvent;
import org.apache.ahc.util.NameValuePair;
+import org.apache.mina.common.IdleStatus;
import org.apache.mina.common.IoHandlerAdapter;
import org.apache.mina.common.IoSession;
@@ -60,32 +57,11 @@
public static final String CONNECTION_CLOSE = "close";
/**
- * The scheduler service to handle timeouts.
- */
- private final ScheduledExecutorService scheduler;
-
- /**
* The session cache used for reusable connections
*/
private SessionCache sessionCache;
/**
- * Instantiates a new HttpIoHandler with a new a single-threaded executor.
- */
- public HttpIoHandler() {
- this(Executors.newSingleThreadScheduledExecutor());
- }
-
- /**
- * Instantiates a new HttpIoHandler with the supplied scheduler.
- *
- * @param scheduler the scheduler to use to track timeouts
- */
- public HttpIoHandler(ScheduledExecutorService scheduler) {
- this.scheduler = scheduler;
- }
-
- /**
* Set the session cache that should be used for
* connection reuse.
*
@@ -107,13 +83,6 @@
}
/**
- * Destroys the handler and shuts down the scheduler.
- */
- public void destroy() {
- scheduler.shutdownNow();
- }
-
- /**
* Stub for handling sessionOpened events.
*
* @see
org.apache.mina.common.IoHandlerAdapter#sessionOpened(org.apache.mina.common.IoSession)
@@ -201,8 +170,6 @@
}
}
- cancelTasks(request);
-
// notify any interesting parties that this is starting
client.notifyMonitoringListeners(MonitoringEvent.REQUEST_COMPLETED,
request);
// complete the future which will also fire the callback
@@ -230,7 +197,6 @@
ioSession.removeAttribute(CURRENT_RESPONSE);
HttpRequestMessage request = (HttpRequestMessage)
ioSession.getAttribute(CURRENT_REQUEST);
- cancelTasks(request);
AsyncHttpClient client = (AsyncHttpClient) ioSession.getAttachment();
@@ -262,7 +228,6 @@
cache.removeSession(ioSession);
}
HttpRequestMessage request = (HttpRequestMessage)
ioSession.getAttribute(CURRENT_REQUEST);
- cancelTasks(request);
AsyncHttpClient client = (AsyncHttpClient) ioSession.getAttachment();
// notify any interesting parties that this is starting
client.notifyMonitoringListeners(MonitoringEvent.CONNECTION_CLOSED_BY_SERVER,
request);
@@ -284,73 +249,24 @@
public void messageSent(IoSession ioSession, Object object) throws
Exception {
HttpRequestMessage msg = (HttpRequestMessage) object;
- //Start the timeout timer now if a timeout is needed and there is not
one already in effect for this request
- if (msg.getTimeOut() > 0 && msg.getTimeoutHandle() == null) {
- TimeoutTask task = new TimeoutTask(ioSession);
- ScheduledFuture<?> handle = scheduler.schedule(task,
msg.getTimeOut(), TimeUnit.MILLISECONDS);
- msg.setTimeoutHandle(handle);
- }
- }
-
- /**
- * Utility function to cancel a request timeout task.
- *
- * @param request the [EMAIL PROTECTED] HttpRequestMessage} request
- */
- private void cancelTasks(HttpRequestMessage request) {
- ScheduledFuture<?> handle = request.getTimeoutHandle();
- if (handle != null) {
- boolean canceled = handle.cancel(true);
- //See if it canceled
- if (!canceled) {
- //Couldn't cancel it and it ran, so too late :-(
- return;
- }
- request.setTimeoutHandle(null);
+ if (msg.getTimeOut() > 0) {
+ ioSession.getConfig().setReaderIdleTime(msg.getTimeOut() /
1000);
}
}
- /**
- * The Class TimeoutTask. Subclass that encapsulates handler for timeouts
for the scheduler.
- */
- class TimeoutTask implements Runnable {
-
- /**
- * The session object.
- */
- private IoSession sess;
-
- /**
- * Instantiates a new timeout task.
- *
- * @param sess the [EMAIL PROTECTED] org.apache.mina.common.IoSession}
representing the connection to the server.
- */
- public TimeoutTask(IoSession sess) {
- this.sess = sess;
- }
-
- /**
- * The running task which handles timing out the connection.
- *
- * @see java.lang.Runnable#run()
- */
- public void run() {
- // complete the future which will also fire the callback
- HttpRequestMessage request =
(HttpRequestMessage)sess.getAttribute(CURRENT_REQUEST);
-
- AsyncHttpClient client = (AsyncHttpClient) sess.getAttachment();
-
- // notify any interesting parties that this is starting
- client.notifyMonitoringListeners(MonitoringEvent.REQUEST_TIMEOUT,
request);
-
- ResponseFuture result = request.getResponseFuture();
- result.setException(new TimeoutException());
-
- //Close the session, its no good since the server is timing out
- sess.close();
- }
+ @Override
+ public void sessionIdle(IoSession session, IdleStatus status) {
+ // complete the future which will also fire the callback
+ HttpRequestMessage request =
(HttpRequestMessage)session.getAttribute(CURRENT_REQUEST);
- }
+ // notify any interesting parties that this is starting
+ AsyncHttpClient client = (AsyncHttpClient) session.getAttachment();
+ client.notifyMonitoringListeners(MonitoringEvent.REQUEST_TIMEOUT,
request);
+
+ ResponseFuture result = request.getResponseFuture();
+ result.setException(new TimeoutException());
+ session.close();
+ }
}
Modified:
mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/codec/HttpRequestMessage.java
URL:
http://svn.apache.org/viewvc/mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/codec/HttpRequestMessage.java?rev=618785&r1=618784&r2=618785&view=diff
==============================================================================
---
mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/codec/HttpRequestMessage.java
(original)
+++
mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/codec/HttpRequestMessage.java
Tue Feb 5 13:43:04 2008
@@ -111,11 +111,6 @@
private boolean followRedirects = true;
/**
- * The timeout handle.
- */
- private ScheduledFuture<?> timeoutHandle;
-
- /**
* The response future.
*/
private volatile ResponseFuture responseFuture;
@@ -190,24 +185,6 @@
*/
public void setTimeOut(int timeOut) {
this.timeOut = timeOut;
- }
-
- /**
- * Gets the timeout handle.
- *
- * @return the timeout <code>ScheduledFuture</code> handle
- */
- protected ScheduledFuture<?> getTimeoutHandle() {
- return timeoutHandle;
- }
-
- /**
- * Sets the timeout handle.
- *
- * @param timeoutHandle the new <code>ScheduledFuture</code> timeout handle
- */
- protected void setTimeoutHandle(ScheduledFuture<?> timeoutHandle) {
- this.timeoutHandle = timeoutHandle;
}
/**