- 浏览: 1254144 次
- 性别:
- 来自: 杭州
文章分类
- 全部博客 (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 2392微服务,现在是一个越来越热的东西,软件架构发展到 ... -
elk安装记录
2016-12-27 18:04 987在centos6.5上安装elk,记录下安装过程和 ... -
dubbo服务框架
2016-12-23 15:40 485上一篇文章介绍了zookeeper,作为服务 ... -
zookeeper服务注册中心配置
2016-12-22 11:40 1668用zookeeper有一段时间了,把配置做下简 ... -
OS X 安装java开发环境
2016-12-03 23:29 602... -
关于Restful API 的设计
2015-06-12 13:32 2282Restful API的流行,很大程度上被当前的移动 ... -
Jvisualvm远程连接tomcat配置:
2015-05-12 16:16 787JvisualVm 监控远程Tomcat,需要在To ... -
jdbc mybatis hibernate springJDBC的一些理解
2015-01-15 17:10 7070... -
Java 的ExecutorService
2015-01-15 11:45 1227早期在Java5以前,我们做多线程程序,一般都 ... -
spring的IOC和AOP
2015-01-14 16:47 2358关于Spring,大家都是耳熟 ... -
java 的线程安全
2014-12-30 14:43 978现代CPU基本都是多核,支持 ... -
JVM ClassLoader
2014-12-25 16:33 946JVM在加载类的时候,都是通过ClassLoad ... -
minor Gc ,Full Gc
2014-12-24 17:22 2107在发生Minor gc之前,虚拟机会先检查老年带最 ... -
Java的主要垃圾回收器
2014-12-24 16:33 1312对象 ... -
JVM的内存结构的一点理解
2014-12-24 11:30 783... -
利用生产者消费者模式实现串行的任务并行化
2014-12-22 17:48 1238试想,一个省有100个处理节点需要 ... -
利用生产者消费者模式实现串行的任务并行化
2014-12-22 17:48 0试想,一个省有100个处理节点需要 ... -
关于Linux的Ulimit参数
2014-12-22 12:13 1283JVM 64位平台与32位平台 ... -
一种表格数据比对的方法
2014-12-19 16:59 1056假设有连个库,一个是生产库,一个是备份库,在一个特 ...
相关推荐
"timeout" 和 "断掉重连" 是网络编程中的关键概念。在Netty中,我们可以利用IdleStateHandler来检测连接的空闲状态,并在超时后执行相应的操作,比如断开连接或者发起重连请求。这种机制对于保持网络服务的可靠性至...
3. **任务接口与实现**:可能包含自定义的任务接口,如`Timeout`和`TimerTask`,需要了解它们的生命周期方法以及如何实现自己的业务逻辑。 4. **并发与线程安全**:因为`HashedWheelTimer`是线程安全的,所以理解其...
startup-timeout = 10 s enabled-transports = ["akka.remote.netty4.tcp"] netty4 { tcp { port = 2554 } } } } } 之前的基准 Starting benchmark of 500000 messages with burst size 5000 and payload
import io.netty.handler.timeout.IdleStateHandler; public class ServerIdleCheckHandler extends IdleStateHandler { public ServerIdleCheckHandler() { super(0, 0, 120, TimeUnit.SECONDS); // 设置120秒未...
和client_header_timeout类似,如果客户端在这个时间内没有发送任何数据,Nginx也会返回408错误。 4. proxy_connect_timeout:设置与代理服务器连接的超时时间。当Nginx向代理服务器转发请求时,这个指令定义了等待...
在IT领域,Xmodem和Ymodem是两种早期的文件传输协议,主要用于串行通信。这些协议在个人计算机发展的初期扮演了重要角色,尤其是在低带宽和不可靠的通信链路环境下。本文将深入探讨这两种协议的工作原理,并提供它们...
8. **定时器测试**:`@Timeout`注解可以限制测试方法的执行时间,如果超过设定的时间,测试将失败。 9. **假设(Assumptions)**:JUnit 4引入了`org.junit.Assume`类,允许在测试开始前设置条件。如果假设失败,...
Java时间轮算法的核心思想是使用一个环形数组来存储定时任务,每个任务对应一个 timeout 对象,timeout 对象包含了任务的执行时间、任务的执行状态等信息。在每个 tick 时间点,Java时间轮算法会遍历环形数组,检查...
- 例如,如果希望将超时时间设置为 1 秒,可以调用 `lrs_set_recv_timeout2(1, 0)`。 #### 四、使用 `MSG_PEEK` 标志 `MSG_PEEK` 是一个接收数据时的标志,它允许 LoadRunner 查看数据而不立即将其从接收队列中...
在Provider上可以配置的Consumer端的属性有timeout、retries、loadbalance、actives等。 十三、Dubbo启动时如果依赖的服务不可用会怎样? Dubbo缺省会在启动时检查依赖的服务是否可用,不可用时会抛出异常,阻止...
EasySocket README内容将不再更新,请到博客阅读最新的框架说明! 博客地址: EasySocket的初衷是希望使Socket编程变得更加简单、快捷,因此项目在实现了Socket基本功能的基础上,还实现了TCP层面的请求回调功能。...
`FutureTask`实现了`Future`接口,它内部维护了一个任务状态,任务发起者调用`get()`或`get(timeout)`方法获取结果时,如果任务尚未完成,会被阻塞等待。任务执行完成后,`FutureTask`会唤醒等待的线程。`cancel()`...
- `spark.rpc.askTimeout`:RPC请求超时时间,与`spark.network.timeout`配合调整。 - `spark.shuffle.blockTransferService`:选择合适的传输服务,如NIO或Netty,以提高数据传输速度。 7. **垃圾回收** - `...
<refresh-timeout>10000</refresh-timeout> ``` ### 4. 复制策略 在主主集群中,数据复制是关键。HornetQ提供了多种复制策略,如全复制(Full Replication)和部分复制(Shared Store)。全复制中,所有节点...
8. Consumer端可配置属性:如timeout、retries、loadbalance和actives。 9. 启动时检查:Dubbo默认在启动时检查依赖服务,不可用时会抛出异常,可通过check="false"关闭此检查。 10. 序列化框架:推荐使用Hessian,...
timeout.ms=30000 ``` 开发者可以根据实际需求调整这些参数,优化服务器性能。 四、依赖库(libs) `libs`目录通常包含项目运行所需的外部依赖库。在Java项目中,这可能是JAR文件,包含了如NIO、网络通信、序列化等...
ServiceBean对象包含了服务的所有参数,如timeout,这些参数可以通过@Service注解进行设置,也可以从应用配置、全局配置或配置中心获取。 服务导出的主要步骤包括: 1. 确定服务参数:整合各个来源的配置,形成最终...
RLock lock(String lockKey, TimeUnit unit, int timeout); boolean tryLock(String lockKey, TimeUnit unit, int waitTime, int leaseTime); void unlock(String lockKey); void unlock(RLock lock); } ``` ...
4. **套接字选项**:如SocketOptions,用于配置和获取Socket的属性,例如SO_TIMEOUT用于设置超时。 5. **高级网络API**:如NIO(Non-blocking I/O)和NIO.2,它们提供了一种更有效处理大量连接的方式,特别是对于...