Hi there,

I used to confused on the same problem as well, so please let me share my
understanding on this.

You would only need on IoConnector if you only need one set of business
logic for handling server's response.

Let's image your IoConnector is your web browser, and IoAcceptor is a remote
website. You can use one browser to connect to many website or open many
pages of one website at the same time. (The more browsers you open, the more
resource gets consumed.)

Every time when you call IoConnector.connect(), an IoSession is created.
Just like your web browser opens a page to a website, your browser and the
remote server need to maintain a HttpSession. All further request to the
same website(IoAcceptor) can all share this session. 

Depends on the class of your protocol (stateful- you store things in the
session | stateless - nothing gets stored ), you can choose to create a
different IoSession for each working thread, or just let all the working
threads share a common one. IoSession is thread-safe, simultaneous calls
would effect each other.

It's recommended to have an ExectorFilter before your IoHandler if methods
in the handler need to do I/O operations(database accessing, socket
operation, etc time consuming tasks). 

Wish this would help you.

Cheers,
Qi






stork wrote:
> 
> I'm not sure I understand - how can a SocketConnector create more than one
> connection?
> That would explain why the connections aren't getting close properly.
> 
> Maarten Bosteels wrote:
>> 
>> Hello Alex,
>> 
>> 1) you can use one SocketConnector to create many connections
>> 
>> 2) search the forum for "Too many open files"
>> This thread might interest you:
>> http://www.nabble.com/Too-many-open-files-to7521146s16868.html
>> 
>> HTH
>> Maarten
>> 
>> On Dec 27, 2007 4:43 PM, stork <[EMAIL PROTECTED]> wrote:
>>>
>>> Hi, I'm trying to write a client that can utilize a set number of
>>> connections
>>> using MINA trunk (mina-core-2.0.0-M1-20071221.061156-110.jar).
>>> I'm using the netcat
>>> (http://svn.apache.org/viewvc/mina/trunk/example/src/main/java/org/apache/mina/example/netcat/Main.java?view=markup)
>>> as an example but I think I'm doing it wrong. I've never used NIO before
>>> so
>>> this is all a bit confusing to me.
>>>
>>> What's the preferred way of managing a lot of nio client connections
>>> using
>>> MINA? At the moment I'm just creating a lot of NioSocketConnector
>>> objects,
>>> and connecting them. I have added an IoFutureListener to the
>>> ConnectFuture
>>> obtained by calling nioSocketConnector.connect(InetSocketAddress):
>>>         public void operationComplete(IoFuture arg0) {
>>>                 if(arg0 instanceof ConnectFuture) {
>>>                         ConnectFuture cf = (ConnectFuture)arg0;
>>>                        
>>> debug("connected="+cf.isConnected()+",exc="+cf.getException());
>>>                         if(cf.isConnected()) {
>>>                                 /**
>>>                                  * Add the connected client to the
>>> clients queue for processing
>>>                                  */
>>>                                 boolean offered =
>>> clients.offer((TyrClient)cf.getSession().getAttribute("CLIENT"));
>>>                                 if(!offered)
>>>                                         throw new
>>> RuntimeException("Couldn't offer connected client.");
>>>                         } else {
>>>                                 /**
>>>                                  * Add a new connection attempt to the
>>> connection queue.
>>>                                  */
>>>                                 boolean offered =
>>> pending.offer(createNewConnection(TIMEOUT));
>>>                                 if(!offered)
>>>                                         throw new
>>> RuntimeException("Couldn't offer connection.");
>>>                         }
>>>                 }
>>>     }
>>>
>>> And I'm getting a lot of this:
>>> [T:NioSocketConnector-75] connected=false,exc=java.net.ConnectException:
>>> Connection refused
>>> [T:NioSocketConnector-76] connected=false,exc=java.net.ConnectException:
>>> Connection refused
>>> [T:NioSocketConnector-77] connected=false,exc=java.net.ConnectException:
>>> Connection refused
>>> (Which is being expected since I'm just connecting to a closed port)
>>> and then,
>>> 7693 [NioSocketConnector-77] WARN
>>> org.apache.mina.common.DefaultExceptionMonitor - Unexpected exception.
>>> org.apache.mina.common.RuntimeIoException: Failed to initialize.
>>>         at
>>> org.apache.mina.common.AbstractPollingIoConnector.<init>(AbstractPollingIoConnector.java:103)
>>>         at
>>> org.apache.mina.common.AbstractPollingIoConnector.<init>(AbstractPollingIoConnector.java:61)
>>>         at
>>> org.apache.mina.transport.socket.nio.NioSocketConnector.<init>(NioSocketConnector.java:52)
>>>         at tyr.Client.createNewConnection(RickRoller.java:46)
>>>         at tyr.Client.operationComplete(RickRoller.java:154)
>>>         at
>>> org.apache.mina.common.DefaultIoFuture.notifyListener(DefaultIoFuture.java:302)
>>>         at
>>> org.apache.mina.common.DefaultIoFuture.notifyListeners(DefaultIoFuture.java:287)
>>>         at
>>> org.apache.mina.common.DefaultIoFuture.setValue(DefaultIoFuture.java:221)
>>>         at
>>> org.apache.mina.common.DefaultConnectFuture.setException(DefaultConnectFuture.java:95)
>>>         at
>>> org.apache.mina.common.AbstractPollingIoConnector.processSessions(AbstractPollingIoConnector.java:262)
>>>         at
>>> org.apache.mina.common.AbstractPollingIoConnector.access$500(AbstractPollingIoConnector.java:41)
>>>         at
>>> org.apache.mina.common.AbstractPollingIoConnector$Worker.run(AbstractPollingIoConnector.java:298)
>>>         at
>>> org.apache.mina.util.NamePreservingRunnable.run(NamePreservingRunnable.java:51)
>>>         at
>>> java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
>>>         at
>>> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
>>>         at java.lang.Thread.run(Thread.java:619)
>>> Caused by: java.io.IOException: Too many open files
>>>         at sun.nio.ch.IOUtil.initPipe(Native Method)
>>>         at
>>> sun.nio.ch.EPollSelectorImpl.<init>(EPollSelectorImpl.java:49)
>>>         at
>>> sun.nio.ch.EPollSelectorProvider.openSelector(EPollSelectorProvider.java:18)
>>>         at java.nio.channels.Selector.open(Selector.java:209)
>>>         at
>>> org.apache.mina.transport.socket.nio.NioSocketConnector.init(NioSocketConnector.java:69)
>>>         at
>>> org.apache.mina.common.AbstractPollingIoConnector.<init>(AbstractPollingIoConnector.java:98)
>>>         ... 15 more
>>>
>>>
>>> So what am I doing wrong?
>>>
>>> Thanks,
>>> Alex.
>>> --
>>> View this message in context:
>>> http://www.nabble.com/Using-a-bunch-of-connections-in-a-client--tp14514544s16868p14514544.html
>>> Sent from the Apache MINA Support Forum mailing list archive at
>>> Nabble.com.
>>>
>>>
>> 
>> 
> 
> 

-- 
View this message in context: 
http://www.nabble.com/Using-a-bunch-of-connections-in-a-client--tp14514544s16868p14520639.html
Sent from the Apache MINA Support Forum mailing list archive at Nabble.com.

Reply via email to