저는 현재 미적분을 수행하는 Java 응용 프로그램을 개발 중입니다. 내 응용 프로그램은 jar 파일로 배포 될 것이고 각각의 미적분을 위해 수행해야 할 것은 jar 파일을 시작하는 스크립트를 사용할 것입니다. 내 문제는 다음과 같습니다. 데이터베이스 질의를 수행하기위한 싱글 톤 클래스를 만들었지 만 두 번의 jar 실행 사이에 JVM에서 내 싱글 톤을 수정하는 방법을 알지 못합니다. 싱글 톤을 저장하고 무한히 기다리는 비어있는 클래스를 실행하려고 시도했지만, 내 항아리 프로그램을 실행할 때 싱글 톤은 검색되지 않지만 인스턴스화되지 않습니다 ... 내 코드는 다음과 같습니다.JVM에서 싱글 톤을 수정하는 방법은 무엇입니까?
메인 프로그램 JVM의 싱글 :
이/**
* Entry point for the singleton generation
* @author yann.blanc
*
*/
public class MainSingleton {
/**
* Entry point
* @param args Command line arguments, used to define the request type
*/
public static void main (String[] args) {
try {
/*Here my singleton is instantiated*/
DatabaseServiceSingleton service = DatabaseServiceSingleton.getInstance();
while (true)
{
Thread.sleep(1000);
}
} catch (ServiceException e) {
e.printStackTrace();
System.exit(0);
} catch (InterruptedException e) {
e.printStackTrace();
System.exit(0);
}
}
}
내 싱글 : 그렇지 않은 이유
public final class DatabaseServiceSingleton implements IDatabaseService {
private static DatabaseServiceSingleton instance = null;
/*PARAM and FUNCTIONS OMIT FOR A BETTER READABILITY*/
/*Constructor with exception to ensure that clients are stopped if the singleton can't be instantiated*/
private DatabaseServiceSingleton() throws ServiceException {
try {
/*Get the current path*/
String urlCourante = getClass().getProtectionDomain().getCodeSource().getLocation().getFile();
String[] tab;
if(urlCourante.contains("\\")){
tab = urlCourante.split("\\");
} else {
tab = urlCourante.split("/");
}
for(int i = 0; i < tab.length -1; i++) {
pathFolder += tab[i];
}
/*Get configuration informations*/
getConfiguration();
/*Init database connectors*/
initBaseConnexion();
System.out.println(this.getClass().getClassLoader().toString());
} catch (Exception e) {
throw new ServiceException("Problème d'entrée sortie lors de la lecture du fichier "
+ "de configuration. Le service n'a pas pu être instancié : "+e.getMessage());
}
}
/*Method for retrieve the singleton instance*/
public static synchronized DatabaseServiceSingleton getInstance() throws ServiceException {
if (instance == null) {
instance = new DatabaseServiceSingleton();
}
return instance;
}
}
나는 그것이 작동 것이라고 확신했다가 ... 당신이 나를 설명 할 수 있습니까? 도움말 :
편집 주셔서 감사합니다 : I 클래스 로더를 확인했고 항상
싱글 톤은 JVM에서 고유합니다. 하지만 2 개의 jars 실행 = 2 JVM. 당신의 필요는 정확히 무엇입니까? –
"내 항아리 프로그램을 실행할 때 싱글 톤이 검색되지는 않지만 인스턴스가 생성되지 않습니다."- 무슨 뜻인지 알 수 있습니까? 그 질문은 나에게 분명하지 않다. – Swapnil
@ArnaudDenoyelle JVM에서 항상 실행되는 의사 서비스를 만들고 내 jar 프로그램에서 액세스 할 수 있어야합니다. 전에 RMI를 사용하고 있었지만 프로그램이 로컬에서 실행되기 때문에 싱글 톤 솔루션을 시도했습니다 ... – Ko2r