Exemple #1
0
  public DefaultUDPServer(
      final RemotingController remotingController, final UDPServiceHandler handler, final int port)
      throws NotifyRemotingException {
    this.initController();
    this.handler = handler;
    this.udpController.setHandler(
        new HandlerAdapter() {

          @Override
          public void onMessageReceived(final Session session, final Object message) {
            DefaultUDPServer.this.handler.onMessageReceived((DatagramPacket) message);
          }
        });
    // 复用SelectorManager
    if (remotingController != null && remotingController.isStarted()) {
      final NioController nioController =
          ((BaseRemotingController) remotingController).getController();
      this.udpController.setSelectorManager(nioController.getSelectorManager());
    }
    try {
      this.udpController.bind(new InetSocketAddress(port));
    } catch (final IOException e) {
      throw new NotifyRemotingException("启动udp服务器失败,端口为" + port, e);
    }
  }