首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Openfire 施用RMI远程调用同步好友列表

2012-12-18 
Openfire 使用RMI远程调用同步好友列表1. 定义接口:?public interface RmiRemoteIntface extends Remote{?

Openfire 使用RMI远程调用同步好友列表

1. 定义接口:

?

public interface RmiRemoteIntface extends Remote{
???
??? void deleteFriend(String user1, String user2) throws RemoteException;
???
??? String addFriend(String user1, String user2) throws RemoteException;
}

?

?

2. 实现接口

?

public class RmiRemoteObj extends UnicastRemoteObject implements
??? ??? RmiRemoteIntface {

??? protected RmiRemoteObj() throws RemoteException {
??? ??? super();
??? }

??? @Override
??? public void deleteFriend(String user1, String user2) throws RemoteException {
//??? ??? IQ iq = new IQ();
//??? ??? iq.setType(IQ.Type.set);
//??? ??? for (ClientSession session : XMPPServer.getInstance().getRoutingTable().getClientsRoutes(true)) {
//??? ??? ??? if (session.getAddress().getNode().equals(user1)) {
//??? ??? ??? ??? processDeleteMsg(iq, user2, session);
//??? ??? ??? } else if (session.getAddress().getNode().equals(user2)) {
//??? ??? ??? ??? processDeleteMsg(iq, user1, session);
//??? ??? ??? }
//??? ??? }
//??? ??? Element element = null;
??? }
???
??? public String addFriend(String user1, String user2) throws RemoteException{
//??? ??? IQ iq = new IQ();
//??? ??? iq.setType(IQ.Type.set);
//??? ??? for (ClientSession session : XMPPServer.getInstance().getRoutingTable().getClientsRoutes(true)) {
//??? ??? ??? if (session.getAddress().getNode().equals(user1)) {
//??? ??? ??? ??? processAddMsg(iq, user2, session);
//??? ??? ??? } else if (session.getAddress().getNode().equals(user2)) {
//??? ??? ??? ??? processAddMsg(iq, user1, session);
//??? ??? ??? }
??? ???
??? ??? System.out.println(user1+ " "+user2);
??? ??? return user1+" "+user2+? " hello world " ;
??? ??? }

?

?

3. 编写RmiServer

?

public class RmiServer extends Thread{
??? public static int rmi = 0;
??? public static RmiServer rmiServer;
??? static{
??????? rmiServer = new RmiServer();
??????? rmiServer.setDaemon(false);
??????? rmiServer.start();
??? }
???
??? public static void main(String args[]){
??? ??? //创建并安装安全管理器
//??? ??? if(System.getSecurityManager() == null){
//??? ??? ??? System.setSecurityManager(new RMISecurityManager());
//??? ??? }
??? ??? try{
??? ??? ??? LocateRegistry.createRegistry(8057);
??? ??? ??? //创建远程对象
??? ??? ??? RmiRemoteIntface rro = new RmiRemoteObj();
??? ??? ??? //将实例注册到专有的URL
??? ??? ??? Naming.rebind("//127.0.0.1:8057/RmiRemoteObj", rro);
??? ??? ???
??? ??? ??? System.out.println("RMI服务器正在运行...");
??? ??? }catch(Exception e){
??? ??? ??? e.printStackTrace();
??? ??? }
??? }
???
??? public void run(){
??? ??? try{
??? ??? ??? LocateRegistry.createRegistry(8057);
??? ??? ??? //创建远程对象
??? ??? ??? RmiRemoteIntface rro = new RmiRemoteObj();
??? ??? ??? //将实例注册到专有的URL
??? ??? ??? Naming.rebind("//127.0.0.1:8057/RmiRemoteObj", rro);
??? ??? ???
??? ??? ??? System.out.println("RMI服务器正在运行...");
??? ??? }catch(Exception e){
??? ??? ??? e.printStackTrace();
??? ??? }
??? }

?

?

4. 编写RmiClient

?

public class RmiClient {

??? public static void main(String args[]){
??? ??? //创建并安装安全管理器
//??? ??? if(System.getSecurityManager()==null)
//??? ??? {
//??? ?????? System.setSecurityManager(new RMISecurityManager());
//??? ??? }

??? ??? String host = "rmi://127.0.0.1:8057/RmiRemoteObj";
//??? ??? String host = "rmi://192.168.1.103:2099/RmiRemoteObj";
//??? ??? String host = "rmi://192.168.1.119:2099/RmiRemoteObj";
//??? ??? if(args.length > 0){
//??? ??? ??? host = args[0];
//??? ??? }
??? ???
??? ??? try{
??? ??? ??? //根据制定的URL定位远程对象
??? ??? ??? RmiRemoteIntface rri = (RmiRemoteIntface)Naming.lookup(host);
//??? ??? ??? rri.addFriend("test01", "test02");
??? ??? ??? System.out.println(rri.addFriend("aaaaaaa", "ccc"));
//??? ??? ??? rri.deleteFriend("admin", "kocko");
??? ??? ??? System.out.println("调用成功!");
??? ??? }catch(Exception e){
??? ??? ??? e.printStackTrace();
??? ??? }

??? }

?

5. 在Openfire启动时加载Rmi服务

??? 在类org.jivesoftware.openfire.spi.ConnectionManagerImpl 的方法?? startListeners()startHTTPBindListeners后面加上RmiServer rmi = new RmiServer();

??? 搞定!

RmiServer rmi = new RmiServer();

热点排行