远程方法调用(RMI) - 服务器如何知道客户端调用的内容?

时间:2013-01-01 20:05:04

标签: java rmi remoteobject

我有一个程序,其中有这些类:
的RMIServer,
RMIClient,
RMIImplementation,
RMIInterface

如下:

的RMIServer:

public static void main ( String args[] ) throws Exception
{
  System.out.println( "RMI Server started" ) ;

  String codebase = "http://localhost:8080/rmi/" ; 
  String name     = "RMIInterface"               ;

  System.setProperty ( "java.rmi.server.codebase" , codebase  ) ;

  RMIImplementation obj  = new RMIImplementation()                                    ;
  RMIInterface      stub = (RMIInterface) UnicastRemoteObject.exportObject( obj , 0 ) ;

  LocateRegistry.getRegistry().bind( name , stub ) ;

  System.out.println( "Done!" ) ;

}  

RMIClient:

public static void main ( String args[] ) throws Exception
{
    String host = "localhost"    ;
    String name = "RMIInterface" ;


    Registry     registry = LocateRegistry.getRegistry( host )     ;
    RMIInterface i        = (RMIInterface) registry.lookup( name ) ;

    System.out.println("RMI service call result:");
    for(Candidate c : list){
        if(c.status == Candidate.Eligibility.ELIGIBLE ){
            System.out.println( "  Issued a license to " + i.issueLicense(c.name, c.age) ) ;   
        }
    } 

    System.out.println( "License Server finished" ) ;
}

RMIInterface:

public interface RMIInterface extends Remote
{
  String issueLicense ( String name , int age ) throws RemoteException ;
}

RMIImplementation:

public class RMIImplementation implements RMIInterface {

    public RMIImplementation() {
        System.out.println("RMIImplementation instance created and ready to serve");
    }

    @Override
    public String issueLicense(String name, int age) {
        return name + " (" + age + ")";
    }
}

(此程序用于测试目的。测试在一台机器上完成。)

好吧,客户端调用远程对象,发送一些参数。

我希望RMIServer打印出从客户端到远程对象的调用摘要。我该怎么做才能访问这些信息?

我希望输出看起来像这样:

RMI Server started
RMIImplementation instance created and ready to serve
RMI service called for : 
  tonny (30)
  john (26)

1 个答案:

答案 0 :(得分:1)

你的问题到处都是,但你的实际目标可以通过

来实现

-Djava.rmi.server.logCalls=true

在服务器上。

相关问题