RMI Sample in MessageHandler Proj

 1 //Interface class: 
2 // RemoteServiceCentral.java
3 //Implementation:
4 // RemoteServiceCentralImpl.java
5
6 import java.rmi.registry.LocateRegistry;
7 import java.rmi.registry.Registry;
8
9 String hostName = null;
10 try {
11 hostName = InetAddress.getLocalHost().getHostName();
12 } catch (UnknownHostException e) {
13 hostName = "localhost";
14 }
15
16 String serviceCentralName, handlerName;
17 int portNo;
18 String rmiURL = "rmi://"+hostName+":"+port+"/"+serviceCentralName;
19
20
21 //Client Side code:
22 Registry registry = LocateRegistry.getRegistry(port);
23 RemoteServiceCentral svCentral = (RemoteServiceCentral) registry.lookup(rmiURL);
24 svCentral.startHandler(handlerName, null);
25
26 //Server side startup:
27 RemoteServiceCentral remoteServer = new RemoteServiceCentralImpl();
28 Registry registry = LocateRegistry.createRegistry(portNo);
29 try {
30 registry.bind(rmiURL, remoteServer);
31 } catch (AlreadyBoundException e) {
32 registry.rebind(rmiURL, remoteServer);
33 }
34
35 //Server side shutdown:
36 Registry registry = LocateRegistry.getRegistry(portNo);
37 registry.unbind(rmiURL);

 

posted @ 2012-01-11 17:23  万法自然~  阅读(202)  评论(0)    收藏  举报