转载:http://guojuanjun.blog.51cto.com/277646/1423392/
-
定义远程接口:
-
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;
}
|
相关推荐
RMI远程方法调用RMI远程方法调用RMI远程方法调用RMI远程方法调用RMI远程方法调用
RMI采用JRMP(Java Remote Method Protocol)通讯协议,是构建在TCP/IP协议上的一种远程调用方法。...同时,RMI定义了一组远程接口,可以用于生成远程对象。客户机可以像本地对象的方法一样用相同的语法调用远程对象。
rmi原理及应用ppt,关于rmi的原理和例子,方便大家参考
这个是基于C#的 RMI远程方法调用 这个是基于C#的 RMI远程方法调用这个是基于C#的 RMI远程方法调用
JavaRMI的原理和实现方法 分布式对象计算 来源于青岛大学学报
java jdk1.8;...掌握远程过程调用原理,基于java RMI进行远程编程和控制。要求定义远程接口类及实现类:定义相应的处理方法;客户端利用RMI实现远程调用服务。同时,在在两台机器之间验证结果正确。
用java的rmi实现的 远程调用。希望对阁下有用
RMI原理.xls
RMI原理及实现 ... RMI是远程方法调用的简称,象其名称暗示的那样,它能够帮助我们查找并执行远程对象的方法。通俗地说,远程调用就象将一个class放在A机器上,然后在B机器中调用这个class的方法。
改资料清楚的讲解了RMI的原理,被结合实例讲解,浅显易懂
RMI(远程方法调用)简单代码例子 RMI(远程方法调用)简单代码例子
rmi 远程方法调用 客户端
Java rmi实现将文件或者图片从客户端上传到远程服务器
RMI远程文件传输
RMI 的demo,多个对象。
本文档主要讲述的是Java 远程方法调用RMI参数详解;根据RMI参数意义,可以归结为以下几点,我们可以根据这几点通过优化GC, 网络等待,流传输协议(http/rmi special socket)等方面来优化RMI。
RMI+Zookeeper实现远程调用框架https://blog.csdn.net/kingcat666/article/details/78579259
用RMI编写一个分布式应用,核心有以下三方面: 定位远程对象 1. 一个应用可以利用RMI的名字服务功能注册器远程对象。 2. 可以象操作普通对象...RMI允许调用者向远程对象传递一个对象,因此 RMI提供这种装载对象的机制。
spring RMI 远程接口调用 包含服务端客户端程序,可完整运行
通过动态代理和socket编程技术实现RMI,远程对象方法的调用。