Author: matthieu
Date: Mon Oct 19 07:33:22 2015
New Revision: 1709372

URL: http://svn.apache.org/viewvc?rev=1709372&view=rev
Log:
MPT-29 handle EOF in read retry

Modified:
    james/project/trunk/mpt/core/pom.xml
    
james/project/trunk/mpt/core/src/main/java/org/apache/james/mpt/session/ExternalSession.java

Modified: james/project/trunk/mpt/core/pom.xml
URL: 
http://svn.apache.org/viewvc/james/project/trunk/mpt/core/pom.xml?rev=1709372&r1=1709371&r2=1709372&view=diff
==============================================================================
--- james/project/trunk/mpt/core/pom.xml (original)
+++ james/project/trunk/mpt/core/pom.xml Mon Oct 19 07:33:22 2015
@@ -61,7 +61,11 @@
             <type>test-jar</type>
             <scope>test</scope>
         </dependency>
-
+        <dependency>
+            <groupId>com.jayway.awaitility</groupId>
+            <artifactId>awaitility</artifactId>
+            <version>1.6.5</version>
+        </dependency>
         <dependency>
             <groupId>commons-io</groupId>
             <artifactId>commons-io</artifactId>

Modified: 
james/project/trunk/mpt/core/src/main/java/org/apache/james/mpt/session/ExternalSession.java
URL: 
http://svn.apache.org/viewvc/james/project/trunk/mpt/core/src/main/java/org/apache/james/mpt/session/ExternalSession.java?rev=1709372&r1=1709371&r2=1709372&view=diff
==============================================================================
--- 
james/project/trunk/mpt/core/src/main/java/org/apache/james/mpt/session/ExternalSession.java
 (original)
+++ 
james/project/trunk/mpt/core/src/main/java/org/apache/james/mpt/session/ExternalSession.java
 Mon Oct 19 07:33:22 2015
@@ -19,17 +19,21 @@
 
 package org.apache.james.mpt.session;
 
+import java.io.IOException;
 import java.nio.ByteBuffer;
 import java.nio.channels.SocketChannel;
 import java.nio.charset.Charset;
+import java.util.concurrent.Callable;
+import java.util.concurrent.TimeUnit;
 
+import org.apache.commons.lang.mutable.MutableInt;
 import org.apache.james.mpt.api.Monitor;
 import org.apache.james.mpt.api.Session;
 
-public final class ExternalSession implements Session {
+import com.jayway.awaitility.Awaitility;
+import com.jayway.awaitility.Duration;
 
-    /** Number of milliseconds to sleep after empty read */
-    private static final int SHORT_WAIT_FOR_INPUT = 10;
+public final class ExternalSession implements Session {
 
     private static final byte[] CRLF = { '\r', '\n' };
 
@@ -112,19 +116,30 @@ public final class ExternalSession imple
         else {
             monitor.debug("[Reading into buffer]");
             readBuffer.clear();
-            while (socket.read(readBuffer) == 0) {
-                // No response yet
-                // Wait a little while
-                Thread.sleep(SHORT_WAIT_FOR_INPUT);
-            }
+            result = tryReadFromSocket();
             // Reset for transfer into string buffer
             readBuffer.flip();
             monitor.debug("[Done]");
-            result = true;
         }
         return result;
     }
 
+    private boolean tryReadFromSocket() throws IOException, 
InterruptedException {
+        final MutableInt status = new MutableInt(0);
+        Awaitility
+            .waitAtMost(Duration.ONE_MINUTE)
+            .pollDelay(new Duration(10, TimeUnit.MILLISECONDS))
+            .until(new Callable<Boolean>() {
+                @Override
+                public Boolean call() throws Exception {
+                    int read = socket.read(readBuffer);
+                    status.setValue(read);
+                    return read != 0;
+                }
+            });
+        return status.intValue() > 0;
+    }
+
     public void start() throws Exception {
         while (!socket.finishConnect()) {
             monitor.note("connecting...");



---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org
For additional commands, e-mail: server-dev-h...@james.apache.org

Reply via email to