2012-07-30 2 views
1

어떤 스레드가 특정 메서드를 실행하는지 이해하는 데 약간의 어려움이 있습니다. 서버 측에 어떤 메소드가 있습니까? 나는 RMI가 처음이다.RMI : 스레드 생성 방법

의 HelloClient :

public class HelloClient 
{ 
    Random rand = new Random(); 

    public static void main(String[] args) 
    { 
     Thread.currentThread().setName("Thread of a client"); 
     if(System.getSecurityManager() == null) 
     { 
      System.setSecurityManager(new RMISecurityManager()); 
     } 

     HelloClient hc = new HelloClient(); 
     hc.methodToMeasureEnglish(); 
    } 


    void methodToMeasureEnglish() 
    { 
     try 
     { 
      Thread.sleep(Math.abs(rand.nextInt()%4000)); 
      Registry reg = LocateRegistry.getRegistry("localhost", 6666); 
      HelloIF hello = (HelloIF) reg.lookup("HELLO"); 
      System.out.println(hello.sayHelloEnglish()); 
     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); 
     } 
    } 
} 

안녕하세요 :

public class Hello extends UnicastRemoteObject implements HelloIF 
{ 
    public Hello(String name) throws RemoteException 
    { 
     try 
     { 
      Registry registry = LocateRegistry.createRegistry(6666); 
      registry.rebind(name, this); 
     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); 
     } 
    } 


    public String sayHelloEnglish() 
    { 
     return "GOOD MORNING"; 
    } 
} 

는 HelloIF

public interface HelloIF extends Remote 
{ 
    public String sayHelloEnglish() throws RemoteException; 
} 

HelloServer에

public class HelloServer 
{ 
     public static void main(String[] args) throws Exception 
     { 
      Thread.currentThread().setName("Server Thread"); 
      if(System.getSecurityManager() == null) 
      { 
       System.setSecurityManager(new RMISecurityManager()); 
      } 
      Hello myObject = new Hello("HELLO"); 
      System.out.println("Server is ready..."); 
     } 

} 

RMI를 사용하고 있습니까?

나는 모든 로그가 클라이언트 스레드에서 온 AspectJ의 클래스

@Aspect 
public class MeasureAspect 
{ 
    private static Logger logger = Logger.getLogger(MeasureAspect.class); 

    @Around("call(void method*())") 
    public Object condition2(ProceedingJoinPoint joinPoint) throws Throwable 
    { 
     PropertyConfigurator.configure("log4j.properties"); 
     Object res = joinPoint.proceed(); 
     logger.info("Thread method " + Thread.currentThread().getName());  
     return res; 
    } 

    @Around("call(String say*())") 
    public Object condition1(ProceedingJoinPoint joinPoint) throws Throwable 
    { 
     PropertyConfigurator.configure("log4j.properties"); 
     Object res = joinPoint.proceed(); 
     logger.info("Thread say " + Thread.currentThread().getName()); 
     return res; 
    } 

} 

을 추가했다. 마녀 스레드가 내 방법을 실행한다고 설명해 주시겠습니까?

답변

1

RMI 사양에서는 어떤 스레드 원격 메소드가 실행되는지에 대해 어떤 가정도하지 않는다고 말합니다. 특히 이것은 다음을 의미합니다.

  1. 단일 스레드라고 가정 할 수 없습니다.
  2. 동일한 클라이언트 스레드에서 연속 호출이 동일한 서버 스레드에서 실행된다고 가정 할 수 없습니다.
  3. 모든 것이 주 스레드에서 실행될 것이라고 가정 할 수는 없습니다.

실제로 서버에서 스레드 풀링 (Oracle JVM은 그렇게하지 않지만 IBM이하는 것처럼 보임) 및/또는 클라이언트에서 콜렉션 풀링 (Oracle JVM은 그렇게하지 않습니다. IBM에 대해 알아야 함), 각 메소드 호출은 자체 스레드에서 실행됩니다.