Java 프로그램의 단일 인스턴스를 열 수있는 클래스를 만듭니다. ServerSocket을 여는 데몬 스레드를 사용합니다. TCP 포트가 이미 취해진 경우 인스턴스 생성시 예외가 발생합니다.예기치 않은 소켓이 때때로 닫히거나 열리지 않습니다.
코드는 일반적으로 Linux 및 Windows에서 작동합니다. 여기
내가 코드를 사용하고 있습니다 :
public class SingleInstaceHandler extends Thread {
private static final Logger log = Logger.getLogger(IEPLC_Tool.class);
private boolean finished = false;
@SuppressWarnings("unused")
private ServerSocket serverSocket;
/*
* Constructor
* Generate the server socket.
* If the TCP door was busy throws IOException.
*/
public SingleInstaceHandler() throws IOException {
@SuppressWarnings("unused")
ServerSocket serverSocket = new ServerSocket(44331);
this.setDaemon(true);
this.start();
log.info("Server socket initialized"); //if commented out it works
}
public void run() {
synchronized (this) {
while (!finished) {
try {
log.debug("Server socket goes to sleep");
this.wait();
log.debug("Server socket waken up");
} catch (InterruptedException e) {
log.debug("ERROR while sending SocketThread2 in wait status");
e.printStackTrace();
System.exit(-1);
}
log.info("Server socket end");
}
}
}
public synchronized void shutdown() {
log.debug("SingleInstaceHandler shutdown() caled");
finished = true;
notifyAll();
}
}
때때로 대신 포트가 바쁜 유지되지 ... 어떤 생각을?
UPPENDED 기타 시험 AFTER :
다른 많은 테스트를 실행. 포트가 다른 SW 인스턴스와 같은 것으로 가져간 경우 new ServerSocket(44331);
이 예외를 throw하지만 때때로 어떤 이유로 포트를 가져 오지 않아도이 리소스를 가져올 수없는 경우도 있습니다. 이 경우 아무런 예외도 발생하지 않고 응용 프로그램에서 원하는만큼 인스턴스를 열 수 있습니다. 어쩌면 내가 포트를 잠글 스레드를 강제로 다른 작업을 수행해야합니다 ...
어떤 생각?
감사합니다,
인트
가 어떻게 소켓이 닫혀 있는지 진단 할
인트? 왜 사용하지 않는 serverSocket 필드가 클래스에 있습니까? –
이 코드는 나에게 의미가 없습니다. ServerSocket을 인스턴스 변수에 저장하지 않으려면 다시 액세스 할 수 없습니까? – home
나는 netstat -vatn | 명령을 사용한다. grep "44331".난 내 응용 프로그램의 단일 인스턴스를 보장하기 위해 그것을 사용하는 경우 – Stefano