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 Type Method and Description void
bind()
Binds to the default local address(es) and start to accept incoming connections.void
bind(Iterable<? extends SocketAddress> localAddresses)
Binds to the specified local addresses and start to accept incoming connections.void
bind(SocketAddress... addresses)
Binds to the specified local addresses and start to accept incoming connections.void
bind(SocketAddress localAddress)
Binds to the specified local address and start to accept incoming connections.void
bind(SocketAddress firstLocalAddress, SocketAddress... addresses)
Binds to the specified local addresses and start to accept incoming connections.SocketAddress
getDefaultLocalAddress()
Returns the default local address to bind when no argument is specified inbind()
method.List<SocketAddress>
getDefaultLocalAddresses()
SocketAddress
getLocalAddress()
Returns the local address which is bound currently.Set<SocketAddress>
getLocalAddresses()
Returns aSet
of the local addresses which are bound currently.boolean
isCloseOnDeactivation()
Returns true if and only if all clients are closed when this acceptor unbinds from all the related local address (i.e.IoSession
newSession(SocketAddress remoteAddress, SocketAddress localAddress)
(Optional) Returns anIoSession
that is bound to the specified localAddress and the specified remoteAddress which reuses the local address that is already bound by this service.void
setCloseOnDeactivation(boolean closeOnDeactivation)
Sets whether all client sessions are closed when this acceptor unbinds from all the related local addresses (i.e.void
setDefaultLocalAddress(SocketAddress localAddress)
Sets the default local address to bind when no argument is specified inbind()
method.void
setDefaultLocalAddresses(Iterable<? extends SocketAddress> localAddresses)
Sets the default local addresses to bind when no argument is specified inbind()
method.void
setDefaultLocalAddresses(List<? extends SocketAddress> localAddresses)
Sets the default local addresses to bind when no argument is specified inbind()
method.void
setDefaultLocalAddresses(SocketAddress firstLocalAddress, SocketAddress... otherLocalAddresses)
Sets the default local addresses to bind when no argument is specified inbind()
method.void
unbind()
Unbinds from all local addresses that this service is bound to and stops to accept incoming connections.void
unbind(Iterable<? extends SocketAddress> localAddresses)
Unbinds from the specified local addresses and stop to accept incoming connections.void
unbind(SocketAddress localAddress)
Unbinds from the specified local address and stop to accept incoming connections.void
unbind(SocketAddress firstLocalAddress, SocketAddress... otherLocalAddresses)
Unbinds from the specified local addresses and stop to accept incoming connections.
IoConnector
Modifier and Type Method and Description ConnectFuture
connect()
Connects to thedefault remote address
.ConnectFuture
connect(IoSessionInitializer<? extends ConnectFuture> sessionInitializer)
Connects to thedefault remote address
and invokes theioSessionInitializer
when the IoSession is created but beforeIoHandler.sessionCreated(IoSession)
is invoked.ConnectFuture
connect(SocketAddress remoteAddress)
Connects to the specified remote address.ConnectFuture
connect(SocketAddress remoteAddress, IoSessionInitializer<? extends ConnectFuture> sessionInitializer)
Connects to the specified remote address and invokes theioSessionInitializer
when the IoSession is created but beforeIoHandler.sessionCreated(IoSession)
is invoked.ConnectFuture
connect(SocketAddress remoteAddress, SocketAddress localAddress)
Connects to the specified remote address binding to the specified local address.ConnectFuture
connect(SocketAddress remoteAddress, SocketAddress localAddress, IoSessionInitializer<? extendsConnectFuture> sessionInitializer)
Connects to the specified remote address binding to the specified local address and and invokes theioSessionInitializer
when the IoSession is created but beforeIoHandler.sessionCreated(IoSession)
is invoked.int
getConnectTimeout()
Deprecated.long
getConnectTimeoutMillis()
SocketAddress
getDefaultLocalAddress()
SocketAddress
getDefaultRemoteAddress()
void
setConnectTimeout(int connectTimeout)
Deprecated.void
setConnectTimeoutMillis(long connectTimeoutInMillis)
Sets the connect timeout in milliseconds.void
setDefaultLocalAddress(SocketAddress defaultLocalAddress)
Sets the default local addressvoid
setDefaultRemoteAddress(SocketAddress defaultRemoteAddress)
Sets the default remote address to connect to when no argument is specified inconnect()
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?
acceptor connector
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.
EmoticonEmoticon