Trustin Lee wrote:
On 8/23/07, Pierre-Louis Bonicoli <[EMAIL PROTECTED]> wrote:
Hi,
I change *a lot* the client parts of the codec tutorial, i keep only two
classes : GraphicalCharGenClient and ImageClient (see below).

I don't understand why the thread called "SocketConnector" doesn't stop
(and so the program doesn't terminate).

Probably your application will exit after 1 minute (i.e. default
worker timeout).  Try to set SocketConnector.workerTimeout to smaller
value (e.g. 5).

HTH,
Trustin
Hi,
I call SocketConnector.workerTimeout(5) but the problem stills, the thread which doesn't stop is "SocketConnectorIoProcessor-0.0".
My config : java client VM (1.5.0_12), dual-core microprocessor, mina-trunk.

I add some logs to SocketConnector and SocketIoProcessor :
the thread "SocketConnectorIoProcessor-0.0" doesn't close because "selector.keys().isEmpty()" (line 530 SocketConnectorIoProcessor.java) stills true. I don't understand what is the link between the "selector" member of SocketConnectorIoProcessor and the "selector" member of SocketConnector but i seems to me that "selector.keys().isEmpty()" in SocketConnectorIoProcessor must return false when "selector.keys().isEmpty()" return false in SocketConnector (line 333).

See below diff and example - let me know if you want a tar with source code (ie mina_trunk with some logs added and example).

Pierre-Louis

Here are diff for classes SocketConnector and SocketConnectorIoProcessor :

Index: core/src/main/java/org/apache/mina/transport/socket/nio/SocketConnector.java
===================================================================
--- core/src/main/java/org/apache/mina/transport/socket/nio/SocketConnector.java (révision 568910) +++ core/src/main/java/org/apache/mina/transport/socket/nio/SocketConnector.java (copie de travail)
@@ -43,6 +43,9 @@
import org.apache.mina.util.NamePreservingRunnable;
import org.apache.mina.util.NewThreadExecutor;

+import org.slf4j.LoggerFactory;
+import org.slf4j.Logger;
+
/**
 * [EMAIL PROTECTED] IoConnector} for socket transport (TCP/IP).
 *
@@ -313,9 +316,11 @@

    private class Worker implements Runnable {
        private long lastActive = System.currentTimeMillis();
+ private final org.slf4j.Logger mLogger = LoggerFactory.getLogger(Worker.class);

        public void run() {
Thread.currentThread().setName(SocketConnector.this.threadName);
+            String pref = SocketConnector.this.threadName;

            for (;;) {
                try {
@@ -330,7 +335,9 @@
                    processTimedOutSessions(selector.keys());

                    if (selector.keys().isEmpty()) {
+ this.mLogger.info(pref + "selector.keys().isEmpty() == true"); if (System.currentTimeMillis() - lastActive > workerTimeout * 1000L) {
+                            this.mLogger.info(pref + "workerTimeout");
                            synchronized (lock) {
                                if (selector.keys().isEmpty()
                                        && connectQueue.isEmpty()) {
@@ -340,6 +347,7 @@
                            }
                        }
                    } else {
+ this.mLogger.info(pref + "selector.keys().isEmpty() == false");
                        lastActive = System.currentTimeMillis();
                    }
                } catch (IOException e) {
@@ -371,4 +379,4 @@
    protected IoServiceListenerSupport getListeners() {
        return super.getListeners();
    }
-}
\ Pas de fin de ligne à la fin du fichier
+}


Index: core/src/main/java/org/apache/mina/transport/socket/nio/SocketIoProcessor.java
===================================================================
--- core/src/main/java/org/apache/mina/transport/socket/nio/SocketIoProcessor.java (révision 568910) +++ core/src/main/java/org/apache/mina/transport/socket/nio/SocketIoProcessor.java (copie de travail)
@@ -41,6 +41,9 @@
import org.apache.mina.common.WriteTimeoutException;
import org.apache.mina.util.NamePreservingRunnable;

+import org.slf4j.LoggerFactory;
+import org.slf4j.Logger;
+
/**
* Performs all I/O operations for sockets which is connected or bound. This class is used by MINA internally.
 *
@@ -508,8 +511,11 @@
    }

    private class Worker implements Runnable {
+ private final org.slf4j.Logger mLogger = LoggerFactory.getLogger(Worker.class);
+
        public void run() {
Thread.currentThread().setName(SocketIoProcessor.this.threadName);
+            String pref = SocketIoProcessor.this.threadName;

            for (;;) {
                try {
@@ -526,6 +532,7 @@
                    notifyIdleness();

                    if (selector.keys().isEmpty()) {
+ this.mLogger.info(pref + " selector.keys().isEmpty() == true");
                        synchronized (lock) {
                            if (selector.keys().isEmpty()
                                    && newSessions.isEmpty()) {
@@ -533,6 +540,8 @@
                                break;
                            }
                        }
+                    } else {
+ this.mLogger.info(pref + " selector.keys().isEmpty() == false");
                    }
                } catch (Throwable t) {
                    ExceptionMonitor.getInstance().exceptionCaught(t);
@@ -546,4 +555,4 @@
            }
        }
    }
-}
\ Pas de fin de ligne à la fin du fichier
+}



You need only this two classes for reproduce :

GraphicalCharGenClient.java
===================================================================
package org.apache.mina.example.imagine.client;

public class GraphicalCharGenClient
{
   public static void main(String args[]) throws Exception
   {
       ImageClient imageClient = new ImageClient("www.google.com", 80);
       imageClient.connect();
   }
}

ImageClient.java
===================================================================
package org.apache.mina.example.imagine.client;

import org.apache.mina.transport.socket.nio.SocketConnector;
import org.apache.mina.common.IoHandlerAdapter;
import org.apache.mina.common.IoSession;
import org.apache.mina.common.ConnectFuture;
import org.apache.mina.common.RuntimeIOException;
import java.util.concurrent.*;
import java.net.InetSocketAddress;

import org.apache.mina.common.IdleStatus;

public class ImageClient extends IoHandlerAdapter
{
   public static final int CONNECT_TIMEOUT = 3000;

   private String host;
   private int port;
   private SocketConnector connector;
   private IoSession session;
   private GraphicalCharGenClient imageListener;
public void finalize() throws Throwable
   {
       super.finalize();
       session.close();
   }

   public ImageClient(String host, int port)
   {
       this.host = host;
       this.port = port;
connector = new SocketConnector(); connector.getSessionConfig().setTcpNoDelay(true);
       connector.setWorkerTimeout(5);
ScheduledThreadPoolExecutor scheduler = new ScheduledThreadPoolExecutor(Runtime.getRuntime().availableProcessors());
   }

   public void connect()
   {
       connector.setHandler(this);
ConnectFuture connectFuture = connector.connect(new InetSocketAddress(host,port)); if(connectFuture.awaitUninterruptibly(CONNECT_TIMEOUT)) {
           session = connectFuture.getSession();
           try {
               session.getConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
           } catch(Throwable t) {
               t.printStackTrace();
               System.exit(1);
           }
} }

   public void disconnect() {
       if (session != null)
       {
           session = null;
       }
   }
}

Reply via email to