I am writing a server like below
public class Server<T extends RequestHandler> {
public void start() {
try{
this.serverSocket = new ServerSocket(this.port, this.backLog);
} catch (IOException e) {
LOGGER.error("Could not listen on port " + this.port, e);
System.exit(-1);
}
while (!stopTheServer) {
socket = null;
try {
socket = serverSocket.accept();
handleNewConnectionRequest(socket);
} catch (IOException e) {
LOGGER.warn("Accept failed at: " + this.port, e);
e.printStackTrace();
}
}
}
protected void handleNewConnectionRequest(Socket socket) {
try {
executorService.submit(new T(socket));
} catch (IOException e) {
e.printStackTrace();
}
}
}
But in the handleNewConnectionRequest(...)
method, I can't create an instance of T as it is actually not a class. Also I can't use the method mentioned here as I want to pass the socket
instance so that the request handler can get OutputStream
and InputStream
on the socket
.
Can't I make a generic server like above and have different protocol handlers e.g
public class HttpRequestHandler extends RequestHandler {
...
}
public class FtpRequestHandler extends RequestHandler {
...
}
public class SmtpRequestHandler extends RequestHandler {
...
}
and then use them like below
Server<HttpRequestHandler> httpServer = new Server<HttpRequestHandler>();
Server<FtpRequestHandler> ftpServer = new Server<FtpRequestHandler >();
Server<SmtpRequestHandler> smtpServer = new Server<SmtpRequestHandler >();