微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Netty_SpringMVC Netty 整合 SpringMVC

程序名称:Netty_SpringMVC

授权协议: GPL

操作系统: 跨平台

开发语言: Java

Netty_SpringMVC 介绍

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);
        }
}

Netty_SpringMVC 官网

http://git.oschina.net/xmtom/share/tree/master/netty_springmvc

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐