Search code examples
javasqlmultithreadingnio

We need advice for a server software implementation with Java NIO


I'm trying to calculate the load on a server I have to build.

I need to create a server witch have one million users registered in an SQL database. During a week each user will approximately connect 3-4 times. Each time a user will up and download 1-30 MB data, and it will take maybe 1-2 minutes.

When an upload is complete it will be deleted within minutes. (Update text removed error in calculations)

I know how to make and query an SQL database but what to consider in this situation?


Solution

  • I am using Netty for a similar scenario. It is just working!

    Here is a starting point for using netty:

    public class TCPListener {
        private static ServerBootstrap bootstrap;
    
        public static void run(){
            bootstrap = new ServerBootstrap(
                    new NioServerSocketChannelFactory(
                            Executors.newCachedThreadPool(),
                            Executors.newCachedThreadPool()));
    
            bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
                public ChannelPipeline getPipeline() throws Exception {
                    TCPListnerHandler handler = new MyHandler();
                    ChannelPipeline pipeline = Channels.pipeline();
                    pipeline.addLast("handler", handler);
    
                    return pipeline;
                }
            });
    
            bootstrap.bind(new InetSocketAddress(9999));  //port number is 9999
        }
    
        public static void main(String[] args) throws Exception {
            run();
        }
    }
    

    and MyHandler class:

    public class MyHandler extends SimpleChannelUpstreamHandler {
        @Override
        public void messageReceived(
            ChannelHandlerContext ctx, MessageEvent e) {
    
    
            try {
                String remoteAddress = e.getRemoteAddress().toString();
                ChannelBuffer buffer= (ChannelBuffer) e.getMessage();
                //Now the buffer contains byte stream from client.
    
            } catch (UnsupportedEncodingException ex) {
                ex.printStackTrace();
            }
    
            byte[] output; //suppose output is a filled byte array
            ChannelBuffer writebuffer = ChannelBuffers.buffer(output.length);
            for (int i = 0; i < output.length; i++) {
                writebuffer.writeByte(output[i]);
            }
    
    
            e.getChannel().write(writebuffer);
        }
    
        @Override
        public void exceptionCaught(
                ChannelHandlerContext ctx, ExceptionEvent e) {
            // Close the connection when an exception is raised.
            e.getChannel().close();
        }
    }