Apache MINA Connector, Acceptor

Apache MINA connector VS acceptor


Apache MINA connector, acceptor

If you use Apache MINA, you must use acceptor or connector. 
In my case, I use MINA for my project. At middle-server, it needs many connection for message sending and message receiving. I struggle with this problem because I didn't know about difference of connector, acceptor. 
You can see many documents and APIs and description about MINA. In there I thought "ah? these two are not same thing?" you might think like me. because these two things are different. now I'll explain.

Similarity of connector & acceptor

From wiki about MINA we can see about acceptor, connector (thread model wiki)

  • Acceptor thread accepts incoming connections, and forwards the connection to the I/O processor thread for read and write operations.
    • Each SocketAcceptor creates one acceptor thread. You can't configure the number of the acceptor threads.


  • Connector thread attempts connections to a remote peer, and forwards the succeeded connection to the I/O processor thread for read and write operations.
    • Each SocketConnector creates one connector thread. You can't configure the number of the connector threads, either.



If you look at the wiki, you can know acceptor and connector are thread, and both of them do I/O process. only difference is that acceptor accepts connection, On the contrary connector attempts connection.
What's the common thing? If you use both class(interface) or check API, you might know many common thing. Let's see API
You can see common Superinterface IoService, most of important method like getFilterChain, getHandler, setHandler in IoService. When You right codes, many cases you can select one of them. It works.


differences of connector and acceptor

Alike similarity, we also see API

IoAcceptor

Modifier and TypeMethod and Description
voidbind()
Binds to the default local address(es) and start to accept incoming connections.
voidbind(Iterable<? extends SocketAddress> localAddresses)
Binds to the specified local addresses and start to accept incoming connections.
voidbind(SocketAddress... addresses)
Binds to the specified local addresses and start to accept incoming connections.
voidbind(SocketAddress localAddress)
Binds to the specified local address and start to accept incoming connections.
voidbind(SocketAddress firstLocalAddress, SocketAddress... addresses)
Binds to the specified local addresses and start to accept incoming connections.
SocketAddressgetDefaultLocalAddress()
Returns the default local address to bind when no argument is specified in bind() method.
List<SocketAddress>getDefaultLocalAddresses()
Returns a List of the default local addresses to bind when no argument is specified in bind() method.
SocketAddressgetLocalAddress()
Returns the local address which is bound currently.
Set<SocketAddress>getLocalAddresses()
Returns a Set of the local addresses which are bound currently.
booleanisCloseOnDeactivation()
Returns true if and only if all clients are closed when this acceptor unbinds from all the related local address (i.e.
IoSessionnewSession(SocketAddress remoteAddress, SocketAddress localAddress)
(Optional) Returns an IoSession that is bound to the specified localAddress and the specified remoteAddress which reuses the local address that is already bound by this service.
voidsetCloseOnDeactivation(boolean closeOnDeactivation)
Sets whether all client sessions are closed when this acceptor unbinds from all the related local addresses (i.e.
voidsetDefaultLocalAddress(SocketAddress localAddress)
Sets the default local address to bind when no argument is specified in bind() method.
voidsetDefaultLocalAddresses(Iterable<? extends SocketAddress> localAddresses)
Sets the default local addresses to bind when no argument is specified in bind() method.
voidsetDefaultLocalAddresses(List<? extends SocketAddress> localAddresses)
Sets the default local addresses to bind when no argument is specified in bind() method.
voidsetDefaultLocalAddresses(SocketAddress firstLocalAddress, SocketAddress... otherLocalAddresses)
Sets the default local addresses to bind when no argument is specified in bind() method.
voidunbind()
Unbinds from all local addresses that this service is bound to and stops to accept incoming connections.
voidunbind(Iterable<? extends SocketAddress> localAddresses)
Unbinds from the specified local addresses and stop to accept incoming connections.
voidunbind(SocketAddress localAddress)
Unbinds from the specified local address and stop to accept incoming connections.
voidunbind(SocketAddress firstLocalAddress, SocketAddress... otherLocalAddresses)
Unbinds from the specified local addresses and stop to accept incoming connections.


IoConnector

Modifier and TypeMethod and Description
ConnectFutureconnect()
Connects to the default remote address.
ConnectFutureconnect(IoSessionInitializer<? extends ConnectFuture> sessionInitializer)
Connects to the default remote address and invokes the ioSessionInitializer when the IoSession is created but before IoHandler.sessionCreated(IoSession) is invoked.
ConnectFutureconnect(SocketAddress remoteAddress)
Connects to the specified remote address.
ConnectFutureconnect(SocketAddress remoteAddress, IoSessionInitializer<? extends ConnectFuture> sessionInitializer)
Connects to the specified remote address and invokes the ioSessionInitializer when the IoSession is created but before IoHandler.sessionCreated(IoSession) is invoked.
ConnectFutureconnect(SocketAddress remoteAddress, SocketAddress localAddress)
Connects to the specified remote address binding to the specified local address.
ConnectFutureconnect(SocketAddress remoteAddress, SocketAddress localAddress, IoSessionInitializer<? extendsConnectFuture> sessionInitializer)
Connects to the specified remote address binding to the specified local address and and invokes the ioSessionInitializer when the IoSession is created but before IoHandler.sessionCreated(IoSession) is invoked.
intgetConnectTimeout()
Deprecated.  
longgetConnectTimeoutMillis() 
SocketAddressgetDefaultLocalAddress() 
SocketAddressgetDefaultRemoteAddress() 
voidsetConnectTimeout(int connectTimeout)
Deprecated.  
voidsetConnectTimeoutMillis(long connectTimeoutInMillis)
Sets the connect timeout in milliseconds.
voidsetDefaultLocalAddress(SocketAddress defaultLocalAddress)
Sets the default local address
voidsetDefaultRemoteAddress(SocketAddress defaultRemoteAddress)
Sets the default remote address to connect to when no argument is specified in connect() method.

What's the difference between them?
You just might find (un)bind, and connect
Yes! Right! Most important thing is that acceptor has (un)bind, connector has connect. So.. what's different?

acceptorconnector

used by server commonly
use bind()
(do not have connect)
passive - ready for connecting by client, If client don't connect, nothing can do  


used by client commonly
use connect()
(do not have bind)
active - client have to connect itself, If target server don't have socket, fails(error)
[notice] commonly acceptor use at server, connector use at client.
[notice] connector do itself, acceptor wait until connect.
First