2013-06-26 2 views
0

내 응용 프로그램은 클라이언트에서 JDBC를 사용하여 데이터베이스의 데이터를 조작하라는 요청에 rmi를 사용합니다. 그 스켈레톤은 각 클라이언트의 작업 요청에 대한 스레드를 실행하고 싶습니다.RMI 멀티 스레드 스켈 톤

public MySkeleton implement MyInterface { 
    public string method1() { 
     myThread.start(); 
    } 

또는 그 밖의 것이 필요한 항목이 있습니까?

+0

확실히'run()'이 아닙니다. 스레드는'Thread # start()'로 시작합니다. –

+0

네, 혼란 스럽습니다. – giozh

+0

좋아요, 이제 쓰레드가 시작해서 거기에서 오버라이드 한'run' 메소드 나 생성자에서 전달한'Runnable'의 쓰레드 중 하나를 실행합니다. –

답변

3

: 가능한 간단한 서버와 그것을 시도, 당신은 새로운 클라이언트를 연결할 때마다 항상 서버 직선에 연결할 수 있는지 볼 것이다 의뢰.

RMI는 이미이를 수행합니다.

내가 아니 그렇게하지

처럼 뭔가에 불과 할 필요가 없습니다. 그냥 정상적으로 메소드를 작성하십시오. RMI는이를 다중 스레드합니다.

+1

+1 항상 클래식하고 맑은 대답. –

4

특별한 조치를 취할 필요가 전혀없는 RMI 프레임 워크는 새로운 스레드를 자동으로 돌려줍니다. 내가 그 골격은 각 클라이언트의 작업에 대한 스레드를 실행하려는

public interface Server extends java.rmi.Remote { 
    void doIt() throws java.rmi.RemoteException; 
} 

public class ServerImpl extends java.rmi.server.UnicastRemoteObject implements Server { 
    public ServerImpl() throws java.rmi.RemoteException { 
      super(); 
    } 

    public void doIt() { 
      System.out.println ("Starting in " + Thread.currentThread().getName()); 
      try { Thread.sleep(10000); } catch(InterruptedException t) {} 
      System.out.println ("Stopping in " + Thread.currentThread().getName()); 
    } 

    public static void main (String[] argv) throws Exception { 
      java.rmi.registry.LocateRegistry.createRegistry(1099); 
      java.rmi.Naming.rebind ("//localhost/Server", new ServerImpl()); 
    } 
} 

public class Client { 
    public static void main(String[] argv) throws Exception { 
      ((Server) java.rmi.Naming.lookup("Server")).doIt(); 
    } 
}