use of org.apache.flink.shaded.netty4.io.netty.handler.codec.string.StringEncoder in project baseio by generallycloud.
the class NettyClient method main.
public static void main(String[] args) throws Exception {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group);
b.channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true);
b.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
pipeline.addLast("frameEncoder", new LengthFieldPrepender(4));
pipeline.addLast("decoder", new StringDecoder(CharsetUtil.UTF_8));
pipeline.addLast("encoder", new StringEncoder(CharsetUtil.UTF_8));
pipeline.addLast("handler", new HelloClient());
}
});
System.out.println("################## Test start ####################");
long old = System.currentTimeMillis();
ChannelFuture f = b.connect("127.0.0.1", 5656).sync();
System.out.println(f.isSuccess());
Channel channel = f.channel();
System.out.println("channel is active :" + channel.isActive() + ",channel:" + channel);
for (int i = 0; i < time; i++) {
String s = "hello Service! ---> :" + i;
ChannelFuture f1 = channel.writeAndFlush(s);
f1.isDone();
}
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
long spend = (System.currentTimeMillis() - old);
System.out.println("## Execute Time:" + time);
System.out.println("## OP/S:" + new BigDecimal(time * 1000).divide(new BigDecimal(spend), 2, BigDecimal.ROUND_HALF_UP));
System.out.println("## Expend Time:" + spend);
f.channel().closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
} finally {
group.shutdownGracefully();
}
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.string.StringEncoder in project camel by apache.
the class MultipleCodecsTest method createRegistry.
@Override
protected JndiRegistry createRegistry() throws Exception {
JndiRegistry registry = super.createRegistry();
// START SNIPPET: registry-beans
ChannelHandlerFactory lengthDecoder = ChannelHandlerFactories.newLengthFieldBasedFrameDecoder(1048576, 0, 4, 0, 4);
StringDecoder stringDecoder = new StringDecoder();
registry.bind("length-decoder", lengthDecoder);
registry.bind("string-decoder", stringDecoder);
LengthFieldPrepender lengthEncoder = new LengthFieldPrepender(4);
StringEncoder stringEncoder = new StringEncoder();
registry.bind("length-encoder", lengthEncoder);
registry.bind("string-encoder", stringEncoder);
List<ChannelHandler> decoders = new ArrayList<ChannelHandler>();
decoders.add(lengthDecoder);
decoders.add(stringDecoder);
List<ChannelHandler> encoders = new ArrayList<ChannelHandler>();
encoders.add(lengthEncoder);
encoders.add(stringEncoder);
registry.bind("encoders", encoders);
registry.bind("decoders", decoders);
// END SNIPPET: registry-beans
return registry;
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.string.StringEncoder in project dubbo by alibaba.
the class QosProcessHandler method decode.
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
if (in.readableBytes() < 1) {
return;
}
// read one byte to guess protocol
final int magic = in.getByte(in.readerIndex());
ChannelPipeline p = ctx.pipeline();
p.addLast(new LocalHostPermitHandler(acceptForeignIp));
if (isHttp(magic)) {
// no welcome output for http protocol
if (welcomeFuture != null && welcomeFuture.isCancellable()) {
welcomeFuture.cancel(false);
}
p.addLast(new HttpServerCodec());
p.addLast(new HttpObjectAggregator(1048576));
p.addLast(new HttpProcessHandler());
p.remove(this);
} else {
p.addLast(new LineBasedFrameDecoder(2048));
p.addLast(new StringDecoder(CharsetUtil.UTF_8));
p.addLast(new StringEncoder(CharsetUtil.UTF_8));
p.addLast(new IdleStateHandler(0, 0, 5 * 60));
p.addLast(new TelnetProcessHandler());
p.remove(this);
}
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.string.StringEncoder in project pancm_project by xuwujing.
the class NettyClient method main.
/**
* Netty创建全部都是实现自AbstractBootstrap。
* 客户端的是Bootstrap,服务端的则是 ServerBootstrap。
*/
public static void main(String[] args) throws Exception {
try {
b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true).handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
p.addLast("decoder", new StringDecoder());
p.addLast("encoder", new StringEncoder());
p.addLast(new BaseClient1Handler());
p.addLast(new BaseClient2Handler());
}
});
// 连接服务端
ChannelFuture future = b.connect(host, port).sync();
System.out.println("客户端连接成功!");
// 发送消息
future.channel().writeAndFlush("Hello Netty Server ,I am a common client");
// 关闭
future.channel().closeFuture().sync();
} finally {
// 释放资源
group.shutdownGracefully();
}
// start();
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.string.StringEncoder in project pancm_project by xuwujing.
the class NettyServer method start.
/**
* Netty创建全部都是实现自AbstractBootstrap。
* 客户端的是Bootstrap,服务端的则是 ServerBootstrap。
*/
public static void start() throws InterruptedException {
// 引导辅助程序
ServerBootstrap sb = new ServerBootstrap();
// 通过nio方式来接收连接和处理连接
EventLoopGroup group = new NioEventLoopGroup();
try {
// 通过nio方式来接收连接和处理连接
sb.group(group);
// 设置nio类型的channel
sb.channel(NioServerSocketChannel.class);
sb.childHandler(new // 有连接到达时会创建一个channel
ChannelInitializer<SocketChannel>() {
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
// 字符串解码 和 编码
p.addLast("decoder", new StringDecoder());
p.addLast("encoder", new StringEncoder());
// 在channel队列中添加一个handler来处理业务
p.addLast("handler", new NettyServerHandler());
// 以("\n")为结尾分割的 解码器
// cp.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
}
});
// 配置完成,开始绑定server,通过调用sync同步方法阻塞直到绑定成功
ChannelFuture f = sb.bind(port).sync();
System.out.println("服务端已启动... 端口是:" + port);
// 应用程序会一直等待,直到channel关闭
f.channel().closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
} finally {
// group.shutdownGracefully().sync();//关闭EventLoopGroup,释放掉所有资源包括创建的线程
}
}
Aggregations