歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Netty使用實例

Netty使用實例

日期:2017/3/1 9:53:24   编辑:Linux編程

貼上兩個自己寫好的Netty使用實例,以便備注,以下兩個例子基於netty-3.5.7.Final.jar用Junit進行測試

Netty 的詳細介紹:請點這裡
Netty 的下載地址:請點這裡

相關閱讀:Netty源碼學習筆記 http://www.linuxidc.com/Linux/2013-09/89778.htm

第一個例子:簡單的發送字符串,接收字符串“Hello, World”

package com.wujintao.netty;

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

import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import org.jboss.netty.handler.codec.string.StringDecoder;
import org.jboss.netty.handler.codec.string.StringEncoder;
import org.junit.Test;

class HelloWorldServerHandler extends SimpleChannelHandler {
public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e)
throws Exception {
e.getChannel().write("Hello, World");
}

public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
System.out.println("Unexpected exception from downstream."
+ e.getCause());
e.getChannel().close();
}
}

class HelloWorldClientHandler extends SimpleChannelHandler {

public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
String message = (String) e.getMessage();
System.out.println(message);
e.getChannel().close();
}

public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
System.out.println("Unexpected exception from downstream."
+ e.getCause());
e.getChannel().close();
}
}


/**
* Netty VS MinaNetty基於Pipeline處理,Mina基於Filter過濾
* Netty的事件驅動模型具有更好的擴展性和易用性
* Https,SSL,PB,RSTP,Text &Binary等協議支持
* Netty中UDP傳輸有更好的支持官方測試Netty比Mina性能更好
* @author Administrator
*
*/
public class TestCase {

public void testServer() {
//初始化channel的輔助類,為具體子類提供公共數據結構
ServerBootstrap bootstrap = new ServerBootstrap(
new NioServerSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()));
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
public ChannelPipeline getPipeline() {
ChannelPipeline pipeline = Channels.pipeline();
pipeline.addLast("decoder", new StringDecoder());
pipeline.addLast("encoder", new StringEncoder());
pipeline.addLast("handler", new HelloWorldServerHandler());
return pipeline;
}
});
//創建服務器端channel的輔助類,接收connection請求
bootstrap.bind(new InetSocketAddress(8080));
}



public void testClient() {
//創建客戶端channel的輔助類,發起connection請求
ClientBootstrap bootstrap = new ClientBootstrap(
new NioClientSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()));
//It means one same HelloWorldClientHandler instance is going to handle multiple Channels and consequently the data will be corrupted.
//基於上面這個描述,必須用到ChannelPipelineFactory每次創建一個pipeline
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
public ChannelPipeline getPipeline() {
ChannelPipeline pipeline = Channels.pipeline();
pipeline.addLast("decoder", new StringDecoder());
pipeline.addLast("encoder", new StringEncoder());
pipeline.addLast("handler", new HelloWorldClientHandler());
return pipeline;
}
});
//創建無連接傳輸channel的輔助類(UDP),包括client和server
ChannelFuture future = bootstrap.connect(new InetSocketAddress(
"localhost", 8080));
future.getChannel().getCloseFuture().awaitUninterruptibly();
bootstrap.releaseExternalResources();
}


@Test
public void testNetty(){
testServer();
testClient();
}

}

第二個例子見下一頁:http://www.linuxidc.com/Linux/2013-09/89779p2.htm

Copyright © Linux教程網 All Rights Reserved