转载: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;
}
|
相关推荐
1. **导出远程对象**: 开发者创建实现远程接口的类,并实例化一个远程对象,然后使用`java.rmi.Naming.rebind()`或`UnicastRemoteObject.exportObject()`将其导出到网络上。 2. **注册远程对象**: 将导出的对象注册...
4. ** stubs 和 skeletons**:在RMI中,远程对象的引用在客户端是通过stubs(桩)表示的,而实际的远程调用则由serverside的skeleton(骨架)处理。现代的JVM已经不再需要手动生成stubs和skeletons,它们由Java...
远程方法调用(Remote Method Invocation,RMI)是Java提供的一种分布式计算技术,它允许一个Java对象调用网络另一端的Java对象的方法。RMI在Java应用中的作用是构建跨网络的、分布式的对象系统,使得开发者可以像...
- ** stub 和 skeleton**:RMI系统使用代理(stub)对象作为远程对象的本地代表,而skeleton对象则在远程服务器上接收和分发调用。在现代Java版本中,这些组件由JDK自动处理。 2. **RMI步骤**: - **注册远程对象...
4. **客户端(Client)**:客户端通过RMI机制获取远程对象的引用,然后调用远程方法。这通常涉及到反序列化过程,因为远程方法的调用结果需要通过网络传输。 5. **服务器端(Server)**:服务器端运行远程对象,...
Java RMI(Remote Method Invocation,远程方法调用)是Java平台提供的一种分布式计算技术,它允许在不同的Java虚拟机之间透明地调用对象的方法。在RMI架构中,客户端能够像调用本地对象一样调用远程服务器上的对象...
远程方法调用(RMI,Remote Method Invocation)是Java提供的一种用于在分布式环境中调用远程对象的方法。RMI的核心思想是使客户端可以像调用本地对象一样调用远程对象,实现这一目标的关键在于隐藏了网络通信的细节...
Java RMI(Remote Method Invocation,远程方法调用)是Java平台提供的一种分布式计算技术,它允许在不同的Java虚拟机之间进行方法调用,仿佛这些方法是在本地对象上执行一样。这个技术极大地简化了构建分布式应用的...
远程方法调用(Remote Method Invocation,简称RMI)是Java平台上的一个核心特性,它允许Java对象在不同的JVM之间进行交互。RMI为分布式计算提供了一个简单而强大的模型,使得开发者可以像调用本地方法一样调用远程...
远程方法调用(Remote Method Invocation,简称RMI)是Java平台提供的一种机制,它允许一个Java对象调用另一个在不同Java虚拟机(JVM)上的对象的方法。RMI是Java分布式计算的核心技术,主要用于构建分布式系统,...
在提供的`动态代理与RMI远程调用.ppt`中,可能会详细解释这两个概念,通过PPT的讲解和实例,可以更直观地理解动态代理和RMI的工作原理。同时,`src`目录下的源码文件则提供了具体的实现示例,读者可以通过阅读代码,...
RMI(Remote Method Invocation,远程方法调用)是Java平台上的一个核心特性,它允许Java对象在不同的JVM之间进行通信,实现分布式计算。RMI系统由两部分组成:服务端(Server)和客户端(Client)。服务端提供远程...
RMI远程方法调用是Java平台上的一个关键特性,它允许Java对象在不同的JVM之间进行通信,从而实现分布式计算。RMI的核心理念是让开发者能够像调用本地方法一样调用远程对象的方法,简化了分布式系统的设计和实现。 *...
远程接口定义了可以在远程对象上调用的方法,这些方法必须是声明为`remote`的,并可能抛出`java.rmi.RemoteException`。远程实现是实现了远程接口的具体类,它包含了实际的业务逻辑。最后,RMIServer是运行远程对象...
远程方法调用(Remote Method Invocation,RMI)是Java平台提供的一种用于在分布式环境中执行远程对象的方法调用机制。RMI允许一个Java对象在一台计算机上执行另一个Java对象的方法,即使该对象位于另一台计算机上。...
- RMI是Java中的一个特性,允许对象在不同的网络节点之间进行交互,执行远程对象的方法。 - RMI调用对最终用户是透明的,意味着用户无需关心方法实际在哪里执行,只需像调用本地方法一样调用远程方法。 - 所有...
4. **导出远程对象**:使用`java.rmi.Naming`或`java.rmi.server.UnicastRemoteObject`类将实现的远程对象导出到网络,使其可被远程访问。 5. **客户端调用**:客户端通过RMIServer获取远程对象的引用,然后就可以...
4. **导出远程对象**:使用`java.rmi.server.UnicastRemoteObject`的`exportObject()`方法将`BankServiceImpl`实例导出为远程对象,使其可以被远程调用。 5. **启动RMID服务**:确保服务器上运行着RMID守护进程,它...