Java RMI远程调用实例

1.定义服务接口,服务接口必须继承java.rmi.Remote,服务方法必须抛出java.rmi.RemoteException.

package rmi.service;

import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.HashMap;

/**
 * //定义服务接口,服务接口必须继承java.rmi.Remote,服务方法必须抛出java.rmi.RemoteException.
 * @author Administrator
 */
public interface IRemoteService extends Remote {

    public String sayHello(String name) throws RemoteException;

    public HashMap<String,Object> getHero(String name) throws RemoteException;

}

2.提供服务实现类,服务实现类继承了java.rmi.server.UnicastRemoteObject,而其构造方法抛出异常,所以服务实现类必须显示给出构造方法并抛出异常.

package rmi.service.impl;


import rmi.service.IRemoteService;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * //提供服务实现类,服务实现类继承了java.rmi.server.UnicastRemoteObject,
 * 而其构造方法抛出异常,所以服务实现类必须显示给出构造方法并抛出异常.
 * @author Administrator
 */
public class RemoteServiceImpl extends UnicastRemoteObject implements IRemoteService {

    private static final long serialVersionUID = -1817498005094186816L;

    public RemoteServiceImpl() throws RemoteException {

    }

    @Override
    public String sayHello(String name) throws RemoteException {
        System.out.println("Hello " + name + "啊!");
        return "Hello " + name + "!";
    }

    @Override
    public HashMap<String,Object> getHero(String name) throws RemoteException {
        HashMap<String,Object> map = new HashMap<String,Object>();
        map.put("id",new AtomicInteger().incrementAndGet());
        map.put("name",name);
        return map;
    }
}

3.发布服务。也就是将服务实现类注册到RMI服务注册表中。

package rmi;

/**
 * //发布服务。也就是将服务实现类注册到RMI服务注册表中。
 * @author Administrator
 */
import rmi.service.IRemoteService;
import rmi.service.impl.RemoteServiceImpl;

import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;

public class RemotePublish {
    public static void main(String[] args) throws Exception {

        // 创建RMI服务注册表
        LocateRegistry.createRegistry(8888);

        IRemoteService rs = new RemoteServiceImpl();

        // 将服务实现类的实例发布到指定端口并命名为rservice,等待远程调用
        Naming.bind("rmi://localhost:8888/rservice", rs);
    }
}

4.定义客户调用接口,注意必须与服务端路径保持一致。

package rmi.service;

import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.HashMap;

/**
 * //定义服务接口,服务接口必须继承java.rmi.Remote,服务方法必须抛出java.rmi.RemoteException.
 * @author Administrator
 */
public interface IRemoteService extends Remote {

    public String sayHello(String name) throws RemoteException;

    public HashMap<String,Object> getHero(String name) throws RemoteException;

}

5.客户端通过RMI协议对发布好的服务器端口进行调用

package rmi;


import rmi.service.IRemoteService;

import java.rmi.Naming;
import java.util.HashMap;

/**
 * 客户端通过RMI协议对发布好的服务器端口进行调用
 */
public class ClientTest {
    public static void main(String[] args) throws Exception {

        // 从远程获取名称为rservice的服务实现类
        long start=System.currentTimeMillis();
        IRemoteService rs = (IRemoteService) Naming.lookup("rmi://localhost:8888/rservice");
        String say = rs.sayHello("world");
        System.out.println(say);

        HashMap<String,Object> hero = rs.getHero("张三");

        System.out.println(hero.toString());

        start=System.currentTimeMillis()-start;
        System.out.println(start);
    }
}

  

 

posted @ 2021-07-29 00:18  buly  阅读(59)  评论(0编辑  收藏  举报