You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by Michael Qi <fo...@gmail.com> on 2008/05/22 08:32:40 UTC

SocketConnector problem

Hi,

  I write a simple program to test SocketConnector, I start 10
connector but when 5 has connected, it hangs. Could you tell me why?
  PS: I am using mina 1.1.7

Regards
 QiHe

The test code is:

package com.qihe.study.util;

import java.net.InetSocketAddress;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.apache.mina.common.ConnectFuture;
import org.apache.mina.common.IoHandlerAdapter;
import org.apache.mina.common.IoSession;
import org.apache.mina.common.ThreadModel;
import org.apache.mina.transport.socket.nio.SocketConnector;
import org.apache.mina.transport.socket.nio.SocketConnectorConfig;

public class Test extends IoHandlerAdapter{
	private static ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 20,
			60L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
	public static void main(String[] args) {
		Test[] handlers = new Test[10];
		for (int i = 0; i < handlers.length; i++) {
			handlers[i] = new Test();
		}
		SocketConnector[] connectors = new SocketConnector[10];
		int i = 0;
		for (SocketConnector connector : connectors) {
			connector = new SocketConnector(2, executor);
			SocketConnectorConfig scfg = connector.getDefaultConfig();
			scfg.setConnectTimeout(5);
			scfg.setThreadModel(ThreadModel.MANUAL);
			ConnectFuture cf = connector.connect(new
InetSocketAddress("localhost", 5222), handlers[i]);
			cf.join();
			if (cf.isConnected()) {
				System.out.print("okokokokokokok");
			} else {
				System.out.println("ffffffffffffffffff");
			}
			System.out.println("\n i = " + i);
			i++;
		}
		
	}
	@Override
	public void exceptionCaught(IoSession session, Throwable cause)
			throws Exception {
		System.out.println("exceptionCaught");
	}
	@Override
	public void sessionCreated(IoSession session) throws Exception {
		System.out.println("sessionCreated");
	}
	@Override
	public void sessionOpened(IoSession session) throws Exception {
		System.out.println("sessionOpened");
	}	
}

Re: SocketConnector problem

Posted by Tuure Laurinolli <tu...@indagon.com>.
Michael Qi wrote:
> Hi,
>   Thank you for your response. You said SocketConnector should be
> disposed, I wonder when to dispose it? If my application runs as a
> service, the SocketConnector is held for a long time.

It shouldn't be a problem unless you create a lot of SocketConnectors. 
There shouldn't be any need for that.


Re: SocketConnector problem

Posted by Michael Qi <fo...@gmail.com>.
Hi,
  Thank you for your response. You said SocketConnector should be
disposed, I wonder when to dispose it? If my application runs as a
service, the SocketConnector is held for a long time.

Regards
 HeQi

On Mon, May 26, 2008 at 9:47 AM, Tuure Laurinolli
<tu...@indagon.com> wrote:
> Michael Qi wrote:
>>
>> Hi,
>>
>>  I write a simple program to test SocketConnector, I start 10
>> connector but when 5 has connected, it hangs. Could you tell me why?
>
> Your thread pool runs out of threads.
>
> Each SocketConnector consumes two threads from the pool, and because you use
> an unbounded queue, the pool does not grow. SocketConnectors also consume
> file descriptors, and need to be disposed properly. Failing to do so causes
> fd leakage at least in MINA 2.
>
>

Re: SocketConnector problem

Posted by Tuure Laurinolli <tu...@indagon.com>.
Michael Qi wrote:
> Hi,
> 
>   I write a simple program to test SocketConnector, I start 10
> connector but when 5 has connected, it hangs. Could you tell me why?

Your thread pool runs out of threads.

Each SocketConnector consumes two threads from the pool, and because you 
use an unbounded queue, the pool does not grow. SocketConnectors also 
consume file descriptors, and need to be disposed properly. Failing to 
do so causes fd leakage at least in MINA 2.