2016-07-18 2 views
0

단일 (96 램) 기계에서 3 RMI 서버를 실행하려고합니다. 세 가지 다른 컴퓨터 클라이언트가 호출하지만 세 클라이언트에 대해 세 가지 다른 포트 번호를 지정하고 바인드 된 개체 이름도 세 클라이언트 모두 서로 다릅니다.하나의 바인드 된 rmi 이름을 여러 클라이언트에서 사용할 수 있습니까?

첫 번째 2 명의 고객에게는 출력이 있고 갈증에 대해서는 출력이 없습니다. "Null 포인터 예외"만 클라이언트쪽에 있습니다. 서버 측에서는 3 개의 서버 모두에서 -Xm250m을 -Xmx 20g로 지정했습니다. 클라이언트에는 모두 -Xmx6g를 부여한 8GB RAM이 있습니다.

+0

무엇이 당신 질문입니까? – xenteros

+0

두 명 이상의 클라이언트가 동시에 RMI 서버에 액세스 할 수 없음 –

+0

아직 질문이 없습니다. – xenteros

답변

1

두 개 이상의 레지스트리 인스턴스를 동일한 시스템에서 실행하려면 사용자 정의 된 RMIRegistry 포트를 사용해야합니다. 이 완전하게 동작하는 RMI 서버 및 클라이언트의 예를 참조 해, 같은 리모트 오브젝트 인스턴스에 접속하는 복수의 클라이언트를 실행할 수 있습니다. 서비스 구현의 내부 동작을 동기화하는 것을 잊지 마십시오.

다음은 서버 시스템에서 실행되는 원격 서비스 인터페이스 및 구현입니다.

import java.rmi.*; 
public interface CounterService extends Remote { 
    public void setValue(String value) throws RemoteException; 
    public String getValue() throws RemoteException; 
} 

- - - - - - 

import java.rmi.*; 
public class CounterServiceImpl implements CounterService { 
    private int callCount=0; 
    private String name; 
    private String value; 

    public CounterServiceImpl(String name) { 
     this.name=name; 
     this.value=""; 
    } 

    public synchronized void setValue(String value) throws RemoteException { 
     callCount++; 
     this.value=value; 
    } 

    public synchronized String getValue() throws RemoteException { 
     callCount++; 
     return String.format("%s (name=%s, callcount=%d)", value, name, callCount); 
    } 
} 

다음은 RMI 클라이언트 및 서버 구현입니다.

import java.util.*; 
import java.rmi.*; 
import java.rmi.registry.Registry; 
import java.rmi.registry.LocateRegistry; 
import java.rmi.server.UnicastRemoteObject; 
import java.rmi.server.ExportException; 

public class RMITest1Client { 

    private static Random randomInt = new Random(); 
    public static int getRandomInt(int min, int max) { 
     int range = (max - min) + 1; 
     return randomInt.nextInt(range) + min; 
    } 

    public static void main(String[] args) throws Exception { 
     String rmiEndPoint = args[0]; 
     String serviceName = args[1]; 
     CounterService counter = (CounterService)Naming.lookup(rmiEndPoint+"/"+serviceName); 
     System.out.println("Connected to " + rmiEndPoint+"/"+serviceName); 

     for(int idx=0; idx<10; idx++) { 
      System.out.println("getValue="+counter.getValue()); 
      Thread.sleep(getRandomInt(1, 5)*1000); 
      counter.setValue("val"+getRandomInt(100, 999)); 
      Thread.sleep(getRandomInt(1, 5)*1000); 
     } 
    } 

} 

- - - - - - 

import java.rmi.*; 
import java.rmi.registry.Registry; 
import java.rmi.registry.LocateRegistry; 
import java.rmi.server.UnicastRemoteObject; 
import java.rmi.server.ExportException; 

public class RMITest1Server { 

    public static void main(String[] args) throws Exception { 
     // create the RMIregistry service 
     int port = Integer.parseInt(args[0]); 
     Registry registry; 
     try { 
      System.out.println("RMIRegistry on port " + port); 
      registry = LocateRegistry.createRegistry(port); 
     } catch (ExportException ex) { 
      // registry may already be created by another process, 
      // get reference to an existing registry instance. 
      System.out.println("Creating registry failed, try to connect an existing registry, ex="+ex.getMessage()); 
      registry = LocateRegistry.getRegistry(port); 
     } 

     CounterService counter = new CounterServiceImpl("counter1"); 
     UnicastRemoteObject.exportObject(counter, port); 
     registry.rebind("counter1", counter); 

     counter = new CounterServiceImpl("counter2"); 
     UnicastRemoteObject.exportObject(counter, port); 
     registry.rebind("counter2", counter); 

     System.out.println("Running..."); 
     Thread.sleep(30000); 

     // close registry objects 
     for(String serviceName : registry.list()) { 
      try { 
       System.out.println("RMIRegistry unbind " + serviceName); 
       Remote obj = (Remote)registry.lookup(serviceName); 
       UnicastRemoteObject.unexportObject(obj, true); 
      } catch (Exception ex) { } 
      try { registry.unbind(serviceName); } catch (Exception ex) { } 
     } 
     System.out.println("RMIRegistry closed"); 
     System.exit(0); // mandatory if RMIRegistry was started in this JVM instance 
    } 

} 

다음은 서버와 클라이언트를 실행하는 테스트 스크립트입니다.

**goServer.bat** 
@SET /p port=Server port (2222, 3333, 0=exit): 
@IF "0"=="%port%" GOTO :EOF 
java -cp "./lib/*;" RMITest1Server %port% 
pause 

- - - - - - 

**goClient.bat** 
@SET /p port=Server port (2222, 3333, 0=exit): 
@IF "0"=="%port%" GOTO :EOF 
@SET /p service=Service index (1,2): 
java -cp "./lib/*;" RMITest1Client rmi://127.0.0.1:%port% counter%service% 
pause 
+0

보안 정책 파일은 클라이언트와 서버 측에서 필수입니까? –

+0

나는 정책 파일을 편집 한 적이 없기 때문에 나는 그렇게 생각하지 않는다. – Whome

관련 문제