RMI原理揭秘之远程对象
来源:互联网 发布:怀化干部在线网络 编辑:程序博客网 时间:2024/06/07 03:34
定义远程接口:
- 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;
}
0 0
- RMI原理揭秘之远程对象
- RMI原理揭秘之远程对象
- RMI原理揭秘之远程对象
- RMI原理揭秘之远程对象
- RMI原理揭秘之远程方法调用
- RMI原理揭秘之安全通信
- RMI 远程对象工程设计模式
- Java远程对象调用 RMI
- Java RMI远程对象调用
- java 远程通讯之 rmi
- 用Spring动态调用RMI远程对象
- RMI-IIop远程接口和对象
- (一) spring 远程服务之rmi
- Java之RMI(远程方法调用)
- JAVA序列化之RMI远程调用
- javaEE之远程方法调用(RMI)
- 《揭秘QTP之Reporter对象》
- ATL揭秘之“对象创建”
- 第一次接触Spring
- 使用MAP文件快速定位程序崩溃代码行
- 关于xpath相对路径前加点与不加点的区别
- 饥饿营销
- springMVC +mybites 中SqlSessionDaoSupport 的使用
- RMI原理揭秘之远程对象
- c++栈和堆
- css之缩进
- Leetcode 动态规划DP类题目
- 堆:欢乐和痛苦
- Swift 造个类型不是梦-白话Swift类型创建
- 2.5.4 进程同步,互斥的区别和联系
- Swift Swift与C语言指针友好合作
- To_Date函数用法