50,331
社区成员




@RabbitListener(queues="SlzrDatatransferModel.Bj")//
public void receive(byte[]strMsg) throws UnsupportedEncodingException{
log.info("------监听消息开始---------");
final NettyClient nettyClient = new NettyClient("122.27.11.160",6856);
nettyClient.start();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
nettyClient.sendMsg(strMsg);
Thread.sleep(3000);
} catch (SendException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public class NettyClient extends Thread{
/**
* 服务端地址
*/
private String ipAdress;
/**
* 服务端端口
*/
private Integer port;
private ChannelFuture channelFuture;
public NettyClient(String ipAdress, Integer port) {
this.ipAdress = ipAdress;
this.port = port;
if (port == null || "".equals(ipAdress)){
throw new RuntimeException("");
}
}
@Override
public void run() {
try {
init();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void init() throws InterruptedException {
EventLoopGroup group = new NioEventLoopGroup();
Bootstrap b = new Bootstrap();
b.group(group).channel(NioSocketChannel.class)
.option(ChannelOption.TCP_NODELAY, true)
.handler(new ChannelInitializer<SocketChannel>(){
@Override
public void initChannel(SocketChannel ch) throws Exception{
ch.pipeline().addLast("decoder", new ClientFilter());
ch.pipeline().addLast("encoder", new ByteArrayEncoder());
ch.pipeline().addLast("handler", new BinaryHandler());
}
});
channelFuture = b.connect(ipAdress, port).sync();
channelFuture.channel().closeFuture().sync();
}
public void sendMsg(byte[] msg) throws SendException {
if (channelFuture != null && channelFuture.channel().isOpen()){
// try {
ChannelFuture sync = channelFuture.channel().
writeAndFlush(Unpooled.copiedBuffer(msg));
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
}
// throw new SendException("send is error ! ");
}
}