Although not strictly necessary, I would have the expectation that,
since MINA is based on NIO, it should be possible to build a single
executable that listens on a port and connects to itself on that port,
all from a single thread. Is this possible at all?
I ask because I have been toying with the Reverser example and there are
a few things I don't quite understand. I have modified the
log4j.properties file to have the thread name printed (using a
ConversionPattern with value "%t %p[%c{1}] %m%n") and added some logging
to the server under the following conditions:
* Start up and accept connections on port 8888.
* Wait for a message with the text "shutdown".
* Shut server down.
The shutdown process is implemented with a listener on a CloseFuture on
the session, as discussed on this list a few days ago. Next you will
find a short log for an example where the client is a telnet session to
port 8888; the first word on each line is the thread name:
main INFO[Main] => Starting up
main INFO[Main] => Acceptor created
main INFO[Main] => Acceptor configured handler
main INFO[Main] => Acceptor bound
main INFO[Main] => Node listening on port 8888
main INFO[Main] => Finished
NioProcessor-1 INFO[ReverseProtocolHandler] => Session created
NioProcessor-1 INFO[ReverseProtocolHandler] => Session opened
NioProcessor-1 INFO[ReverseProtocolHandler] => Session received:
[shutdown]
NioProcessor-1 INFO[ReverseProtocolHandler] => Session creating
closeFuture
NioProcessor-1 INFO[ReverseProtocolHandler] => Session adding listener
to closeFuture
NioProcessor-1 INFO[ReverseProtocolHandler] => Session waiting for
closeFuture
NioProcessor-1 INFO[ReverseProtocolHandler] => Session caught an
exception
NioProcessor-1 INFO[ReverseProtocolHandler] => Session done closing,
will now shutdown
NioProcessor-1 INFO[Main] => Node shutting down
NioProcessor-1 INFO[Main] => Acceptor unbound
NioProcessor-1 INFO[Main] => Acceptor disposed
NioProcessor-1 INFO[ReverseProtocolHandler] => Session closed
Now, it is obvious from this log that there are at least two threads at
play here: a main thread that ends up right away and a secondary thread
NioProcessor-1 where all the session management happens. Why is this so?
Is it because the main thread simply falls off the end of the earth?
Just as a reminder, this is the code for the main routine:
public static void main(String[] args) throws Exception {
LOGGER.info("=> Starting up");
acceptor = new NioSocketAcceptor();
LOGGER.info("=> Acceptor created");
// Prepare the configuration
DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
chain.addLast("codec",
new ProtocolCodecFilter(
new
TextLineCodecFactory(Charset.forName("UTF-8"))));
acceptor.setHandler(new ReverseProtocolHandler());
LOGGER.info("=> Acceptor configured handler");
acceptor.bind(new InetSocketAddress(PORT));
LOGGER.info("=> Acceptor bound");
LOGGER.info("=> Node listening on port " + PORT);
LOGGER.info("=> Finished");
}
Should there be an instruction after the acceptor.bind() call that
somehow waits until the acceptor is done? Perhaps Reverser this is not
the best example to be looking at?
Thanks for any light shed on my ignorance. Best regards,
--
Gonzalo Diethelm
DCV - Chile