-
定义远程接口:
-
123456
package
com.guojje;
import
java.rmi.Remote;
import
java.rmi.RemoteException;
public
interface
IHello
extends
Remote {
public
int
helloWorld()
throws
RemoteException;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
package com.guojje; import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; public class Hello extends UnicastRemoteObject implements IHello { private static final long serialVersionUID = 1L; private int index = 0 ; protected Hello() throws RemoteException { } @Override public int helloWorld(){ System.out.println( "Hello!" ); return ++index; } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
package com.guojje; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; public class HelloServer { public static void main(String args[]) { try { IHello rhello = new Hello(); Registry registry = LocateRegistry.createRegistry( 8888 ); registry.bind( "test" , rhello); System.out.println( "Remote Hello Object is bound sucessfully!" ); } catch (Exception e) { e.printStackTrace(); } } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
package com.guojje; import java.rmi.Naming; public class HelloClient { public static void main(String args[]) { try { for ( int i = 0 ; i < 5 ; i++) { IHello rhello = (IHello) Naming .lookup( "rmi://localhost:8888/test" ); System.out.println(rhello.helloWorld()); } } catch (Exception e) { e.printStackTrace(); } } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
package com.guojje; import java.io.Serializable; import java.rmi.RemoteException; public class Hello implements IHello,Serializable { private static final long serialVersionUID = 1L; private int index = 0 ; protected Hello() throws RemoteException { } @Override public int helloWorld(){ System.out.println( "Hello!" ); return ++index; } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
package com.guojje; import java.rmi.Naming; public class HelloClient { public static void main(String args[]) { try { for ( int i = 0 ; i < 5 ; i++) { IHello rhello = (IHello) Naming .lookup( "rmi://localhost:8888/test" ); System.out.println(rhello.getClass()); System.out.println(rhello.helloWorld()); } } catch (Exception e) { e.printStackTrace(); } } } |
1
2
3
4
5
6
7
8
9
10
|
package com.guojje; public class HelloServer { public static void main(String args[]) { try { new Hello(); } catch (Exception e) { e.printStackTrace(); } } } |
1
2
3
4
5
|
protected UnicastRemoteObject( int port) throws RemoteException { this .port = port; exportObject((Remote) this , port); } |
1
2
3
4
5
|
public static Remote exportObject(Remote obj, int port) throws RemoteException { return exportObject(obj, new UnicastServerRef(port)); } |
1
2
3
4
5
6
7
8
9
10
11
12
|
/** * Exports the specified object using the specified server ref. */ private static Remote exportObject(Remote obj, UnicastServerRef sref) throws RemoteException { // if obj extends UnicastRemoteObject, set its ref. if (obj instanceof UnicastRemoteObject) { ((UnicastRemoteObject) obj).ref = sref; } return sref.exportObject(obj, null , false ); } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
/** * Export this object, create the skeleton and stubs for this * dispatcher. Create a stub based on the type of the impl, * initialize it with the appropriate remote reference. Create the * target defined by the impl, dispatcher (this) and stub. * Export that target via the Ref. */ public Remote exportObject(Remote impl, Object data, boolean permanent) throws RemoteException { Class implClass = impl.getClass(); Remote stub; try { stub = Util.createProxy(implClass, getClientRef(), forceStubUse); } catch (IllegalArgumentException e) { throw new ExportException( "remote object implements illegal remote interface" , e); } if (stub instanceof RemoteStub) { setSkeleton(impl); } Target target = new Target(impl, this , stub, ref.getObjID(), permanent); ref.exportObject(target); hashToMethod_Map = hashToMethod_Maps.get(implClass); return stub; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
/** * Export the object so that it can accept incoming calls. */ public void exportObject(Target target) throws RemoteException { /* * Ensure that a server socket is listening, and count this * export while synchronized to prevent the server socket from * being closed due to concurrent unexports. */ synchronized (this) { listen(); exportCount++; } /* * Try to add the Target to the exported object table; keep * counting this export (to keep server socket open) only if * that succeeds. */ boolean ok = false ; try { super .exportObject(target); ok = true ; } finally { if (!ok) { synchronized ( this ) { decrementExportCount(); } } } } |
1
|
super .exportObject(target); |
1
2
3
4
5
6
7
|
/** * Export the object so that it can accept incoming calls. */ public void exportObject(Target target) throws RemoteException { target.setExportedTransport( this ); ObjectTable.putTarget(target); } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
/** * Binds the name to the specified remote object. * @exception RemoteException If remote operation failed. * @exception AlreadyBoundException If name is already bound. */ public void bind(String name, Remote obj) throws RemoteException, AlreadyBoundException, AccessException { checkAccess( "Registry.bind" ); synchronized (bindings) { Remote curr = bindings.get(name); if (curr != null ) throw new AlreadyBoundException(name); bindings.put(name, obj); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
case 2 : // lookup(String) { java.lang.String $param_String_1; try { java.io.ObjectInput in = call.getInputStream(); $param_String_1 = (java.lang.String) in.readObject(); } catch (java.io.IOException e) { throw new java.rmi.UnmarshalException( "error unmarshalling arguments" , e); } catch (java.lang.ClassNotFoundException e) { throw new java.rmi.UnmarshalException( "error unmarshalling arguments" , e); } finally { call.releaseInputStream(); } java.rmi.Remote $result = server.lookup($param_String_1); try { java.io.ObjectOutput out = call.getResultStream( true ); out.writeObject($result); } catch (java.io.IOException e) { throw new java.rmi.MarshalException( "error marshalling return" , e); } break ; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
|
/** * Checks for objects that are instances of java.rmi.Remote * that need to be serialized as proxy objects. */ protected final Object replaceObject(Object obj) throws IOException { if ((obj instanceof Remote) && !(obj instanceof RemoteStub)) { Target target = ObjectTable.getTarget((Remote) obj); if (target != null ) { return target.getStub(); } } return obj; } |