|
@@ -4,6 +4,7 @@ import java.net.InetSocketAddress;
|
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
+import com.phm.common.exception.GlobalException;
|
|
import com.phm.netty.client.handler.NettyUdpClientHandler;
|
|
import com.phm.netty.client.handler.NettyUdpClientHandler;
|
|
|
|
|
|
import io.netty.bootstrap.Bootstrap;
|
|
import io.netty.bootstrap.Bootstrap;
|
|
@@ -31,39 +32,34 @@ public class NettyUdpClient {
|
|
EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
|
|
EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
|
|
try {
|
|
try {
|
|
Bootstrap clientBootstrap = new Bootstrap();
|
|
Bootstrap clientBootstrap = new Bootstrap();
|
|
- clientBootstrap = clientBootstrap.group(eventLoopGroup);
|
|
|
|
- clientBootstrap = clientBootstrap.channel(NioDatagramChannel.class);
|
|
|
|
- clientBootstrap = clientBootstrap.option(ChannelOption.SO_BROADCAST, true);
|
|
|
|
- // clientBootstrap = clientBootstrap.handler(new NettyUdpClientInitialize(customValue));
|
|
|
|
- clientBootstrap = clientBootstrap.handler(new ChannelInitializer<NioDatagramChannel>() {
|
|
|
|
- @Override
|
|
|
|
- protected void initChannel(NioDatagramChannel ch) {
|
|
|
|
- // 使用 AttributeKey 定义一个键
|
|
|
|
- final AttributeKey<String> key = AttributeKey.valueOf("customKey");
|
|
|
|
|
|
+ clientBootstrap = clientBootstrap.group(eventLoopGroup).channel(NioDatagramChannel.class)
|
|
|
|
+ .option(ChannelOption.SO_BROADCAST, true).handler(new ChannelInitializer<NioDatagramChannel>() {
|
|
|
|
+ @Override
|
|
|
|
+ protected void initChannel(NioDatagramChannel ch) {
|
|
|
|
+ // 使用 AttributeKey 定义一个键
|
|
|
|
+ final AttributeKey<String> key = AttributeKey.valueOf("customKey");
|
|
|
|
|
|
- // 获取 Channel 的 Attribute
|
|
|
|
- Attribute<String> attr = ch.attr(key);
|
|
|
|
|
|
+ // 获取 Channel 的 Attribute
|
|
|
|
+ Attribute<String> attr = ch.attr(key);
|
|
|
|
|
|
- // 设置自定义值
|
|
|
|
- attr.set(customValue);
|
|
|
|
|
|
+ // 设置自定义值
|
|
|
|
+ attr.set(customValue);
|
|
|
|
|
|
- // 在这里添加你的处理器
|
|
|
|
- ch.pipeline().addLast(new NettyUdpClientHandler());
|
|
|
|
- }
|
|
|
|
- });
|
|
|
|
|
|
+ // 在这里添加你的处理器
|
|
|
|
+ ch.pipeline().addLast(new NettyUdpClientHandler());
|
|
|
|
+ }
|
|
|
|
+ });
|
|
Channel channel = clientBootstrap.bind(8989).sync().channel();
|
|
Channel channel = clientBootstrap.bind(8989).sync().channel();
|
|
// 给服务端发送请求信息
|
|
// 给服务端发送请求信息
|
|
channel.writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer(data), new InetSocketAddress(address, port)))
|
|
channel.writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer(data), new InetSocketAddress(address, port)))
|
|
.sync();
|
|
.sync();
|
|
- log.info("channel_id = {} ", channel.id().toString());
|
|
|
|
-
|
|
|
|
// 方式一:查询等待超时 单位s, 超时自动关闭
|
|
// 方式一:查询等待超时 单位s, 超时自动关闭
|
|
- channel.closeFuture().await(10000);
|
|
|
|
-
|
|
|
|
|
|
+ // channel.closeFuture().await(10000);
|
|
// 方式er:原地等候返回消息,再关闭连接
|
|
// 方式er:原地等候返回消息,再关闭连接
|
|
channel.closeFuture().sync();
|
|
channel.closeFuture().sync();
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
log.error(e.getMessage());
|
|
log.error(e.getMessage());
|
|
|
|
+ throw new GlobalException("在线下载数据异常,请重新下载");
|
|
} finally {
|
|
} finally {
|
|
log.info("netty client udp close!");
|
|
log.info("netty client udp close!");
|
|
eventLoopGroup.shutdownGracefully();
|
|
eventLoopGroup.shutdownGracefully();
|