I have a Java printing application, it prints ticket to a network printer using raw TCP sockets. This application is a REST listener, when it receive a command, it prints using one single connection to a single ethernet thermal printer.
This is my code used by a singleton service:
public class TcpPrinterWrapper implements PrinterWrapper {
private static Logger log = LoggerFactory.getLogger(TcpPrinterWrapper.class);
private PrinterOpzioni opzioni;
private TcpWrapper wrapper;
public TcpPrinterWrapper(PrinterOpzioni opzioni) {
super();
this.opzioni = opzioni;
wrapper=new TcpWrapper();
}
@Override
protected void finalize() throws Throwable {
if(wrapper!=null) try{wrapper.close();}catch (Exception e) {}
super.finalize();
}
private void openPort(){
try {
if(wrapper==null)
wrapper=new TcpWrapper();
wrapper.openPort(opzioni, opzioni.getTcpTimeout());
} catch (Exception e) {
log.error("Errore apertura porta",e);
throw new HardwareException("Porta stampante non valida o non disponibile");
}
}
@Override
public synchronized void print(byte[] content, boolean close) throws Exception {
try{
if(wrapper==null || !wrapper.isOpened()){
openPort();
}
wrapper.write(content);
if(close)
close();
}catch(Exception e){
try{
wrapper.close();
}catch(Exception e1){}
throw e;
}
}
@Override
public void close(){
try {
wrapper.close();
wrapper=null;
} catch (Exception e) {}
}
}
My question is: should it close its socket at the end of each print job? Or can a TCP socket stay opened for many hours waiting for jobs?
I don't know REST requests frequency, but I'm sure they can wait many hours but the also be called many in a minute. This is why I cannot answer, can a socket stay opened waiting for so long? In other hand can I open/close/open/close many sockets in few minutes? Should I complicate with a disconnection timer?