5 changed files with 113 additions and 1 deletions
@ -0,0 +1,4 @@ |
|||
package com.ccsens.logistics.service; |
|||
|
|||
public interface INettyService { |
|||
} |
@ -0,0 +1,13 @@ |
|||
package com.ccsens.logistics.service; |
|||
|
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.stereotype.Service; |
|||
import org.springframework.transaction.annotation.Propagation; |
|||
import org.springframework.transaction.annotation.Transactional; |
|||
|
|||
@Slf4j |
|||
@Service |
|||
@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class) |
|||
public class NerryService implements INettyService{ |
|||
|
|||
} |
@ -0,0 +1,24 @@ |
|||
package com.ccsens.util; |
|||
|
|||
import io.netty.buffer.ByteBuf; |
|||
import io.netty.channel.ChannelHandlerContext; |
|||
import io.netty.channel.ChannelInboundHandlerAdapter; |
|||
import io.netty.util.AttributeKey; |
|||
|
|||
import java.nio.charset.Charset; |
|||
|
|||
public class SimpleClientHandler extends ChannelInboundHandlerAdapter{ |
|||
@Override |
|||
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { |
|||
if (msg instanceof ByteBuf) { |
|||
String value = ((ByteBuf) msg).toString(Charset.defaultCharset()); |
|||
System.out.println("服务器端返回的数据:" + value); |
|||
} |
|||
|
|||
AttributeKey<String> key = AttributeKey.valueOf("ServerData"); |
|||
ctx.channel().attr(key).set("客户端处理完毕"); |
|||
|
|||
//把客户端的通道关闭
|
|||
// ctx.channel().close();
|
|||
} |
|||
} |
@ -0,0 +1,60 @@ |
|||
package com.ccsens.util; |
|||
|
|||
import io.netty.bootstrap.Bootstrap; |
|||
import io.netty.channel.ChannelFuture; |
|||
import io.netty.channel.ChannelInitializer; |
|||
import io.netty.channel.EventLoopGroup; |
|||
import io.netty.channel.nio.NioEventLoopGroup; |
|||
import io.netty.channel.socket.nio.NioSocketChannel; |
|||
import io.netty.handler.codec.DelimiterBasedFrameDecoder; |
|||
import io.netty.handler.codec.Delimiters; |
|||
import io.netty.handler.codec.string.StringEncoder; |
|||
|
|||
public class nattyTest { |
|||
|
|||
|
|||
public static void main(String[] args) throws InterruptedException { |
|||
// 首先,netty通过ServerBootstrap启动服务端
|
|||
Bootstrap client = new Bootstrap(); |
|||
|
|||
//第1步 定义线程组,处理读写和链接事件,没有了accept事件
|
|||
EventLoopGroup group = new NioEventLoopGroup(); |
|||
client.group(group ); |
|||
|
|||
//第2步 绑定客户端通道
|
|||
client.channel(NioSocketChannel.class); |
|||
|
|||
//第3步 给NIoSocketChannel初始化handler, 处理读写事件
|
|||
client.handler(new ChannelInitializer<NioSocketChannel>() { //通道是NioSocketChannel
|
|||
@Override |
|||
protected void initChannel(NioSocketChannel ch) throws Exception { |
|||
//字符串编码器,一定要加在SimpleClientHandler 的上面
|
|||
ch.pipeline().addLast(new StringEncoder()); |
|||
ch.pipeline().addLast(new DelimiterBasedFrameDecoder( |
|||
Integer.MAX_VALUE, Delimiters.lineDelimiter()[0])); |
|||
//找到他的管道 增加他的handler
|
|||
ch.pipeline().addLast(new SimpleClientHandler()); |
|||
} |
|||
}); |
|||
|
|||
//连接服务器
|
|||
ChannelFuture future = client.connect("47.106.129.173", 15506).sync(); |
|||
|
|||
|
|||
for(int i=0;i<5;i++){ |
|||
future.channel().writeAndFlush("{\"service_id\":\"123\",\"type\":\"READSN4\",\"thing_id\":\"050005A2\",\"data\":{\"GetType\":\"Auto\",\"Highest\":70,\"Lowest\":30}}"+"\r\n"); |
|||
System.out.println("请求"); |
|||
Thread.sleep(5000); |
|||
} |
|||
|
|||
|
|||
|
|||
// //当通道关闭了,就继续往下走
|
|||
// future.channel().closeFuture().sync();
|
|||
|
|||
//接收服务端返回的数据
|
|||
// AttributeKey<String> key = AttributeKey.valueOf("ServerData");
|
|||
// Object result = future.channel().attr(key).get();
|
|||
// System.out.println(result.toString());
|
|||
} |
|||
} |
Loading…
Reference in new issue