`
san_yun
  • 浏览: 2693203 次
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

netty初探

阅读更多

netty API

//启动类

 ServerBootstrap bootstrap = new
  ServerBootstrap( NioServerSocketChannelFactory(new
 Executors.newCachedThreadPool()),Executors.newCachedThreadPool());

//关键的部分,Set up the event pipeline factory.

bootstrap.setPipelineFactory(new
 WebSocketServerPipelineFactory());

// Bind and start to accept incoming connections.

 bootstrap.bind(new
 InetSocketAddress(8080));

WebSocketServerPipelineFactory部分:

public
 class WebSocketServerpipelineFactory implements
 ChannelPipelineFactory{

	public
 ChannelPipeline getPipeline(){
                  //创建一个默认的pipeline implemenation.

                  ChannelPipeline pipeline = pipeline();
                  pipeline.addLast("decoder"
,new
 HttpRequestDecoder());
                  pipeline.addLast("aggregator"
,new
 HttpChunkAggregator(65536));
		  pipeline.addLast("encoder"
,new
 HttpResponseEncoder());
                  pipeline.addLast("handler"
,new
 WebSocketServerhandler());

       }
}

WebSocketServerhandler部分:

public
 class WebSocketServerHandler extends
 SimpleChannelUpstreamHandler {
 
  private
 static
 final
 String
 WEBSOCKET_PATH = "/websocket"
;

 public
 void messageReceived(ChannelHandlerContext ctx, MessageEvent evt) {  
    Object
 message = evt.getMessage();  
    // Do something with the received message.  

    ...  
    // And forward the event to the next handler.  

    ctx.sendUpstream(evt);  
 }
}

netty 文档

http://www.jboss.org/netty/documentation.html
http://jef.javaeye.com/blog/544206

分享到:
评论

相关推荐

    Netty初探:掌握高性能网络通信框架,提升Java网络编程技能

    通过阅读《Netty初探:掌握高性能网络通信框架,提升Java网络编程技能》这本书,你将深入了解这些概念,并学会如何运用Netty构建实际的网络应用,提升你的Java编程技能。这本书会逐步引导你从基础知识到高级特性的...

    gRPC框架初探

    ### gRPC框架初探 #### 一、gRPC框架简介 gRPC是一个高效的远程过程调用(Remote Procedure Call,简称RPC)框架,由Google开发并开源。它支持多种编程语言,如C++、Java、Python、Go等,并利用HTTP/2进行传输层...

    基于JavaDisruptor的并发编程深度学习项目.zip

    通过本项目,你将学习到并发编程的核心概念、无锁并行计算框架的基础使用与高级特性,以及如何整合Disruptor与Netty构建高性能的网络应用。 目录结构 disruptorapi包含Disruptor框架的基础使用和高级特性示例...

    基于Spring Boot和LMAX Disruptor的高性能并发框架.zip

    项目涵盖了并发编程的核心概念、无锁并行计算框架的使用、高级特性、底层源码分析以及与Netty的整合实战。通过本项目,开发者可以深入理解并发编程的原理,掌握Disruptor框架的使用,并能够构建高性能的并发应用。 ...

    简单的分布式RPC框架blackRpc

    ② 框架组成:spring,netty,zookeeper 序列化方式支持:fastjson,msgpack,protostuff 集群负载均衡策略:轮训,加权轮训,随机,加权随机,一致性哈希 ③ 支持spring多种作用域 ④ 该框架比较适合初探分布式RPC原理...

    dubbo-demo.rar

    在实际应用中,根据性能需求,我们可能会选择更高效的如Dubbo的Netty或HTTP协议。 实施步骤如下: - **创建服务接口**:在"dubbo-api"模块中,定义服务接口,例如`UserService.java`,声明服务方法。 - **实现...

Global site tag (gtag.js) - Google Analytics