Java中的RMI

java中的rmi

rmi 是 java中的一种rpc实现 ,提供了一种机制来实现分布式应用的通信和调用远程方法的能力.

rmi的独特优势

  • rmi是java 特定的远程调用机制,它利用java的强类型和面向对象的特性,支持参数和返回值的序列化
  • rmi提供了对象的远程引用,使得客户端可以像本地对象一样调用远程对象的方法,而不需要编写底层的通信代码
  • rmi提供了透明的远程对象传输,客户不需要关系网络通信细节,就可以调用远程方法
  • rmi支持动态类加载,允许在运行时动态下载和加载远程对象的类定义

测试案例

rmi 服务器端案例

package cn.demo.rmi.server;

import cn.demo.rmi.pojo.TestBean;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface RemoteInterface extends Remote {

    public String sayHello() throws RemoteException;


    String test2(String msg) throws RemoteException;

    String test3(TestBean testBean) throws RemoteException;

}

package cn.demo.rmi.server;

import cn.demo.rmi.pojo.TestBean;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Date;

public class RemoteService extends UnicastRemoteObject implements RemoteInterface {


    public RemoteService() throws RemoteException {
        super();
    }

    @Override
    public String sayHello() throws RemoteException {
        System.err.println("====>sayHello invoke time"+new Date());
        return "hello from remote object!";
    }

    @Override
    public String test2(String msg) throws RemoteException {
        System.err.println("====>test2 invoke time"+new Date());
        return "rec: "+msg;
    }

    @Override
    public String test3(TestBean testBean) throws RemoteException {
        System.err.println("====>test3 invoke time"+new Date());

        return testBean.toString();
    }
}

package cn.demo.rmi.server;

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

/**
 * rmi 是 java中的一种rpc实现 ,提供了一种机制来实现分布式应用的通信和调用远程方法的能力
 *
 * rmi的独特优势
 * --rmi是java 特定的远程调用机制,它利用java的强类型和面向对象的特性,支持参数和返回值的序列化
 * --rmi提供了对象的远程引用,使得客户端可以像本地对象一样调用远程对象的方法,而不需要编写底层的通信代码
 * --rmi提供了透明的远程对象传输,客户不需要关系网络通信细节,就可以调用远程方法
 * --rmi支持动态类加载,允许在运行时动态下载和加载远程对象的类定义
 */
public class Server {
    public static void main(String[] args) {
        try {
            //创建rmi注册表实例
            Registry registry = LocateRegistry.createRegistry(1099);
            //注册远程对象
            RemoteService remoteService = new RemoteService();
            registry.rebind("RemoteService", remoteService);
            System.out.println("server is running...");

        }catch (Exception e){
            e.printStackTrace();

        }
    }
}


传输的java对象

package cn.demo.rmi.pojo;

import java.io.Serializable;

public class TestBean implements Serializable {

    private String name;
    private Long id;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "TestBean{" +
                "name='" + name + '\'' +
                ", id=" + id +
                '}';
    }
}

rmi客户端代码案例

package cn.demo.rmi.cli;

import cn.demo.rmi.pojo.TestBean;
import cn.demo.rmi.server.RemoteInterface;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class Client {
    public static void main(String[] args) {
        try {
            //查找rmi注册表中的远程对象引用
            Registry registry = LocateRegistry.getRegistry("localhost", 1099);

            RemoteInterface remoteObject = (RemoteInterface) registry.lookup("RemoteService");

            //调用远程方法
            String s = remoteObject.sayHello();
            System.out.println(s);

            String r21r21 = remoteObject.test2("r21r21");
            System.out.println(r21r21);

            TestBean t = new TestBean();
            t.setId(123L);
            t.setName("r23rde");
            String s1 = remoteObject.test3(t);
            System.out.println(s1);


        } catch (RemoteException | NotBoundException e) {
            e.printStackTrace();
        }

    }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ThinkPet

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值