thrift servr

1.同步阻塞

      CaculatorService.Processor<CaculatorServiceHandler> processor = new CaculatorService.Processor<CaculatorServiceHandler>(
          new CaculatorServiceHandler());
      TServerTransport serverTransport = new TServerSocket(8888);
      Args targs = new Args(serverTransport);
      targs.processor(processor);
      targs.transportFactory(new TFramedTransport.Factory());
      targs.protocolFactory(new TCompactProtocol.Factory());
      TServer server = new TSimpleServer(targs);
      System.out.println("Starting the TSimpleServer...");
      server.serve();

      CaculatorService.Processor<CaculatorServiceHandler> processor = new CaculatorService.Processor<CaculatorServiceHandler>(
          new CaculatorServiceHandler());
      TServerTransport serverTransport = new TServerSocket(8888);
      org.apache.thrift.server.TThreadPoolServer.Args targs = new org.apache.thrift.server.TThreadPoolServer.Args(serverTransport);
      targs.processor(processor);
      targs.transportFactory(new TFramedTransport.Factory());
      targs.protocolFactory(new TCompactProtocol.Factory());
      TThreadPoolServer server = new TThreadPoolServer(targs);
      System.out.println("Starting the TThreadPoolServer...");
      server.serve();

      TTransport transport = new TFramedTransport(new TSocket("localhost", 8888));
      transport.open();
      TProtocol protocol = new TCompactProtocol(transport);
      CaculatorService.Client client = new CaculatorService.Client(protocol);
      transport.close();

2.同步非阻塞

      CaculatorService.Processor<CaculatorServiceHandler> processor = new CaculatorService.Processor<CaculatorServiceHandler>(
          new CaculatorServiceHandler());
      TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(8888);
      org.apache.thrift.server.TNonblockingServer.Args targs = new org.apache.thrift.server.TNonblockingServer.Args(serverTransport);
      targs.processor(processor);
      targs.transportFactory(new TFramedTransport.Factory());
      targs.protocolFactory(new TCompactProtocol.Factory());
      TNonblockingServer server = new TNonblockingServer(targs);
      System.out.println("Starting the TNonblockingServer...");
      server.serve();

      CaculatorService.Processor<CaculatorServiceHandler> processor = new CaculatorService.Processor<CaculatorServiceHandler>(
          new CaculatorServiceHandler());
      TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(8888);
      org.apache.thrift.server.THsHaServer.Args targs = new org.apache.thrift.server.THsHaServer.Args(serverTransport);
      targs.processor(processor);
      targs.transportFactory(new TFramedTransport.Factory());
      targs.protocolFactory(new TCompactProtocol.Factory());
      TServer server = new THsHaServer(targs);
      System.out.println("Starting the THsHaServer...");
      server.serve();
      CaculatorService.Processor<CaculatorServiceHandler> processor = new CaculatorService.Processor<CaculatorServiceHandler>(
          new CaculatorServiceHandler());
      TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(8888);
      org.apache.thrift.server.TThreadedSelectorServer.Args targs = new org.apache.thrift.server.TThreadedSelectorServer.Args(
          serverTransport);
      targs.processor(processor);
      targs.transportFactory(new TFramedTransport.Factory());
      targs.protocolFactory(new TCompactProtocol.Factory());
      TServer server = new TThreadedSelectorServer(targs);
      System.out.println("Starting the TThreadedSelectorServer...");
      server.serve();

      TTransport transport = new TFramedTransport(new TSocket("localhost", 8888));
      transport.open();
      TProtocol protocol = new TCompactProtocol(transport);
      CaculatorService.Client client = new CaculatorService.Client(protocol);
      transport.close();

3.异步,注册回调

    try {
      TProtocolFactory protocolFactory = new TCompactProtocol.Factory();
      TAsyncClientManager clientManager = new TAsyncClientManager();
      TNonblockingSocket transport = new TNonblockingSocket("localhost", 8888);
      CaculatorService.AsyncClient client = new CaculatorService.AsyncClient(protocolFactory, clientManager, transport);
      Random random = new Random();
      for (int i = 0; i < 10; ++i) {
        int x = random.nextInt(1000), y = random.nextInt(1000);
        System.out.println("" + x + " + " + y + " = ?");
        try {
          client.add(x, y, new MyCallback());
          Thread.sleep(100);
        } catch (TException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        } catch (InterruptedException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
      transport.close();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

class MyCallback implements AsyncMethodCallback<CaculatorService.AsyncClient.add_call> {

  @Override
  public void onComplete(add_call arg0) {
    // TODO Auto-generated method stub
    try {
      System.out.println(arg0.getResult());
    } catch (TException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

  @Override
  public void onError(Exception arg0) {
    // TODO Auto-generated method stub
    System.out.println(arg0.toString());
  }
  
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值