Parameter Passing in Remote Methods Invocation
1. Phần chung
Tạo Object để truyền qua lại giữa các ứng dụng RMI, file Ball_Ser.java
| import java.io.Serializable;public class Ball_Ser implements Serializable
{ private int weight; public Ball_Ser(int w){ weight=w; } public int getWeight(){ return weight; } public void setWeight(int w){ weight=w; } public String toString(){ return “Ball (Serializabel) with weight : “+weight; } } |
Tạo Remote Interface: file IPingBall.java
| import java.rmi.Remote;import java.rmi.RemoteException;
public interface IPingBall extends Remote { public Ball_Ser ping(Ball_Ser b) throws RemoteException; } |
Biên dịch 2 file này bằng lệnh javac *.java
2. Phía server: tạo thư mục server, copy 2 tập tin IPingBall.class và Ball_Ser.class vào.
a. Tạo đối tượng implements của Remote interface: Tập tin PingServerImpl.java
| import java.rmi.RemoteException;import java.rmi.server.UnicastRemoteObject;
public class PingServerImpl extends UnicastRemoteObject implements IPingBall{ public PingServerImpl()throws RemoteException{ super(); } public Ball_Ser ping(Ball_Ser b) throws RemoteException{ System.out.println(“Client send a ball objectweight “ + b.getWeight()); b.setWeight(b.getWeight()+15); return b; } } |
b. Tạo Server, lớp BallServer.java
Nội dung của file như sau
| import java.rmi.Naming;public class BallServer{
public static void main(String args[]) { try { java.rmi.registry.LocateRegistry.createRegistry(1099); IPingBall server=new PingServerImpl(); //UnicastRemoteObject.exportObject(server); Naming.bind(“rmi://localhost/pingobject”,server); System.out.println(“Waiting for client request …”); }catch (Exception ex) { ex.printStackTrace(); } } } |
c. Tạo file compile.bat để biên dịch server
| Javac -d . *.javapause |
Chạy file này và đảm bảo mọi thứ OK.
d. Tạo file runServer.bat với nội dung
| java BallServer |
e. Chạy server bằng cách thực thi file runServer. Kết quả như sau:
| E:\java\java2\Slides\RMI\ex_03\pass_Params_by_Val>java BallServerWaiting for client request … |
3. Phía Client
a. Tạo thư mục client, copy 2 tập tin IPingBall.class và Ball_Ser.class vào.
b. Tạo file BallClient.java với nội dung
| import java.rmi.Naming;public class BallClient {
public static void main(String args[]){ try { Ball_Ser ball=new Ball_Ser(12); System.out.println(“Ball weight before send to server “+ball.getWeight()); IPingBall server=(IPingBall)Naming.lookup(“rmi://localhost/pingobject”); System.out.println(“Ball weight after send to server “+ball.getWeight()); Ball_Ser anotherBall=server.ping(ball); System.out.println(“Ball weight return by server “+anotherBall.getWeight()); } catch (Exception ex) { ex.printStackTrace(); } } } |
4. Tạo file client.policy
| grant {permission java.net.SocketPermission “*:1024-65535″, “connect”;
}; |
5. Tạo file compile.bat để biên dịch client
| javac -d . *.javapause |
Chạy file này và đảm bảo mọi thứ OK.
6. Tạo file Client.bat với nội dung
| java BallClient
pause |
7. Chạy Client.bat để thực thi ứng dụng. Phía client sẽ hiển thị dạng sau :
| E:\java\java2\Slides\RMI\ex_03\pass_Params_by_Val>java -Djava.Security.policy=client.policy BallClientBall weight before send to server 12
Ball weight after send to server 12 Ball weight return by server 27 |
Phía server sẽ xuất hiện dạng sau
| E:\java\java2\Slides\RMI\ex_03\pass_Params_by_Val>java BallServerWaiting for client request …
Client send a ball objectweight 12 |
thachphongphong said
thầy phân tích dùng em 3 lớp trong 1 ứng dụng RMI?