java RMI示例服务器代码

目录结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// MyRMIClient
package Client;
import InterFace.*;
import java.rmi.Naming;
import java.util.ArrayList;
import java.util.Scanner;

public class MyRMIClient {
public static void Print_Tip(){
System.out.println("选择操作代号完成操作");
System.out.println("1:add args:book b");
System.out.println("2:queryByID args:int bookID");
System.out.println("3:queryByName args: String name");
System.out.println("4:delete args:int BookID");
System.out.println("5: print args:null");
System.out.println("6: exit");
}
public static void main(String args[]) {

try {
String name = "rmi://106.14.2.163:6789/BookSystem";
String[] test = Naming.list( "rmi://106.14.2.163:6789/BookSystem");
System.out.println(test[0]);
BookSystemInfo BookSystem=( BookSystemInfo)Naming.lookup(name);

System.out.println("查找服务端成功");
Scanner input=new Scanner(System.in);
int number = 6;
Print_Tip();
while ((number = input.nextInt())!=6){
switch (number){
case 1:{
System.out.println("输入ID和名字");
int bookID = input.nextInt();
String bookName = input.next();
Book newbook = new Book(bookName,bookID);
boolean res = BookSystem.add(newbook);
if(res==true){
System.out.println("添加成功");
}
else{
System.out.println("添加失败");
}
}
break;
case 2:{
System.out.println("输入ID");
int bookID = input.nextInt();
Book res = BookSystem.queryByID(bookID);
if(res==null){
System.out.println("无该ID号的书籍");
}
else{
System.out.println("书籍信息如下");
res.Print_content();
}
}
break;
case 3:{
System.out.println("输入name");
String bookName = input.next();
ArrayList<Book> res = BookSystem.queryByName(bookName);
if(res==null){
System.out.println("同名书籍数目为零");
}
else {
System.out.println("查询结果如下");
for (Book b : res) {
b.Print_content();
}
}
}
break;
case 4:{
System.out.println("输入ID");
int bookID = input.nextInt();
boolean res = BookSystem.delete(bookID);
if(res==true){
System.out.println("删除成功");
}
else{
System.out.println("删除失败");
}
}
break;
case 5:{
System.out.println("调用该方法只是在服务端打印书籍列表,客户端不显示");
BookSystem.print();
}
break;
}

}

} catch (Exception e) {
System.err.println("??? exception:");
e.printStackTrace();
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//  Book
package InterFace;

import java.io.Serializable;

public class Book implements Serializable {
private static final long serialVersionUID = 6529685098267757690L;
private String name;
private int BookID;
public Book(String n,int b){
name = n;
BookID = b;
}
public boolean EqualName(String otherName){
return name.equals(otherName);
}
public boolean EqualID(int otherID){
return (BookID==otherID);
}
public void Print_content(){
System.out.println(BookID+": "+name);
}
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// BookSystem
package InterFace;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.io.Serializable;
import java.util.ArrayList;


public class BookSystem implements BookSystemInfo,Serializable {
private static final long serialVersionUID = 6529685098267757665L;
private ArrayList<Book> list;
public BookSystem() throws RemoteException{
list = new ArrayList<>();
}
public boolean add(Book b) throws RemoteException{
try{
list.add(b);
}catch (Exception e){
return false;
}
return true;
}

public Book queryByID(int BookID) throws RemoteException{
for(Book b :list){
if(b.EqualID(BookID)){
return b;
}
}
return null;
}
public ArrayList<Book> queryByName(String name) throws RemoteException{
ArrayList<Book> res = new ArrayList<Book>();
for(Book b :list){
if(b.EqualName(name)){
res.add(b);
}
}
return res;
}
public boolean delete(int BookID) throws RemoteException{
for(Book b :list){
if(b.EqualID(BookID)){
list.remove(b);
return true;
}
}
return false;
}
public void print(){
System.out.println("书籍列表如下");
for(Book b :list){
b.Print_content();
}
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// BookSystemInfo
package InterFace;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.io.Serializable;
import java.util.ArrayList;

public interface BookSystemInfo extends Remote,Serializable{
boolean add(Book b) throws RemoteException;
Book queryByID(int bookID) throws RemoteException;
ArrayList<Book> queryByName(String name) throws RemoteException;
boolean delete(int BookID) throws RemoteException;
void print() throws RemoteException;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// MyRMIServer
package Server;
import java.rmi.registry.LocateRegistry;
import java.rmi.Naming;
import InterFace.*;
import java.rmi.server.UnicastRemoteObject;

public class MyRMIServer {


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

try {
String serverIP = "106.14.2.163";
System.setProperty("java.rmi.server.hostname", serverIP);
LocateRegistry.createRegistry(6789);
String name ="rmi://127.0.0.1:6789/BookSystem";
BookSystemInfo engine = new BookSystem();
BookSystemInfo skeleton = ( BookSystemInfo) UnicastRemoteObject.exportObject(engine, 5678);
System.out.println("Registering BookSystem Object");
Naming.bind(name,skeleton);
} catch (Exception e) {
System.err.println("Exception:" + e);
e.printStackTrace();
}
}
}