Пример #1
0
  public static RemotingServer createRemotingServer() throws InterruptedException {
    NettyServerConfig config = new NettyServerConfig();
    config.setServerChannelMaxIdleTimeSeconds(30);
    RemotingServer remotingServer = new NettyRemotingServer(config);
    remotingServer.registerProcessor(
        0,
        new NettyRequestProcessor() {
          private int i = 0;

          @Override
          public RemotingCommand processRequest(
              ChannelHandlerContext ctx, RemotingCommand request) {
            System.out.println("processRequest=" + request + " " + (i++));
            request.setRemark("hello, I am respponse " + ctx.channel().remoteAddress());
            return request;
          }

          @Override
          public boolean rejectRequest() {
            return false;
          }
        },
        Executors.newCachedThreadPool());
    remotingServer.start();
    return remotingServer;
  }
Пример #2
0
  // @Test
  public void test_idle_event()
      throws InterruptedException, RemotingConnectException, RemotingSendRequestException,
          RemotingTimeoutException {
    RemotingServer server = createRemotingServer();
    RemotingClient client = createRemotingClient();

    for (int i = 0; i < 10; i++) {
      RemotingCommand request = RemotingCommand.createRequestCommand(0, null);
      RemotingCommand response = client.invokeSync("localhost:8888", request, 1000 * 3);
      System.out.println(i + " invoke result = " + response);
      assertTrue(response != null);

      Thread.sleep(1000 * 10);
    }

    Thread.sleep(1000 * 60);

    client.shutdown();
    server.shutdown();
    System.out.println("-----------------------------------------------------------------");
  }