`

Netty笔记一(可以运行看到结果的简单例子)

阅读更多

最近一段时间一直研究聊天程序,学习了nio的知识,以后会和大家分享的,今天写了一个可以运行看到结果的netty helloworld程序,这个程序虽然简单,但从这个程序可以知道netty的运行流程,希望对初学者有所帮助。

第一步:建立一个maven项目,或者读者可以自己导入netty依赖包

<dependency>
      <groupId>io.netty</groupId>
      <artifactId>netty</artifactId>
      <version>3.5.6.Final</version>
 </dependency>

 现在netty 4.0已经出来了,因为3.x版本的资料比较多有利于学习,先从3.x开始了,以后会继续出4.x的学习笔记的,呵呵,上程序了

第二步:复制下面代码到项目中

 

package com.my.day1;

import java.net.InetSocketAddress;
import java.util.concurrent.Executors;

import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import org.jboss.netty.handler.codec.string.StringDecoder;
import org.jboss.netty.handler.codec.string.StringEncoder;

/**   
 * @Title: NettyDemo.java 
 * @Package com.my.day1 
 * @Description: 一个类完整运行netty,运行就能看到结果
 * @author jimmy lovelyxuehanxin@163.com   
 * @date 2013-4-16 下午9:38:29 
 */
public class NettyDemo {
	public static void main(String[] args) {
		NioServer server = new NioServer();
	}
}

class NioServer{
	ServerBootstrap bootstrap;
	Channel parentChannel;
	InetSocketAddress localAddress;
	MyChannelHandler channelHandler = new MyChannelHandler();
	public NioServer(){
	bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(
			Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
	
	
	ChannelPipeline pineline = bootstrap.getPipeline();
	// String格式字符串解码写的时候都会经过这个过滤器处理
    pineline.addLast("encode", new StringEncoder());
    // 接受信息的时候会被处理
	pineline.addLast("decode", new StringDecoder());
	// 自定义处理类,我们的业务一般从这个类开始
	pineline.addLast("servercnfactory", channelHandler);
	bootstrap.bind(new InetSocketAddress(8080));
	}
	
}
// 处理channel中的数据,SimpleChannelHandler帮我们实现好了很多有用户的方法这里就只重写了几个方法
class MyChannelHandler extends SimpleChannelHandler{

	// netty默认信息接受入口
	@Override
	public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
			throws Exception {
		System.out.println("接受到的信息" + e.getMessage());
		// 返回信息可以在dos对话框中看到自己输的内容
		e.getChannel().write(e.getMessage());
		super.messageReceived(ctx, e);
	}

	@Override
	public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e)
			throws Exception {
		System.out.println("channel Connected......");
		super.channelConnected(ctx, e);
	}

	@Override
	public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e)
			throws Exception {
		System.out.println("channelClosed");
		super.channelClosed(ctx, e);
	}
	
}

 第三步:运行上面的代码,cmd打开dos窗口输入telnet 127.0.0.1 8080 输入内容 程序就可以打印你输入的内容并且可以在dos命令框里打印出你输入的内容。

 

总结:这个列子很简单,可以清楚程序怎么运行的,对理解netty很有帮助

 

 

9
5
分享到:
评论
1 楼 lianglong2000 2013-12-30  
非常感谢!

相关推荐

Global site tag (gtag.js) - Google Analytics