java netty教程(java的Netty中怎么使用定时器执行任务)

本文目录
java的Netty中怎么使用定时器执行任务
第一、三种方式只能校验不含负号“-”的数字,即输入一个负数-199,输出结果将是false;
而第二方式则可以通过修改正则表达式实现校验负数,将正则表达式修改为“^-?+”即可匹配所有数字。
请教一个关于netty的问题
如果是proxy,那么netty就有一个例子了
import java.net.InetSocketAddress;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.socket.ClientSocketChannelFactory;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
/**
***隐藏网址***
***隐藏网址***
* @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $
*/
public class HexDumpProxy {
public static void main(String args) throws Exception {
// Validate command line options.
if (args.length != 3) {
System.err.println(
"Usage: " + HexDumpProxy.class.getSimpleName() +
" 《local port》 《remote host》 《remote port》");
return;
}
// Parse command line options.
int localPort = Integer.parseInt(args);
String remoteHost = args;
int remotePort = Integer.parseInt(args);
System.err.println(
"Proxying *:" + localPort + " to " +
remoteHost + ’:’ + remotePort + " ...");
// Configure the bootstrap.
Executor executor = Executors.newCachedThreadPool();
ServerBootstrap sb = new ServerBootstrap(
new NioServerSocketChannelFactory(executor, executor));
// Set up the event pipeline factory.
ClientSocketChannelFactory cf =
new NioClientSocketChannelFactory(executor, executor);
sb.setPipelineFactory(
new HexDumpProxyPipelineFactory(cf, remoteHost, remotePort));
// Start up the server.
sb.bind(new InetSocketAddress(localPort));
}
}
//===========================
import static org.jboss.netty.channel.Channels.*;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.socket.ClientSocketChannelFactory;
/**
***隐藏网址***
***隐藏网址***
* @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $
*/
public class HexDumpProxyPipelineFactory implements ChannelPipelineFactory {
private final ClientSocketChannelFactory cf;
private final String remoteHost;
private final int remotePort;
public HexDumpProxyPipelineFactory(
ClientSocketChannelFactory cf, String remoteHost, int remotePort) {
this.cf = cf;
this.remoteHost = remoteHost;
this.remotePort = remotePort;
}
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline p = pipeline(); // Note the static import.
p.addLast("handler", new HexDumpProxyInboundHandler(cf, remoteHost, remotePort));
return p;
}
}
//================================
import java.net.InetSocketAddress;
import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.ChannelFutureListener;
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.channel.SimpleChannelUpstreamHandler;
import org.jboss.netty.channel.socket.ClientSocketChannelFactory;
/**
***隐藏网址***
***隐藏网址***
* @version $Rev: 2121 $, $Date: 2010-02-02 09:38:07 +0900 (Tue, 02 Feb 2010) $
*/
public class HexDumpProxyInboundHandler extends SimpleChannelUpstreamHandler {
private final ClientSocketChannelFactory cf;
private final String remoteHost;
private final int remotePort;
private volatile Channel outboundChannel;
public HexDumpProxyInboundHandler(
ClientSocketChannelFactory cf, String remoteHost, int remotePort) {
this.cf = cf;
this.remoteHost = remoteHost;
this.remotePort = remotePort;
}
@Override
public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e)
throws Exception {
// Suspend incoming traffic until connected to the remote host.
final Channel inboundChannel = e.getChannel();
inboundChannel.setReadable(false);
// Start the connection attempt.
ClientBootstrap cb = new ClientBootstrap(cf);
cb.getPipeline().addLast("handler", new OutboundHandler(e.getChannel()));
ChannelFuture f = cb.connect(new InetSocketAddress(remoteHost, remotePort));
outboundChannel = f.getChannel();
f.addListener(new ChannelFutureListener() {
public void operationComplete(ChannelFuture future) throws Exception {
if (future.isSuccess()) {
// Connection attempt succeeded:
// Begin to accept incoming traffic.
inboundChannel.setReadable(true);
} else {
// Close the connection if the connection attempt has failed.
inboundChannel.close();
}
}
});
}
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
throws Exception {
ChannelBuffer msg = (ChannelBuffer) e.getMessage();
System.out.println("》》》 " + ChannelBuffers.hexDump(msg));
outboundChannel.write(msg);
}
@Override
public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e)
throws Exception {
if (outboundChannel != null) {
closeOnFlush(outboundChannel);
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
throws Exception {
e.getCause().printStackTrace();
closeOnFlush(e.getChannel());
}
private static class OutboundHandler extends SimpleChannelUpstreamHandler {
private final Channel inboundChannel;
OutboundHandler(Channel inboundChannel) {
this.inboundChannel = inboundChannel;
}
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
throws Exception {
ChannelBuffer msg = (ChannelBuffer) e.getMessage();
System.out.println("《《《 " + ChannelBuffers.hexDump(msg));
inboundChannel.write(msg);
}
@Override
public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e)
throws Exception {
closeOnFlush(inboundChannel);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
throws Exception {
e.getCause().printStackTrace();
closeOnFlush(e.getChannel());
}
}
/**
* Closes the specified channel after all queued write requests are flushed.
*/
static void closeOnFlush(Channel ch) {
if (ch.isConnected()) {
ch.write(ChannelBuffers.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
}
}
}

本文相关文章:
结构体数组变量赋值(在某个函数中,对结构体数组赋值,如何在其他函数中使用结构体数组中的数据,最好用例子说明一下)
2026年9月24日 06:45
subplot position(matlab,使用subplot时添加总title的方法)
2026年9月19日 00:45
websocket同步模块易语言(使用易语言,怎么写才能和局域网内的电脑时间同步)
2026年9月18日 16:00
flash特效文字(如何使用Flash制作逐渐显示的文字动画)
2026年9月18日 15:45
html文本标签属性标签(如何使用HTML的title属性)
2026年9月16日 19:30
易语言程序需要注册才能使用(易语言程序限制使用次数怎样运行一次注册项的数值减1,到0时程序必须注册)
2026年9月15日 03:30
更多文章:
directive vue(如何在Vue中建立全局引用或者全局命令)
2026年5月6日 19:15
javaeclipse包的新建(用eclipse如何创建java工程)
2026年1月11日 08:30
c语言switch语句case后面接什么(c语言switch语句中case后面必须要接整型常量和字符型常量吗)
2025年10月26日 08:15
怎么把普通文件导入变成web项目(java项目怎么转成web项目)
2025年9月18日 16:00
在循环体内使用break语句(在循环结构中使用break语句,退出的是一层循环,还是多重循环呢可以给出一个简单且巧妙的例子吗)
2026年5月1日 00:00
position在css的属性(CSS中position属性详解)
2025年9月26日 23:45
amazed是什么牌子(amazing和amazed有什么区别)
2026年4月3日 15:00
周期函数值域的求法(已知函数的最小正周期为求;当时,求函数的值域.)
2025年11月30日 22:15
毕设做网站必须要用到ssm框架嘛(毕业设计做ssm还是app更难)
2026年3月31日 03:00
new directory(如何将威纶通tk6070ip触摸屏程序上传到电脑里)
2026年8月7日 01:45










