简体   繁体   中英

What is the Proper way to Poll for Client Connections — Apache Thrift Server

I have very little experience with server-side programming, and I am taking on a task where I need to implement part of a Back-end Server using Apache thrift.

Right now, my application consists of a "Front-End" server and a "Back-End" server. The Front-End Server will make RPC calls to the Backend.

Right now, if I start the backend server from the command-line, I want that backend server to stay alive and, in a sense, keep polling for other nodes that want to establish a connection with it. Currently, the backend just spits out an error when it's not immediately able to connect to a front-end node. The backend code right now looks like this:

public class BENode {
public static void main(String [] args) throws Exception {

...


    TSocket sock = new TSocket(hostFE, portFE);
    TTransport transport = new TFramedTransport(sock);
    TProtocol protocol = new TBinaryProtocol(transport);
    BcryptService.Client client = new BcryptService.Client(protocol);
    transport.open();
}
}

I believe it's the last line that throws an Exception (java.net.ConnectException: Connection refused) when the front-end (FE) node is not available to connect. However, the intended behaviour is that the back-end server will stay alive and keep polling to establish a connection with the front-end node, until it finally is able to connect successfully. I am quite sure I am not supposed to use an infinite while loop with a try-catch block as that is inefficient and bad practice:

try{

     transport.open();
} catch() {

}

What is the best way to do this?

Given, I understand it right and the FE should connect the BE server. Then, the basic idea is this:

  • start the BE Thrift Server
  • have FE Thrift Client(s) connect to it

Since the BE is intended to act as a Server, we have to start a Thrift Server , not a client:

public static void simple(Calculator.Processor processor) {
  try {
    TServerTransport serverTransport = new TServerSocket(9090);
    TServer server = new TSimpleServer(new Args(serverTransport).processor(processor));
    System.out.println("Starting the simple server...");
    server.serve();
  } catch (Exception e) {
    e.printStackTrace();
  }
}

The infinite loop you are looking for is buried in server.serve(); . And if the idea is really backwards from what you wrote (you have variables named FEport ) and what you want is really to have BE connecting FE, then you have to switch all of the above: BE=client, FE=server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM