有人可以使用UDP服务器为Netty 4.0引导我吗?我看到很多3.x示例,但即使在netty源示例中也没有4.x的迹象.(注意我对Netty很新)
基本上,它是https://netty.io/Documentation/New+and+Noteworthy#HNewbootstrapAPI的示例,但是对于UDP而言.非常感谢帮助
我正在尝试用 Netty 实现一个 UDP 服务器。这个想法是只绑定一次(因此只创建一个Channel)。这Channel仅使用一个处理程序进行初始化,该处理程序通过ExecutorService.
@Configuration
public class SpringConfig {
@Autowired
private Dispatcher dispatcher;
private String host;
private int port;
@Bean
public Bootstrap bootstrap() throws Exception {
Bootstrap bootstrap = new Bootstrap()
.group(new NioEventLoopGroup(1))
.channel(NioDatagramChannel.class)
.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
.handler(dispatcher);
ChannelFuture future = bootstrap.bind(host, port).await();
if(!future.isSuccess())
throw new Exception(String.format("Fail to bind on [host = %s , port = %d].", host, port), future.cause());
return bootstrap;
}
}
@Component
@Sharable
public class Dispatcher extends ChannelInboundHandlerAdapter implements InitializingBean { …Run Code Online (Sandbox Code Playgroud)