关于java RMI分布式程序开发实例
作者: javaboy2012
Email:yanek@163.com
qq: 1046011462
一. 服务器端
接口定义:注意必须继承Remote接口
package com.yanek.rmi.server;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.List;
public interface ChannelManager extends Remote {
public List<Channel> getChannels() throws RemoteException;
}
接口传输的对象:注意必须实现Serializable接口。
package com.yanek.rmi.server;
import java.io.Serializable;
public class Channel implements Serializable{
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Channel(int id, String name) {
super();
this.id = id;
this.name = name;
}
}
接口实现: 注意继承UnicastRemoteObject类
package com.yanek.rmi.server;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class ChannelManagerImpl extends UnicastRemoteObject implements ChannelManager {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* @param args
*/
public static void main(String[] args) {
try {
ChannelManager cm=ChannelManagerImpl.getInstance();
ChannelManager cm1=ChannelManagerImpl.getInstance();
ChannelManager cm2=ChannelManagerImpl.getInstance();
System.out.println(cm1);
System.out.println(cm2);
System.out.println(cm);
List<Channel> channels=cm.getChannels();
for(int i=0;i<channels.size();i++)
{
Channel c=channels.get(i);
System.out.println(c.getId()+"-"+c.getName());
}
} catch (RemoteException e) {
e.printStackTrace();
}
}
private static final Object lock = new Object();
private static ChannelManager instance;
public static ChannelManager getInstance() throws RemoteException {
if (instance == null) {
synchronized (lock) {
if (instance == null)
instance = new ChannelManagerImpl();
}
}
return instance;
}
private ChannelManagerImpl() throws RemoteException {
}
@Override
public List<Channel> getChannels() throws RemoteException {
List<Channel> channels=new ArrayList<Channel>();
channels.add(new Channel(1,"java"));
channels.add(new Channel(2,"php"));
channels.add(new Channel(3,"C"));
channels.add(new Channel(4,"ASP"));
System.out.println(new Date()+" getChannels method called!");
return channels;
}
}
服务启动类:
package com.yanek.rmi.server;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
public class RMIServer {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("RMI Server starting ...");
if (System.getSecurityManager() == null) {
//System.setSecurityManager(new RMISecurityManager());
}
try {
LocateRegistry.createRegistry(1099);
try {
Naming.rebind("ChannelManager", ChannelManagerImpl.getInstance());
} catch (MalformedURLException e) {
e.printStackTrace();
}
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("RMI Server ready...");
}
}
将如上四个类打包为server.jar
将如上ChannelManager和Channel类打包为server_4client.jar
二. 客户端
package com.yanek.rmi.client;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.List;
import com.yanek.rmi.server.Channel;
import com.yanek.rmi.server.ChannelManager;
public class ClientUtil {
/**
* @param args
*/
public static void main(String[] args) {
ChannelManager cm = (ChannelManager) ClientUtil.renewRMI("ChannelManager");
List<Channel> channels;
try {
channels = cm.getChannels();
for (int i = 0; i < channels.size(); i++) {
Channel c = channels.get(i);
System.out.println(c.getId() + "-" + c.getName());
}
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Remote renewRMI(String name) {
try {
if (System.getSecurityManager() == null) {
//System.setSecurityManager(new RMISecurityManager());
}
return Naming.lookup("rmi://192.168.136.1:1099/" + name);
} catch (Exception re) {
throw new IllegalStateException("bind " + name + " failure . ");
}
}
}
package com.yanek.rmi.client;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;
import com.yanek.rmi.server.Channel;
import com.yanek.rmi.server.ChannelManager;
public class ClientTest {
/**
* @param args
*/
public static void main(String[] args) {
ChannelManager cm = (ChannelManager) ClientUtil.renewRMI("ChannelManager");
List<Channel> channels=new ArrayList<Channel>();
try {
channels = cm.getChannels();
for (int i = 0; i < channels.size(); i++) {
Channel c = channels.get(i);
System.out.println(c.getId() + "-" + c.getName());
}
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
将如上两个类打包为client.jar
三. 发布和部署
服务端发布: 引用server.jar包,执行RMIServer类的Main方法
客户端调用: 引用server_4client.jar和client.jar
如下为windows下bat方式调用批处理脚本
client.bat
@echo off
set cp=config
for %%i in (lib\*.jar) do call a.bat %%i
set cp=%cp%;
echo %cp%;
java -cp %cp% com.yanek.rmi.client.ClientTest
pause
server.bat
@echo off
set cp=config
for %%i in (lib\*.jar) do call a.bat %%i
set cp=%cp%;
echo %cp%;
java -cp %cp% com.yanek.rmi.server.RMIServer
pause
辅助文件 a.bat
set cp=%cp%;%1
服务端启动截图
客户端调用:
代码和部署文件下载地址: http://download.csdn.net/detail/5iasp/5093357 (不需要积分)