- 浏览: 1237294 次
- 性别:
- 来自: 杭州
文章分类
- 全部博客 (399)
- 心情故事 (12)
- java (115)
- linux (55)
- 关系型数据库 (35)
- struts,jsf,spring (11)
- jdbc,hibernate,ibatis (11)
- jsp,jstl,c:tag,标签库 (2)
- ejb,webservice (1)
- tomcat,jboss,jetty,weblogic,websphere (15)
- java网络编程 (6)
- java线程 (0)
- js,jquery,json,xml,dom,html.regex (25)
- 设计模式 (6)
- BUG记录 (2)
- ant (2)
- jsp,servlet (4)
- swing (6)
- lucene+nutch (6)
- log4j (2)
- windows doc (2)
- ruby (1)
- ruby on rails (3)
- 操作系统网络 (18)
- apache 错误 (1)
- tomcat (10)
- jboss (9)
- jetty (9)
- weblogic (9)
- websphere (10)
- apache (2)
- AIX的iostat命令查看系统磁盘的使用情况 (1)
- oracle 统计一个表格有多少列 (1)
- Exception in thread "main" java.security.KeyStoreException: Windows-MY not found (1)
- jsp (1)
- jstl (1)
- c:tag (1)
- 标签库 (1)
- struts (1)
- jsf (1)
- spring (2)
- oracle,sqlplus (2)
- sqlplus (2)
- show errors (1)
- proc (1)
- function (1)
- ORA-06544: PL/SQL: internal error (1)
- arguments: [55916] (1)
- [] (7)
- 终端身份实施文档 (1)
- 重装系统之后飞鸽传书只能看到自己 (1)
- vsftp "上传 553 Could not create file" (1)
- startWebLogic.sh启动失败,提示Error initializing Embedded LDAP Server (1)
- java agent 注册为 windows 服务 (1)
- centos (1)
- svn (1)
- apr (1)
- apr-util (1)
- activemq (2)
- oracle (5)
- mysql (3)
- nosql (3)
- NSIS (1)
- windows wmic (1)
- c 指针 (1)
- c c++ (0)
- jmeter (0)
- 性能测试 (0)
- linux,备份 (2)
- C++ ,Virtual (1)
- windows dos (1)
- android (2)
- 大数据,云计算 (1)
- JVM垃圾收集 (1)
- jdbc (2)
- invoke (1)
- hibernate (1)
- ibatis (1)
- 个人开源项目源码收藏 (1)
- 批处理 (1)
- Mongodb mapreduce (8)
- kettle (1)
- Mongodb capped (1)
- mongodb gridfs (1)
- Mongodb 入门基础知识 (1)
- mongodb (8)
- hadoop2.5.1 (1)
- hadoop (4)
- eclipse (1)
- hdfs fs (1)
- elipse hadoop plugin (1)
- PHP相关知识 (1)
- js (1)
- jquery (1)
- json (1)
- xml (1)
- dom (1)
- html.regex (1)
- 网络知识 (1)
- nginx (1)
- docker (1)
- 测试 (1)
- nodejs (1)
- iptables (1)
- linux gitlab (1)
最新评论
-
July01:
最近了解到一款StratoIO打印控件,功能如下:1、Html ...
web页面调用window.print()函数实现打印的功能 -
hxdtech:
非常感谢!
我在学习ibatis时的培训ppt -
zmwxiaoming:
what 能连数据库不错
SOLR的学习整理 -
springdata_springmvc:
java程序语言学习教程 地址http://www.zuida ...
java获取当前操作系统的信息 -
huanzei:
整理的不错,
oracle lpad函数
客户端
package com.mchz.netty.test.client; import java.net.InetSocketAddress; import java.util.concurrent.Executors; import org.jboss.netty.bootstrap.ClientBootstrap; import org.jboss.netty.channel.ChannelFuture; import org.jboss.netty.channel.ChannelPipeline; import org.jboss.netty.channel.ChannelPipelineFactory; import org.jboss.netty.channel.Channels; import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory; /** * Sends one message when a connection is open and echoes back any received data * to the server. Simply put, the echo client initiates the ping-pong traffic * between the echo client and server by sending the first message to the * server. */ public class EchoClient extends Thread { private final String host; private final int port; private final int firstMessageSize; private Integer recyle = 5; public EchoClient(String host, int port, int firstMessageSize, String threadName) { this.host = host; this.port = port; this.firstMessageSize = firstMessageSize; System.out.println("current thread name is ====" + threadName); this.start(); } public void run() { ClientBootstrap bootstrap = new ClientBootstrap( new NioClientSocketChannelFactory( Executors.newCachedThreadPool(), Executors.newCachedThreadPool())); bootstrap.setPipelineFactory(new ChannelPipelineFactory() { public ChannelPipeline getPipeline() throws Exception { return Channels.pipeline(new EchoClientHandler( firstMessageSize, recyle)); } }); ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); future.getChannel().getCloseFuture().awaitUninterruptibly(); bootstrap.setOption("child.tcpNoDelay", true); bootstrap.setOption("child.keepAlive", true); bootstrap.releaseExternalResources(); } public static void main(String[] args) throws Exception { int i = 1; while (true) { i++; // new EchoClient("172.16.4.123", 8080, 256, "thread=" + i); new EchoClient("127.0.0.1", 8080, 256, "thread=" + i); if (i > 3) { break; } } Thread.sleep(1000 * 200); System.out.println("end...."); } }
package com.mchz.netty.test.client; import java.util.concurrent.atomic.AtomicLong; import java.util.logging.Level; import java.util.logging.Logger; import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.ChannelHandlerContext; import org.jboss.netty.channel.ChannelStateEvent; import org.jboss.netty.channel.ExceptionEvent; import org.jboss.netty.channel.SimpleChannelUpstreamHandler; /** * Handler implementation for the echo client. It initiates the ping-pong * traffic between the echo client and server by sending the first message to * the server. */ public class EchoClientHandler extends SimpleChannelUpstreamHandler { private static final Logger logger = Logger .getLogger(EchoClientHandler.class.getName()); private Integer recyle=5; private final ChannelBuffer firstMessage; private final AtomicLong transferredBytes = new AtomicLong(); /** * Creates a client-side handler. */ public EchoClientHandler(int firstMessageSize,Integer recyle) { this.recyle=recyle; if (firstMessageSize <= 0) { throw new IllegalArgumentException("firstMessageSize: " + firstMessageSize); } firstMessage = ChannelBuffers.buffer(firstMessageSize); for (int i = 0; i < firstMessage.capacity(); i++) { firstMessage.writeByte((byte) i); } } public long getTransferredBytes() { return transferredBytes.get(); } @Override public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) { for (int i = 0; i < recyle; i++) { try { System.out.println("send a message to server ..."); e.getChannel().write(firstMessage); Thread.sleep(5000); } catch (InterruptedException e1) { e1.printStackTrace(); } } } @Override public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) { // Close the connection when an exception is raised. System.out.println("close the connection when an exception is raised"); logger.log(Level.WARNING, "Unexpected exception from downstream.", e.getCause()); e.getChannel().close(); } }
服务端
package com.mchz.netty.test.server;
import java.net.InetSocketAddress; import java.util.concurrent.Executors; import org.jboss.netty.bootstrap.ServerBootstrap; import org.jboss.netty.channel.ChannelPipelineFactory; import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; /** * Echoes back any received data from a client. */ public class EchoServer { private final int port; public EchoServer(int port) { this.port = port; } public void run() { // Configure the server. ServerBootstrap bootstrap = new ServerBootstrap( new NioServerSocketChannelFactory( Executors.newCachedThreadPool(), Executors.newCachedThreadPool())); ChannelPipelineFactory pipelineFactory = new MyPipelineFactory( new EchoServerHandler()); bootstrap.setPipelineFactory(pipelineFactory); // bootstrap.setOption("allIdleTime", "10"); bootstrap.bind(new InetSocketAddress(port)); } public static void main(String[] args) throws Exception { int port; if (args.length > 0) { port = Integer.parseInt(args[0]); } else { port = 8080; } new EchoServer(port).run(); } }
package com.mchz.netty.test.server;
import java.util.concurrent.atomic.AtomicLong; import java.util.logging.Level; import java.util.logging.Logger; import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.channel.ChannelHandlerContext; import org.jboss.netty.channel.ChannelStateEvent; import org.jboss.netty.channel.ExceptionEvent; import org.jboss.netty.channel.MessageEvent; import org.jboss.netty.handler.timeout.IdleState; import org.jboss.netty.handler.timeout.IdleStateAwareChannelHandler; import org.jboss.netty.handler.timeout.IdleStateEvent; /** * Handler implementation for the echo server. */ public class EchoServerHandler extends IdleStateAwareChannelHandler { private static final Logger logger = Logger .getLogger(EchoServerHandler.class.getName()); private final AtomicLong transferredBytes = new AtomicLong(); public long getTransferredBytes() { return transferredBytes.get(); } @Override public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { System.out.println("server has been connected"); super.channelConnected(ctx, e); } @Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) { // Send back the received message to the remote peer. transferredBytes.addAndGet(((ChannelBuffer) e.getMessage()) .readableBytes()); System.out .println("I an server ,I received a message,and I will received a message after 5 mill later"); // try { // Thread.sleep(5000); // } catch (InterruptedException e1) { // e1.printStackTrace(); // } // e.getChannel().write(e.getMessage()); } @Override public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) { // Close the connection when an exception is raised. System.out.println(" Close the connection when an exception is raised"+e.getCause().getMessage()); logger.log(Level.WARNING, "Unexpected exception from downstream.", e.getCause()); e.getChannel().close(); } @Override public void channelIdle(ChannelHandlerContext ctx, IdleStateEvent e) throws Exception { // super.channelIdle(ctx, e); if( e.getState() == IdleState.ALL_IDLE){ //// e.getChannel().write("str123".getBytes()); super.channelIdle(ctx, e); } } }
package com.mchz.netty.test.server;
import org.jboss.netty.channel.ChannelHandler; import org.jboss.netty.channel.ChannelPipeline; import org.jboss.netty.channel.ChannelPipelineFactory; import org.jboss.netty.channel.Channels; import org.jboss.netty.handler.timeout.ReadTimeoutHandler; import org.jboss.netty.util.HashedWheelTimer; import org.jboss.netty.util.Timer; public class MyPipelineFactory implements ChannelPipelineFactory { private ChannelHandler serverHandler; public MyPipelineFactory(ChannelHandler serverHander) { this.serverHandler = serverHander; } public ChannelPipeline getPipeline() throws Exception { ChannelPipeline pipeline = Channels.pipeline(); Timer timer = new HashedWheelTimer(); pipeline.addLast("timeout", new ReadTimeoutHandler(timer, 10)); pipeline.addLast("idleHandler", serverHandler); return pipeline; } }
- netty-3.4.5.Final.jar (950.7 KB)
- 下载次数: 40
- Netty_3.1中文用户手册.zip (119.2 KB)
- 下载次数: 166
- netty-test.zip (1.7 MB)
- 下载次数: 109
发表评论
-
centos6.5 hadoop伪分布式搭建
2017-01-10 10:41 25打算整理下machine learn ,再次安装 ... -
springboot
2016-12-29 11:13 2330微服务,现在是一个越来越热的东西,软件架构发展到 ... -
elk安装记录
2016-12-27 18:04 957在centos6.5上安装elk,记录下安装过程和 ... -
dubbo服务框架
2016-12-23 15:40 454上一篇文章介绍了zookeeper,作为服务 ... -
zookeeper服务注册中心配置
2016-12-22 11:40 1633用zookeeper有一段时间了,把配置做下简 ... -
OS X 安装java开发环境
2016-12-03 23:29 562... -
关于Restful API 的设计
2015-06-12 13:32 2239Restful API的流行,很大程度上被当前的移动 ... -
Jvisualvm远程连接tomcat配置:
2015-05-12 16:16 753JvisualVm 监控远程Tomcat,需要在To ... -
jdbc mybatis hibernate springJDBC的一些理解
2015-01-15 17:10 7032... -
Java 的ExecutorService
2015-01-15 11:45 1177早期在Java5以前,我们做多线程程序,一般都 ... -
spring的IOC和AOP
2015-01-14 16:47 2327关于Spring,大家都是耳熟 ... -
java 的线程安全
2014-12-30 14:43 873现代CPU基本都是多核,支持 ... -
JVM ClassLoader
2014-12-25 16:33 894JVM在加载类的时候,都是通过ClassLoad ... -
minor Gc ,Full Gc
2014-12-24 17:22 2067在发生Minor gc之前,虚拟机会先检查老年带最 ... -
Java的主要垃圾回收器
2014-12-24 16:33 1273对象 ... -
JVM的内存结构的一点理解
2014-12-24 11:30 751... -
利用生产者消费者模式实现串行的任务并行化
2014-12-22 17:48 1211试想,一个省有100个处理节点需要 ... -
利用生产者消费者模式实现串行的任务并行化
2014-12-22 17:48 0试想,一个省有100个处理节点需要 ... -
关于Linux的Ulimit参数
2014-12-22 12:13 1236JVM 64位平台与32位平台 ... -
一种表格数据比对的方法
2014-12-19 16:59 1022假设有连个库,一个是生产库,一个是备份库,在一个特 ...
相关推荐
Netty基础,用于学习Netty,参考黑马程序员的netty教程
springboot整合 netty做心跳检测 springboot整合 netty做心跳检测 springboot整合 netty做心跳检测 springboot整合 netty做心跳检测 springboot整合 netty做心跳检测 springboot整合 netty做心跳检测 springboot整合...
netty-3.2.5.Final.jar netty包
Netty (netty-netty-4.0.56.Final.tar.gz)是一个 NIO 客户端服务器框架,可以快速轻松地开发协议服务器和客户端等网络应用程序。它极大地简化和流线了网络编程,例如 TCP 和 UDP 套接字服务器。 “快速和简单”并...
Netty入门教程
netty-buffer-4.1.32.Final-sources.jar netty-buffer-4.1.32.Final.jar netty-build-22-sources.jar netty-build-22.jar netty-codec-4.1.32.Final-sources.jar netty-codec-4.1.32.Final.jar netty-codec-...
Netty (netty-netty-3.10.6.Final.tar.gz)是一个 NIO 客户端服务器框架,可以快速轻松地开发协议服务器和客户端等网络应用程序。它极大地简化和流线了网络编程,例如 TCP 和 UDP 套接字服务器。 “快速和简单”并...
Netty (netty-netty-5.0.0.Alpha2.tar.gz)是一个 NIO 客户端服务器框架,可以快速轻松地开发协议服务器和客户端等网络应用程序。它极大地简化和流线了网络编程,例如 TCP 和 UDP 套接字服务器。 “快速和简单”并...
跟闪电侠学Netty:Netty即时聊天实战与底层原理-book-netty
Netty 教程 Netty权威指南 507页完整版
Netty进阶之路,跟着案例学Netty,李林峰大神新作。值得一读。
Netty大纲-同步netty专栏
netty学习,netty进阶之路:实战,案列,进阶知识
Java进阶技术-netty进阶之路
netty+4G DTU
Netty在Android开发中的应用实战系列(一)——— 搭建服务端与客户端:https://azhon.blog.csdn.net/article/details/100569489 Netty在Android开发中的应用实战系列(二)——— Encoder | Decoder | Handler 的...
netty代码demo
Netty进阶之路 跟着案例学Netty 整本书无密码,Netty进阶之路 跟着案例学Netty
《Netty进阶之路:跟着案例学Netty》中的案例涵盖了Netty的启动和停止、内存、并发多线程、性能、可靠性、安全等方面,囊括了Netty绝大多数常用的功能及容易让人犯错的地方。在案例的分析过程中,还穿插讲解了Netty...