RMI远程方法调用 客户端实现(三)

2.客户端:

1)通过符合JRMP规范的URL字符串在注册表中获取并强转成Remote子接口对象;

2)调用这个Remote子接口对象中的某个方法就是为一次远程方法调用行为
在这里插入图片描述

IHelloService

package com.lwl.demo;

import com.lwl.pojo.User;

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

public interface IHelloService extends Remote {

    //定义sayHello  方法
    public String sayHello(User user)throws RemoteException;
}

User

package com.lwl.pojo;

import java.io.Serializable;

public class User implements Serializable {
    private static final long serialVersionUID = -1;
    private String username;
    private Integer age;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public User(String username, Integer age) {
        this.username = username;
        this.age = age;
    }
}

RMIClient

package com.lwl.client;

import com.lwl.demo.IHelloService;
import com.lwl.pojo.User;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

public class RMIClient {

    public static void main(String[] args) throws RemoteException, NotBoundException, MalformedURLException {
        //从注册表中获取远程对象 强转
        IHelloService service = (IHelloService) Naming.lookup("//127.0.0.1:8888/rmiServer");
        //准备参数
        User user = new User("laowang", 18);

        //调用远程方法sayHello
        String s = service.sayHello(user);
        System.out.println(s+"------------");

        //

    }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

时小浅

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

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

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

打赏作者

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

抵扣说明:

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

余额充值