设为首页 加入收藏

TOP

Java RMI的例子
2014-11-24 02:45:48 来源: 作者: 【 】 浏览:0
Tags:Java RMI 例子

参考别人的程序后写了个自己的RMI程序


RMI,远程方法调用(Remote Method Invocation)是Enterprise
JavaBeans的支柱,是建立分布式Java应用程序的方便途径。RMI是非常容易使用的,但是它非常的强大。
RMI的基础是接口,RMI构架基于一个重要的原理:定义接口和定义接口的具体实现是分开的。


java 代码
package org.itrun.remote;

import java.rmi.Remote;
import java.rmi.RemoteException;


/**
* 远程接口
* @author jiangzhen
*
*/
public interface TestInterfactRemote extends Remote{

public String add(String a,String b) throws RemoteException;

public String add() throws RemoteException;

}





package org.itrun.remote;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;


/**
* 接口的实现
* @author jiangzhen
*
*/
public class TestInterfaceRemoteImpl extends UnicastRemoteObject implements
TestInterfactRemote {




public TestInterfaceRemoteImpl() throws RemoteException {
super();
}

public String add(String a, String b) throws RemoteException {
return a+b;
}

public String add() throws RemoteException {
return "Hello Word";
}

}





package org.itrun.server;

import java.rmi.Naming;

import org.itrun.remote.TestInterfaceRemoteImpl;
import org.itrun.remote.TestInterfactRemote;


/**
* 服务器端
* @author jiangzhen
*
*/
public class Server{


public Server() {
try {
TestInterfactRemote testInterfactRemote = new TestInterfaceRemoteImpl();
Naming.rebind("rmi://10.0.0.123/server", testInterfactRemote);
} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String args[]) {
new Server();
}
}




package org.itrun.client;

import java.rmi.Naming;

import org.itrun.remote.TestInterfactRemote;


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Linux下的ping程序源代码 下一篇透过 Linux 内核看无锁编程

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: