0

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?

1 Answers1

1

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.

JensG
  • 13,148
  • 4
  • 45
  • 55