服务器类:News_register_factory
Java code
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import News_apart_DAOImp.NAprt_affair_noticeDAOImp;
import News_apart_DAOImp.News_apart_DAOImp;
import News_aprt_DAO.News_register_factoryDAO;
public class News_register_factory extends UnicastRemoteObject implements
News_register_factoryDAO,Runnable {
private String item;
protected News_register_factory() throws RemoteException {
super();
// TODO Auto-generated constructor stub
}
public boolean register() throws RemoteException {
if(this.item == "newsDAOImp"){
LocateRegistry.createRegistry(8808);
News_apart_DAOImp news = new News_apart_DAOImp();
try {
Naming.rebind("//localhost:8808/news" , news);
System.out.println("\n news registered");
//registry.unbind("news");
/* UnicastRemoteObject.unexportObject(registry, true);
System.out.println(">>>>>INFO:服务停止!");
System.exit(0); //终止RMI服务程序的运行
*/
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
if(this.item == "NApart_affairImp"){
LocateRegistry.createRegistry(8809);
NAprt_affair_noticeDAOImp affair = new NAprt_affair_noticeDAOImp();
try {
Naming.rebind("//localhost:8809/Naffair" , affair);
System.out.println("\n affair registered");
/* //registry.unbind("Naffair");
UnicastRemoteObject.unexportObject(registry, true);
System.out.println(">>>>>INFO:服务停止!");
//System.exit(0); //终止RMI服务程序的运行
*
*/
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
if(this.item == "BApart_affairImp"){
LocateRegistry.createRegistry(8810);
NAprt_affair_noticeDAOImp affair = new NAprt_affair_noticeDAOImp();
try {
Naming.rebind("//localhost:8810/Baffair" , affair);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
return false;
}
public synchronized void setItem(String it) throws RemoteException {
// TODO Auto-generated method stub
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.item = it;
}
public void run() {
// TODO Auto-generated method stub
try {
System.out.println(Thread.currentThread().getName() + " is saling ticket " + register());
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我编辑的测试类如下:
服务器类:News_register_factory
Java code
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import News_apart_DAOImp.NAprt_affair_noticeDAOImp;
import News_apart_DAOImp.News_apart_DAOImp;
import News_aprt_DAO.News_register_factoryDAO;
public class News_register_factory extends UnicastRemoteObject implements
News_register_factoryDAO,Runnable {
private String item;
protected News_register_factory() throws RemoteException {
super();
// TODO Auto-generated constructor stub
}
public boolean register() throws RemoteException {
if(this.item == "newsDAOImp"){
LocateRegistry.createRegistry(8808);
News_apart_DAOImp news = new News_apart_DAOImp();
try {
Naming.rebind("//localhost:8808/news" , news);
System.out.println("\n news registered");
//registry.unbind("news");
/* UnicastRemoteObject.unexportObject(registry, true);
System.out.println(">>>>>INFO:服务停止!");
System.exit(0); //终止RMI服务程序的运行
*/
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
if(this.item == "NApart_affairImp"){
LocateRegistry.createRegistry(8809);
NAprt_affair_noticeDAOImp affair = new NAprt_affair_noticeDAOImp();
try {
Naming.rebind("//localhost:8809/Naffair" , affair);
System.out.println("\n affair registered");
/* //registry.unbind("Naffair");
UnicastRemoteObject.unexportObject(registry, true);
System.out.println(">>>>>INFO:服务停止!");
//System.exit(0); //终止RMI服务程序的运行
*
*/
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
if(this.item == "BApart_affairImp"){
LocateRegistry.createRegistry(8810);
NAprt_affair_noticeDAOImp affair = new NAprt_affair_noticeDAOImp();
try {
Naming.rebind("//localhost:8810/Baffair" , affair);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
return false;
}
public synchronized void setItem(String it) throws RemoteException {
// TODO Auto-generated method stub
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.item = it;
}
public void run() {
// TODO Auto-generated method stub
try {
System.out.println(Thread.currentThread().getName() + " is saling ticket " + register());
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
其中Imp类及其stub全部部署在服务端本地。
客户端类:
Java code
package LookUpImp;