use of io.netty.handler.codec.string.StringDecoder in project java-in-action by xinghalo.
the class EchoServer method bind.
public void bind(int port) throws InterruptedException {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
ServerBootstrap b = new ServerBootstrap();
try {
b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ByteBuf delimiter = Unpooled.copiedBuffer("$_".getBytes());
ch.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, delimiter));
ch.pipeline().addLast(new StringDecoder());
ch.pipeline().addLast(new EchoServerHandler());
}
});
ChannelFuture f = b.bind(port).sync();
f.channel().closeFuture().sync();
} finally {
bossGroup.shutdownGracefully().sync();
workerGroup.shutdownGracefully().sync();
}
}
use of io.netty.handler.codec.string.StringDecoder 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 io.netty.handler.codec.string.StringDecoder 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 io.netty.handler.codec.string.StringDecoder in project openremote by openremote.
the class TcpStringServer method addDecoders.
@Override
protected void addDecoders(SocketChannel channel) {
// Add delimiter and string decoders to do the work
channel.pipeline().addLast(new DelimiterBasedFrameDecoder(maxFrameLength, stripDelimiter, Unpooled.wrappedBuffer(delimiter.getBytes())));
channel.pipeline().addLast(new StringDecoder());
}
use of io.netty.handler.codec.string.StringDecoder in project alibaba-mom by younfor.
the class DefaultProducer method start.
@Override
public void start() {
SIP = System.getProperty("SIP");
if (SIP == null)
SIP = "127.0.0.1";
System.out.println("connect:" + System.getProperty("SIP"));
group = new NioEventLoopGroup();
bootstrap = new Bootstrap();
bootstrap.group(group).channel(NioSocketChannel.class).option(ChannelOption.SO_REUSEADDR, true).option(ChannelOption.SO_KEEPALIVE, true).option(ChannelOption.TCP_NODELAY, true).option(ChannelOption.SO_KEEPALIVE, true).handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel channel) throws Exception {
channel.pipeline().addLast(new RpcEncoder()).addLast(new LineBasedFrameDecoder(1024 * 256)).addLast(new StringDecoder()).addLast(new SimpleChannelInboundHandler<Object>() {
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
logger.error("producer失去broker链接");
connect();
}
@Override
protected void channelRead0(ChannelHandlerContext arg0, Object info) throws Exception {
logger.debug("recv ack: " + (String) info);
if (info != null) /*&&info.toString().startsWith("ackGroup"*/
{
String[] id = ((String) info).split("@");
// System.out.println(id.length+":len:"+id[0]+" | "+id[1]);
if (id[0].startsWith("fail")) {
// System.out.println("无人订阅");
SendResult sr = new SendResult();
sr.setMsgId(id[1]);
resultMap.get(id[1]).put(sr);
} else {
for (int i = 1; i < id.length; i++) {
// System.out.println(id[i]);
SendResult sr = new SendResult();
sr.setMsgId(id[i]);
resultMap.get(id[i]).put(sr);
}
}
// synchronized(lock){
// lock.notifyAll();
// }
} else {
// 异步方式接受数据
if (asyncResults.containsKey((String) info)) {
SendResult sr = new SendResult();
sr.setMsgId((String) info);
asyncResults.get((String) info).onResult(sr);
;
} else {
}
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
logger.error("--生产者的异常关闭--" + cause.getMessage());
cause.printStackTrace();
logger.error("重新连接");
ctx.close();
connect();
}
});
}
});
connect();
}
Aggregations