CZ 2018/6/14 19:23 c.~
Java的网络编程
---RMI
1.首先需要构造一个对象Object--User 且要实现Serializable接口//因为传输的数据需要序列化
下面贴代码://
public class SystemUser implements Serializable{
private static final long serialVersionUID = 8863716205543323989L;
private int userid;
private String username;
private String password;
private String email;
private String state;
private String realname;
private String telephone;
public int getUserid() {
return userid;
}
public void setUserid(int userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getRealname() {
return realname;
}
public void setRealname(String realname) {
this.realname = realname;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
@Override
public String toString() {
return "SystemUser [userid=" + userid + ", username=" + username
+ ", password=" + password + ", email=" + email + ", state="
+ state + ", realname=" + realname + ", telephone=" + telephone
+ "]";
}
}
2.对该对象写一个接口继承Remote类,里面可以写你需要对该对象操作的方法,如查询对象信息,修改对象信息,下面贴代码,且写一个方法
public interface ISystemService extends Remote {
/**
* 传递消息给服务器端的信息显示框
* @param str
* @throws RemoteException
*/
public void showMsgToServer(String str) throws RemoteException;
}
3.写具体实现类ISystemServiceImpl去实现这个接口//并且要继承UnicastRemoteObject这个类
public class ISystemServiceImpl extends UnicastRemoteObject implements ISystemService {
public ISystemServiceImpl() throws RemoteException {
super();
// TODO Auto-generated constructor stub
}
@Override
public void showMsgToServer(String str) throws RemoteException {
// TODO Auto-generated method stub
}
}
4.搭建服务端---Server
1.首先写一个服务端接口 IServiceOperator
public interface IServiceOperator {
/**
* 启动服务器
* @throws IOException
* @throws NumberFormatException
*/
public void start();
/**
* 停止服务
*/
public void stop();
}
2.具体实现类 ServerOperatorImpl
这里我使用了设计模式--单例模式//有客户端只能访问这个服务端口//饿汉单例
public class ServerOperatorImpl implements IServiceOperator{
private static IServiceOperator instance = new ServerOperatorImpl();
private static INetService netService;
private static ISystemService systemService;
private static Registry registry;
private ServerOperatorImpl(){
}
/**
* 单例模式
* @return
*/
public static IServiceOperator getInstance(){
return instance;
}
/**
* 启动服务
* */
@Override
public void start(){
try {
registry=LocateRegistry.createRegistry(6600);//这个开启RMI服务注册通讯端口
//3.实例化业务组件
systemService=new ISystemServiceImpl();
//4.将业务组件注册到RMI服务器上(可注册多个业务组件)
Naming.rebind("rmi://127.0.0.1:6600/UserService", systemService);//绑定服务端
System.out.println("RMI Server Start!");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 停止服务
* */
@Override
public void stop(){
try {
if (registry != null) {
Naming.unbind("rmi://127.0.0.1:6600/UserService");//取消绑定
UnicastRemoteObject.unexportObject(registry, true);
registry = null;
System.out.println("RMI服务已停止!");
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NotBoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
服务端构建完成
5.搭建客户端测试
1.写客户端测试
public class RmiClient {
public static void main(String[] args) {
for(int i=1;i<3;i++){
Thread t=new Thread(new Client(i));
t.start();
}
}
}
class Client implements Runnable{
int flag=0;
Client(int flag){
this.flag=flag;
}
//客户端线程会去调用它
@Override
public void run() {
//调用远程对象,注意RMI路径与接口必须与服务器配置一致
try {
ISystemService inetservice=(ISystemService) Naming.lookup("rmi://127.0.0.1:6600/ISystemService");
inetservice.showMsgToServer("请求服务");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NotBoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
至此,RMI服务端-客户端模式就搭建好了,但是效率不是很高。
//写一个类RMIFactory,这是我在项目内为了封装性,而多写的一层封装,可以不写
public class RMIFactory {
private RMIFactory(){}
/**
* 获取系统管理端业务组件
* @return
*/
public static ISystemService getService(){
try {
return (ISystemServiceImpl) Naming.lookup("rmi://127.0.0.1:6600/UserService");//在测试类那里可以调用该方法
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NotBoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
};
return null;
}
}
}