RMI——hello world程序
来源:互联网 发布:雷洋之死 知乎 编辑:程序博客网 时间:2024/05/16 03:30
RMI——hello world程序
RMI作为分布式的基础之一,主要用来解决的问题是两个jvm进程间通讯的问题。RMI可以用作提供统一的服务接口和实现,方面各个系统间统一。
好,下面上货。
RMI分为服务端和客户端。下面我们先看一下服务端。
一、服务端。
1、服务端的目录结构截图:
2、MyRemote接口
package com.xueyou.demo.rmiserver.service;import com.xueyou.demo.rmiserver.dao.Student;import java.rmi.Remote;import java.rmi.RemoteException;public interface MyRemote extends Remote { public String sayHello() throws RemoteException; public Student sayStudent() throws RemoteException;}
3、MyRemoteImpl类
package com.xueyou.demo.rmiserver.serviceImpl;import com.xueyou.demo.rmiserver.dao.Student;import com.xueyou.demo.rmiserver.service.MyRemote;import java.rmi.RemoteException;import java.rmi.server.UnicastRemoteObject;public class MyRemoteImpl extends UnicastRemoteObject implements MyRemote { public String sayHello() throws RemoteException { return "hello world RMI"; } public MyRemoteImpl() throws RemoteException { } public Student sayStudent() throws RemoteException { return new Student("xiaoming", 10); }}
4、Student实体类。这里需要注意的是实体类需要实现serializeble接口。
package com.xueyou.demo.rmiserver.dao;import java.io.Serializable;public class Student implements Serializable { private String name; private int age; public Student() { } public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }}
5、启动服务类
package com.xueyou.demo.rmiserver;import com.xueyou.demo.rmiserver.service.MyRemote;import com.xueyou.demo.rmiserver.serviceImpl.MyRemoteImpl;import java.net.MalformedURLException;import java.rmi.AlreadyBoundException;import java.rmi.Naming;import java.rmi.RemoteException;import java.rmi.registry.LocateRegistry;public class App { public static void main(String[] args) { try { MyRemote service = new MyRemoteImpl(); LocateRegistry.createRegistry(9191); try { Naming.bind("rmi://192.168.0.113:9191/RemoteHello", service); } catch (AlreadyBoundException e) { e.printStackTrace(); } } catch (RemoteException e) { e.printStackTrace(); } catch (MalformedURLException e) { e.printStackTrace(); } System.out.println("success publish"); }}
二、客户端
首先看一下客户端的目录结构。需要注意的是,客户端的目录结构中,要调用的接口的包名和结构需要和服务端一致。这里一定要注意了,因为如果借口的包路径不一致的话,会出现ClassNotFoundException。
如图:
1、其他文件和服务端一致,这里只介绍如何调用RMI
package com.xueyou.demo.rmiclient;import com.xueyou.demo.rmiserver.dao.Student;import com.xueyou.demo.rmiserver.service.MyRemote;import java.net.MalformedURLException;import java.rmi.Naming;import java.rmi.NotBoundException;import java.rmi.RemoteException;public class App { public static void main(String[] args) { System.out.println("begin use RMI"); try { MyRemote service = (MyRemote) Naming.lookup("rmi://192.168.0.113:9191/RemoteHello"); String s = service.sayHello(); Student student = service.sayStudent(); System.out.println(s); System.out.println(student.getName()); System.out.println(student.getAge()); } catch (NotBoundException e) { e.printStackTrace(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (RemoteException e) { e.printStackTrace(); } }}
运行截图:
阅读全文
0 0
- RMI——hello world程序
- Rmi Hello World (实践rmi)
- 第一个Java程序—hello world
- Java RMI之 hello world
- Metapost—Hello World
- 第一个python CGI程序——hello,world
- Ext的简单的测试程序——Hello World
- 内核驱动的第一个程序——hello world
- 第一个Objective-C程序——Hello World!
- 第一个java程序——hello world
- 第一个Rust程序——Hello,world!
- 没有main的hello world 程序——Java
- 第一个PHP程序——Hello World
- 【C#】第一个C#程序——Hello World!
- 第一个Java程序示例——Hello World!
- Ext的简单的测试程序——Hello World
- Java程序开发体验——Hello World
- hello world —— 第一个java程序
- POJ 2422 The Wolves and the Sheep 笔记
- IntelliJ IDEA安装、配置、测试(win7_64bit)
- 机器学习--神经网络算法系列--交叉熵(Cross_entropy)
- hashMap的原理 深入理解
- React Native网络请求fetch简单封装
- RMI——hello world程序
- cocos2d中游戏失败后再次回到游戏主界面出现“Node still marked as running on node destruction! Was base class onExit() “
- 尺取法
- Java String和基本数据类型之间的转换
- Hdu1695 GCD
- 前端|加载的图片太多或者太大怎么办(上)
- Ruby 中进程与线程
- HDU-2072 单词数
- 共享单车上的智能锁,做出来有多难?