Skip to content

Commit

Permalink
update Example_06
Browse files Browse the repository at this point in the history
  • Loading branch information
godpan committed Aug 28, 2017
1 parent d89f936 commit 0632778
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 0 deletions.
32 changes: 32 additions & 0 deletions Example_06/local/src/main/java/JoinRmiEvt.java
@@ -0,0 +1,32 @@
import java.io.Serializable;
import java.rmi.Remote;

/**
* Created by panguansen on 17/8/24.
*/
public class JoinRmiEvt implements Remote , Serializable{
private static final long serialVersionUID = 1L;
private Long id;
private String name;

public JoinRmiEvt(Long id, String name) {
this.id = id;
this.name = name;
}

public Long getId() {
return id;
}

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

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
10 changes: 10 additions & 0 deletions Example_06/local/src/main/java/RemoteRmi.java
@@ -0,0 +1,10 @@
import java.rmi.Remote;
import java.rmi.RemoteException;

/**
* Created by panguansen on 17/8/24.
*/
public interface RemoteRmi extends Remote {
public void sendNoReturn(String message) throws RemoteException, InterruptedException;
public String sendHasReturn(JoinRmiEvt joinRmiEvt) throws RemoteException;
}
22 changes: 22 additions & 0 deletions Example_06/local/src/main/java/RemoteRmiClient.java
@@ -0,0 +1,22 @@
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

/**
* Created by panguansen on 17/8/24.
*/
public class RemoteRmiClient {
public static void main(String[] args) throws RemoteException, NotBoundException, MalformedURLException, InterruptedException {
System.out.println("the client has started");
String url = "rmi://127.0.0.1:2553/remote_rmi";
RemoteRmi remoteRmi = (RemoteRmi) Naming.lookup(url);
Long startTime = System.currentTimeMillis();
System.out.println("the client has running");
remoteRmi.sendNoReturn("send no return");
System.out.println(remoteRmi.sendHasReturn(new JoinRmiEvt(1L,"godpan")));
Long endTime = System.currentTimeMillis();
System.out.println("the running time is " + (endTime - startTime));
System.out.println("the client has end");
}
}
32 changes: 32 additions & 0 deletions Example_06/remote/src/main/java/JoinRmiEvt.java
@@ -0,0 +1,32 @@
import java.io.Serializable;
import java.rmi.Remote;

/**
* Created by panguansen on 17/8/24.
*/
public class JoinRmiEvt implements Remote , Serializable{
private static final long serialVersionUID = 1L;
private Long id;
private String name;

public JoinRmiEvt(Long id, String name) {
this.id = id;
this.name = name;
}

public Long getId() {
return id;
}

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

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
20 changes: 20 additions & 0 deletions Example_06/remote/src/main/java/RemoteRMIServer.java
@@ -0,0 +1,20 @@
import java.net.MalformedURLException;
import java.rmi.AlreadyBoundException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;

/**
* Created by panguansen on 17/8/24.
*/
public class RemoteRMIServer {
public static void main(String[] args) throws RemoteException, AlreadyBoundException, MalformedURLException, InterruptedException {
System.out.println("the RemoteRMIServer is Starting ...");
RemoteRmiImpl remoteRmi = new RemoteRmiImpl();
System.out.println("Binding server implementation to registry");
LocateRegistry.createRegistry(2553);
Naming.bind("rmi://127.0.0.1:2553/remote_rmi",remoteRmi);
System.out.println("the RemoteRMIServer is Started");
Thread.sleep(10000000);
}
}
10 changes: 10 additions & 0 deletions Example_06/remote/src/main/java/RemoteRmi.java
@@ -0,0 +1,10 @@
import java.rmi.Remote;
import java.rmi.RemoteException;

/**
* Created by panguansen on 17/8/24.
*/
public interface RemoteRmi extends Remote {
public void sendNoReturn(String message) throws RemoteException, InterruptedException;
public String sendHasReturn(JoinRmiEvt joinRmiEvt) throws RemoteException;
}
25 changes: 25 additions & 0 deletions Example_06/remote/src/main/java/RemoteRmiImpl.java
@@ -0,0 +1,25 @@
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

/**
* Created by panguansen on 17/8/24.
*/
public class RemoteRmiImpl extends UnicastRemoteObject implements RemoteRmi {

private static final long serialVersionUID = 1L;

public RemoteRmiImpl() throws RemoteException {};

@Override
public void sendNoReturn(String message) throws RemoteException, InterruptedException {
Thread.sleep(2000);
// throw new RemoteException();
}

@Override
public String sendHasReturn(JoinRmiEvt joinRmiEvt) throws RemoteException {
if (joinRmiEvt.getId() >= 0)
return new StringBuilder("the").append(joinRmiEvt.getName()).append("has join").toString();
else return null;
}
}

0 comments on commit 0632778

Please sign in to comment.