我们需要有关使用Java NIO的服务器软件实现的建议
发布时间:2020-12-15 02:48:13 所属栏目:Java 来源:网络整理
导读:我正在尝试计算我必须构建的服务器上的负载. 我需要创建一个服务器,在SQL数据库中注册了一百万用户.在一周内,每个用户将大约连接3-4次.每次用户启动并下载1-30 MB数据时,可能需要1-2分钟. 上传完成后,将在几分钟内删除. (在计算中更新文本删除错误) 我知道如
我正在尝试计算我必须构建的服务器上的负载.
我需要创建一个服务器,在SQL数据库中注册了一百万用户.在一周内,每个用户将大约连接3-4次.每次用户启动并下载1-30 MB数据时,可能需要1-2分钟. 上传完成后,将在几分钟内删除. 我知道如何制作和查询SQL数据库,但在这种情况下要考虑什么? 解决方法
我正在使用Netty来实现类似的方案.它只是工作!
以下是使用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(); } } 和MyHandler类: 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(); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |