Пример #1
0
  public void start() throws InterruptedException {
    EventLoopGroup group = new NioEventLoopGroup();

    try {
      ServerBootstrap b = new ServerBootstrap();
      b.group(group)
          .channel(NioServerSocketChannel.class)
          .localAddress(new InetSocketAddress(port))
          .childHandler(
              new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel ch) throws Exception {
                  ch.pipeline()
                      .addLast(new EchoServerHandler())
                      .addFirst(new OutboundTest1())
                      .addFirst(new OutboundTest2());
                }
              });
      ChannelFuture f = b.bind().sync();
      System.out.println(
          EchoServer.class.getName() + " started and listen on" + f.channel().localAddress());
      f.channel().closeFuture().sync();
    } finally {
      group.shutdownGracefully().sync();
    }
  }
Пример #2
0
 public void start() throws InterruptedException, URISyntaxException {
   EventLoopGroup group = new NioEventLoopGroup();
   try {
     Bootstrap b = new Bootstrap();
     b.group(group).channel(NioSocketChannel.class);
     b.handler(
         new ChannelInitializer<Channel>() {
           @Override
           protected void initChannel(Channel ch) throws Exception {
             ChannelPipeline pipeline = ch.pipeline();
             pipeline.addLast("decoder", new RtspResponseDecoder());
             pipeline.addLast("encoder", new RtspRequestEncoder());
             pipeline.addLast("aggregator", new HttpObjectAggregator(1024 * 1024 * 64));
             pipeline.addLast("handler", new ResponseHandler());
           }
         });
     b.option(ChannelOption.SO_KEEPALIVE, true);
     ChannelFuture channelFutrue = b.connect(host, port).sync();
     if (channelFutrue.isSuccess()) {
       channel = channelFutrue.channel();
       URI uri = new URI("rtsp://127.0.0.1:554/hello-world");
       HttpRequest request = this.buildOptionsRequest(uri.toASCIIString());
       this.send(request);
       channel.closeFuture().sync();
     }
   } finally {
     // 优雅退出,释放NIO线程组
     group.shutdownGracefully();
   }
 }
Пример #3
0
  public void stop() {
    if (m_workerGroup == null) {
      throw new IllegalStateException("Invoked close on an Acceptor that wasn't initialized");
    }
    if (m_bossGroup == null) {
      throw new IllegalStateException("Invoked close on an Acceptor that wasn't initialized");
    }
    m_workerGroup.shutdownGracefully();
    m_bossGroup.shutdownGracefully();

    LOG.info("Closed boss and worker Event loops");
    System.out.println("Server stopped");
  }
Пример #4
0
  public static void main(String[] args) throws Exception {
    // Configure SSL.
    final SslContext sslCtx;
    if (SSL) {
      SelfSignedCertificate ssc = new SelfSignedCertificate();
      sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
    } else {
      sslCtx = null;
    }

    // Configure the server.
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
      ServerBootstrap b = new ServerBootstrap();
      b.group(bossGroup, workerGroup)
          .channel(NioServerSocketChannel.class)
          .option(ChannelOption.SO_BACKLOG, 100)
          .handler(new LoggingHandler(LogLevel.INFO))
          .childHandler(
              new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel ch) throws Exception {
                  ChannelPipeline p = ch.pipeline();
                  if (sslCtx != null) {
                    p.addLast(sslCtx.newHandler(ch.alloc()));
                  }
                  p.addLast(
                      new StringEncoder(CharsetUtil.UTF_8),
                      new LineBasedFrameDecoder(8192),
                      new StringDecoder(CharsetUtil.UTF_8),
                      new ChunkedWriteHandler(),
                      new FileServerHandler());
                }
              });

      // Start the server.
      ChannelFuture f = b.bind(PORT).sync();

      // Wait until the server socket is closed.
      f.channel().closeFuture().sync();
    } finally {
      // Shut down all event loops to terminate all threads.
      bossGroup.shutdownGracefully();
      workerGroup.shutdownGracefully();
    }
  }
Пример #5
0
 @Override
 protected void serviceStop() throws Exception {
   if (channel != null && group != null) {
     channel.close().sync();
     group.shutdownGracefully();
     bootstrap = null;
     group = null;
     channel = null;
   }
 }
Пример #6
0
  public void start(int port) throws InterruptedException {
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();

    try {
      ServerBootstrap b = new ServerBootstrap();
      b.group(bossGroup, workerGroup)
          .channel(NioServerSocketChannel.class)
          .option(ChannelOption.SO_BACKLOG, 10000) // max number of waiting connections
          //                    .handler(new LoggingHandler(LogLevel.INFO))
          .childHandler(new PipelineFactory());

      ChannelFuture f = b.bind(port).sync();

      f.channel().closeFuture().sync();
    } finally {
      workerGroup.shutdownGracefully();
      bossGroup.shutdownGracefully();
    }
  }
  @Override
  public void destroy() {
    this.bindTimer.cancel();
    stop();

    // Shut down all event loops to terminate all threads.
    bossGroup.shutdownGracefully();
    workerGroup.shutdownGracefully();

    try {
      // Wait until all threads are terminated.
      bossGroup.terminationFuture().sync();
      workerGroup.terminationFuture().sync();
    } catch (InterruptedException e) {
      // is ok
    }

    this.serverBootstrap = null;
    unregisterMBean();
    logger.info("{} destroyed on SMPP port [{}]", configuration.getName(), configuration.getPort());
  }
  public static void main(String[] args) throws SSLException, InterruptedException {
    // Configure SSL.git
    final SslContext sslCtx;
    if (SSL) {
      sslCtx = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE);

    } else {
      sslCtx = null;
    }

    // Configure the client.
    EventLoopGroup group = new NioEventLoopGroup();
    try {
      Bootstrap b = new Bootstrap();
      b.group(group)
          .channel(NioSocketChannel.class)
          .option(ChannelOption.TCP_NODELAY, true)
          .handler(
              new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(@NotNull SocketChannel ch) {
                  ChannelPipeline p = ch.pipeline();
                  if (sslCtx != null) {
                    p.addLast(sslCtx.newHandler(ch.alloc(), HOST, PORT));
                  }
                  // p.addLast(new LoggingHandler(LogLevel.INFO));
                  p.addLast(new MyChannelInboundHandler());
                }
              });

      // Start the client.
      ChannelFuture f = b.connect(HOST, PORT).sync();

      // Wait until the connection is closed.
      f.channel().closeFuture().sync();
    } finally {
      // Shut down the event loop to terminate all threads.
      group.shutdownGracefully();
    }
  }
Пример #9
0
  public static void main(String[] args) throws Exception {
    UserBroadcastService userBroadcastService = new UserBroadcastService();
    RabbitService publisher = new RabbitService(userBroadcastService, RABBIT_HOST);
    publisher.listen();

    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    DefaultEventExecutorGroup eventExecutors = new DefaultEventExecutorGroup(20);

    try {
      ServerBootstrap b = new ServerBootstrap();
      b.group(bossGroup, workerGroup)
          .channel(NioServerSocketChannel.class)
          .option(ChannelOption.SO_BACKLOG, 100)
          .handler(new LoggingHandler(LogLevel.DEBUG))
          .childHandler(
              new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel ch) throws Exception {
                  ChannelPipeline p = ch.pipeline();
                  p.addLast(new GreetingHandler());
                  p.addLast(new LineBasedFrameDecoder(1000));
                  p.addLast(new UserMessageDecoder(userBroadcastService));
                  p.addLast(eventExecutors, new MessagePublishingHandler(publisher));
                }
              });

      // Start the server.
      ChannelFuture f = b.bind(PORT).sync();

      // Wait until the server socket is closed.
      f.channel().closeFuture().sync();
    } finally {
      // Shut down all event loops to terminate all threads.
      bossGroup.shutdownGracefully();
      workerGroup.shutdownGracefully();
      publisher.close();
    }
  }
Пример #10
0
 public void close() {
   group.shutdownGracefully();
 }
Пример #11
0
 public void stop() {
   serverBossGroup.shutdownGracefully();
   serverWorkerGroup.shutdownGracefully();
 }
Пример #12
0
 public void stop() {
   bossGroup.shutdownGracefully();
   workerGroup.shutdownGracefully();
 }
Пример #13
0
 public static void shutdownGracefully() {
   group.shutdownGracefully();
   bootstrap = null;
 }
  private void close(boolean shutdownEventLoopGroup) {
    ServerChannel serverChannel = this.serverChannel.get();
    if (serverChannel == null) {
      LOG.assertTrue(clientChannels.isEmpty());
      return;
    } else if (!this.serverChannel.compareAndSet(serverChannel, null)) {
      return;
    }

    EventLoopGroup eventLoopGroup =
        shutdownEventLoopGroup ? serverChannel.eventLoop().parent() : null;
    try {
      long start = System.currentTimeMillis();
      Channel[] clientChannels = this.clientChannels.toArray(new Channel[] {});
      this.clientChannels.clear();

      final CountDownLatch countDown = new CountDownLatch(clientChannels.length + 1);
      GenericFutureListener<ChannelFuture> listener =
          new GenericFutureListener<ChannelFuture>() {
            @Override
            public void operationComplete(@NotNull ChannelFuture future) throws Exception {
              try {
                Throwable cause = future.cause();
                if (cause != null) {
                  LOG.warn(cause);
                }
              } finally {
                countDown.countDown();
              }
            }
          };
      serverChannel.close().addListener(listener);
      for (Channel channel : clientChannels) {
        channel.close().addListener(listener);
      }

      try {
        countDown.await(5, TimeUnit.SECONDS);
      } catch (InterruptedException e) {
        LOG.warn(
            "Cannot close all channels for 10 seconds, channels: "
                + Arrays.toString(clientChannels));
      }

      long duration = System.currentTimeMillis() - start;
      if (duration > 1000) {
        LOG.info(
            "Close all channels took "
                + duration
                + " ms: "
                + (duration / 60000)
                + " min "
                + ((duration % 60000) / 1000)
                + "sec");
      }
    } finally {
      if (eventLoopGroup != null) {
        eventLoopGroup.shutdownGracefully(1, 2, TimeUnit.NANOSECONDS);
      }
    }
  }