You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2006/11/22 12:54:06 UTC
svn commit: r478136 -
/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/reactor/
Author: olegk
Date: Wed Nov 22 03:54:05 2006
New Revision: 478136
URL: http://svn.apache.org/viewvc?view=rev&rev=478136
Log:
Some code refactoring in the default I/O reactor impls
Modified:
jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/reactor/AbstractIOReactor.java
jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/reactor/AbstractMultiworkerIOReactor.java
jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/reactor/BaseIOReactor.java
jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/reactor/DefaultConnectingIOReactor.java
jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/reactor/DefaultListeningIOReactor.java
Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/reactor/AbstractIOReactor.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/reactor/AbstractIOReactor.java?view=diff&rev=478136&r1=478135&r2=478136
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/reactor/AbstractIOReactor.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/reactor/AbstractIOReactor.java Wed Nov 22 03:54:05 2006
@@ -43,26 +43,26 @@
public abstract class AbstractIOReactor implements IOReactor {
- public static int TIMEOUT_CHECK_INTERVAL = 1000;
-
private volatile boolean closed = false;
+ private final long selectTimeout;
private final Selector selector;
private final SessionSet sessions;
private final SessionQueue closedSessions;
private final ChannelQueue newChannels;
- private long lastTimeoutCheck;
-
protected IOEventDispatch eventDispatch = null;
- public AbstractIOReactor() throws IOException {
+ public AbstractIOReactor(long selectTimeout) throws IOException {
super();
+ if (selectTimeout <= 0) {
+ throw new IllegalArgumentException("Select timeout may not be negative or zero");
+ }
+ this.selectTimeout = selectTimeout;
this.selector = Selector.open();
this.sessions = new SessionSet();
this.closedSessions = new SessionQueue();
this.newChannels = new ChannelQueue();
- this.lastTimeoutCheck = System.currentTimeMillis();
}
protected abstract void acceptable(SelectionKey key);
@@ -75,6 +75,8 @@
protected abstract void timeoutCheck(SelectionKey key, long now);
+ protected abstract void validate(Set keys);
+
protected abstract void keyCreated(final SelectionKey key, final IOSession session);
protected abstract IOSession keyCancelled(final SelectionKey key);
@@ -96,7 +98,7 @@
try {
for (;;) {
- int readyCount = this.selector.select(TIMEOUT_CHECK_INTERVAL);
+ int readyCount = this.selector.select(this.selectTimeout);
if (this.closed) {
break;
}
@@ -107,14 +109,7 @@
processEvents(this.selector.selectedKeys());
}
- long currentTime = System.currentTimeMillis();
- if( (currentTime - this.lastTimeoutCheck) >= TIMEOUT_CHECK_INTERVAL) {
- this.lastTimeoutCheck = currentTime;
- Set keys = this.selector.keys();
- if (keys != null) {
- processTimeouts(keys);
- }
- }
+ validate(this.selector.keys());
processClosedSessions();
@@ -154,14 +149,6 @@
this.closedSessions.push(session);
}
key.attach(null);
- }
- }
-
- private void processTimeouts(final Set keys) {
- long now = System.currentTimeMillis();
- for (Iterator it = keys.iterator(); it.hasNext();) {
- SelectionKey key = (SelectionKey) it.next();
- timeoutCheck(key, now);
}
}
Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/reactor/AbstractMultiworkerIOReactor.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/reactor/AbstractMultiworkerIOReactor.java?view=diff&rev=478136&r1=478135&r2=478136
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/reactor/AbstractMultiworkerIOReactor.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/reactor/AbstractMultiworkerIOReactor.java Wed Nov 22 03:54:05 2006
@@ -43,7 +43,7 @@
private int currentWorker = 0;
- public AbstractMultiworkerIOReactor(int workerCount) throws IOException {
+ public AbstractMultiworkerIOReactor(long selectTimeout, int workerCount) throws IOException {
super();
if (workerCount <= 0) {
throw new IllegalArgumentException("Worker count may not be negative or zero");
@@ -52,7 +52,7 @@
this.ioReactors = new BaseIOReactor[workerCount];
this.threads = new WorkerThread[workerCount];
for (int i = 0; i < this.ioReactors.length; i++) {
- this.ioReactors[i] = new BaseIOReactor();
+ this.ioReactors[i] = new BaseIOReactor(selectTimeout);
}
}
Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/reactor/BaseIOReactor.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/reactor/BaseIOReactor.java?view=diff&rev=478136&r1=478135&r2=478136
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/reactor/BaseIOReactor.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/reactor/BaseIOReactor.java Wed Nov 22 03:54:05 2006
@@ -31,13 +31,21 @@
import java.io.IOException;
import java.nio.channels.SelectionKey;
+import java.util.Iterator;
+import java.util.Set;
import org.apache.http.nio.reactor.IOSession;
public class BaseIOReactor extends AbstractIOReactor {
- public BaseIOReactor() throws IOException {
- super();
+ private final long timeoutCheckInterval;
+
+ private long lastTimeoutCheck;
+
+ public BaseIOReactor(long selectTimeout) throws IOException {
+ super(selectTimeout);
+ this.timeoutCheckInterval = selectTimeout;
+ this.lastTimeoutCheck = System.currentTimeMillis();
}
protected void acceptable(final SelectionKey key) {
@@ -60,6 +68,19 @@
handle.resetLastWrite();
this.eventDispatch.outputReady(session);
+ }
+
+ protected void validate(final Set keys) {
+ long currentTime = System.currentTimeMillis();
+ if( (currentTime - this.lastTimeoutCheck) >= this.timeoutCheckInterval) {
+ this.lastTimeoutCheck = currentTime;
+ if (keys != null) {
+ for (Iterator it = keys.iterator(); it.hasNext();) {
+ SelectionKey key = (SelectionKey) it.next();
+ timeoutCheck(key, currentTime);
+ }
+ }
+ }
}
protected void timeoutCheck(final SelectionKey key, long now) {
Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/reactor/DefaultConnectingIOReactor.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/reactor/DefaultConnectingIOReactor.java?view=diff&rev=478136&r1=478135&r2=478136
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/reactor/DefaultConnectingIOReactor.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/reactor/DefaultConnectingIOReactor.java Wed Nov 22 03:54:05 2006
@@ -59,7 +59,7 @@
public DefaultConnectingIOReactor(int workerCount, final HttpParams params)
throws IOException {
- super(workerCount);
+ super(TIMEOUT_CHECK_INTERVAL, workerCount);
if (params == null) {
throw new IllegalArgumentException("HTTP parameters may not be null");
}
Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/reactor/DefaultListeningIOReactor.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/reactor/DefaultListeningIOReactor.java?view=diff&rev=478136&r1=478135&r2=478136
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/reactor/DefaultListeningIOReactor.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/reactor/DefaultListeningIOReactor.java Wed Nov 22 03:54:05 2006
@@ -57,7 +57,7 @@
public DefaultListeningIOReactor(int workerCount, final HttpParams params)
throws IOException {
- super(workerCount);
+ super(TIMEOUT_CHECK_INTERVAL, workerCount);
if (params == null) {
throw new IllegalArgumentException("HTTP parameters may not be null");
}