Netty 整合 SpringMVC Netty_SpringMVC

码农软件 · 软件分类 · 并发/并行处理框架 · 2019-09-02 15:27:23

软件介绍

Netty 整合 SpringFramework 代码示例。

启动类代码:

package  cn.ranko.api;
import  cn.ranko.framework.core.RankoServer;
/**
  *  Created  by  zhujun  on  2016/8/11.
  *
  *  测试地址:http://localhost:8200/api-demo/test.action
  *  内容返回:{"demoId":1,"demoStr":"test1"}
  *
  */
public  class  ApiServer  {
        private  static  RankoServer  server;
        public  static  void  main(String[]  args)  throws  Exception  {
                int  serverPort  =  Integer.parseInt(args.length  >  0  &&  args[0]  !=  null  ?  args[0]  :  "8200");
                server  =  new  RankoServer(serverPort);
                server.start();
        }
}

整合代码:

package  cn.ranko.framework.core;
import  io.netty.bootstrap.ServerBootstrap;
import  io.netty.channel.*;
import  io.netty.channel.nio.NioEventLoopGroup;
import  io.netty.channel.socket.SocketChannel;
import  io.netty.channel.socket.nio.NioServerSocketChannel;
import  io.netty.handler.codec.http.HttpObjectAggregator;
import  io.netty.handler.codec.http.HttpRequestDecoder;
import  io.netty.handler.codec.http.HttpResponseEncoder;
import  io.netty.handler.stream.ChunkedWriteHandler;
import  org.apache.log4j.Logger;
import  org.springframework.mock.web.MockServletContext;
import  javax.servlet.ServletException;
/**
  *  Created  by  zhujun  on  2016/6/20.
  */
public  class  RankoServer  {
        private  Logger  logger  =  Logger.getLogger(getClass().getName());
        private  int  serverPort;
        private  ServerBootstrap  bootstrap;
        public  RankoServer(int  serverPort)  {
                this.serverPort  =  serverPort;
        }
        public  void  start()  throws  InterruptedException,  ServletException  {
                if  (this.bootstrap  !=  null)  {
                        throw  new  IllegalStateException("Server  is  started,  please  do  not  repeat");
                }
                bootstrap  =  new  ServerBootstrap();
                ApplicationContextHolder.init();
                bootstrap.group(new  NioEventLoopGroup(),  new  NioEventLoopGroup()).channel(
                                NioServerSocketChannel.class).childHandler(new  ChannelInitializer<SocketChannel>()  {
                        @Override
                        protected  void  initChannel(SocketChannel  socketChannel)  throws  Exception  {
                                ChannelPipeline  pipeline  =  socketChannel.pipeline();
                                pipeline.addLast("decoder",  new  HttpRequestDecoder());
                                pipeline.addLast("aggregator",  new  HttpObjectAggregator(65536));  //  上传限制3M
                                pipeline.addLast("encoder",  new  HttpResponseEncoder());
                                pipeline.addLast("chunkedWriter",  new  ChunkedWriteHandler());
                                pipeline.addLast("handler",new  DispatcherServletHandler(ApplicationContextHolder.getDispatcherServlet()));
                        }
                });
                ChannelFuture  f  =  bootstrap.bind(this.serverPort).sync();//  配置完成,开始绑定server,通过调用sync同步方法阻塞直到绑定成功
                f.channel().closeFuture().sync();//  应用程序会一直等待,直到channel关闭
                logger.info("server  listens  to  port  "  +  this.serverPort);
        }
}

本文地址:https://codercto.com/soft/d/13722.html

科学计算导论

科学计算导论

希思 / 清华大学出版社 / 2005-10 / 48.00元

本书全面地介绍了科学计算中解各种主要问题的数值方法,包括线性和非线性方程、最小二乘法、特征值、最优化、插值、积分、常微分方程和偏微分方程、快速傅里叶变换和随机数生成。 本书的特点是: 以使用算法的读者为对象,重点讲授算法背后的思想和原理,而不是算法的详细分析。 强调敏感性和病态性等概念,对同一问题的不同算法进行比较和评价,提高读者对算法的鉴赏能力。 对每类......一起来看看 《科学计算导论》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

URL 编码/解码
URL 编码/解码

URL 编码/解码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具