On Nov 22, 2007 3:24 AM, Frédéric Brégier <[EMAIL PROTECTED]> wrote:
> Hello all !
>
> Well, after some rewriting (but no test up to now), I get the following
> bootstraping code.
> I know that it could be dizzy to help on this kind of "very long" code,
> but what I am thinking is that such a code in a manual web page of Mina
> could help, of course, once we correct it (since I'm sure it is not perfect).
>
> So here it is, beginning by the server part:
> // Create service
> NioSocketAcceptor acceptor = new NioSocketAcceptor(myNumberOfCpuPlusOne);
> // It implies "myNumberOfCpuPlusOne" IoAcceptor using an internal
> newCachedThreadPool
>
> SocketSessionConfig socketSessionConfig = (SocketSessionConfig)
> acceptor.getSessionConfig();
> socketSessionConfig.setReuseAddress(true);
> //XXX VISTA BUG ? => I need to remove setSoLinger(x) and KeepAlive(true)
> socketSessionConfig.setSoLinger(1);
> socketSessionConfig.setKeepAlive(false); //Not sure
> socketSessionConfig.setTcpNoDelay(true);
> socketSessionConfig.setReceiveBufferSize(myTCPBLOCKSIZE);
> socketSessionConfig.setSendBufferSize(myTCPBLOCKSIZE);
> socketSessionConfig.setIdleTime(IdleStatus.BOTH_IDLE, myTIMEOUT);
>
> // Create IoHandler (I am using an extension of DemuxingIoHandler)
> MyDemuxingIoHandler myDemuxingIoHandler = new MyDemuxingIoHandler();
> // Create for each class one message handler
> // MyMessage
> MyMessageHandler myMessageHandler = new MyMessageHandler();
> myDemuxingIoHandler.addMessageHandler(MyMessage.class, myMessageHandler);
> ...
>
> // Configuration of the filter chain
> DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
> // Compression
> CompressionFilter compression =
> new CompressionFilter(COMPRESSIONCLIENT,
> COMPRESSIONSERVER,
> CompressionFilter.COMPRESSION_MAX);
> chain.addLast("Compression",compression);
> // Serialization : I add a pool of Executor since serizalisation is not free
> of CPU and after that my protocol
> // implies some extra codes (like JDBC calls)
> ProtocolCodecFactory codec = new ObjectSerializationCodecFactory();
> // Was Executors.newCachedThreadPool();
> ExecutorService executorSerialize = new
> OrderedThreadPoolExecutor(LSDConstants.SERVER_THREAD);
> chain.addLast("SerializePool", new ExecutorFilter(executorSerialize));
> chain.addLast("Serialize", new ProtocolCodecFilter(codec));
>
> // Set port
> InetSocketAddress inetSocketAddress = new InetSocketAddress(mySERVER_PORT);
>
>
> // All together
> acceptor.setLocalAddress(inetSocketAddress);
> acceptor.setHandler(myDemuxingIoHandler);
>
> // When Unbind, do not disconnect already connected client
> acceptor.setDisconnectOnUnbind(false);
> try {
> acceptor.bind();
> } catch (IOException e) {
> myLogger.error("Cannot Bind Service "+myname, e);
> return false;
> }
>
> When shutting down:
> // I use a complicated way (which I do not include here since too long)
> // to wait that all sessions are really closed before exiting the acceptor
> // (looping on ioAcceptor.getManagedSessions() to see if requests are pending)
> // But if you know a good way to do that (clean shutdown), I will take it !
> // Then realy closed if any session are still opened
> acceptor.setDisconnectOnUnbind(true);
> acceptor.unbind();
> if (executorSerialize != null) {
> executorSerialize.shutdownNow();
> executorSerialize = null;
> }
> // Did I forgot something ?
You forgot to call acceptor.dispose(). Except that, it looks perfect!
Cheers,
Trustin
--
what we call human nature is actually human habit
--
http://gleamynode.net/
--
PGP Key ID: 0x0255ECA6