Created
October 11, 2016 10:09
-
-
Save hoshi524/668fb3a49c1fb5b9f2d4f507414ef772 to your computer and use it in GitHub Desktop.
Java RMI test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.Serializable; | |
import java.rmi.Remote; | |
import java.rmi.RemoteException; | |
import java.rmi.registry.LocateRegistry; | |
import java.rmi.registry.Registry; | |
import java.rmi.server.UnicastRemoteObject; | |
import java.util.Arrays; | |
public class Main { | |
private static final String name = "name"; | |
public static final void main(String args[]) throws Exception { | |
new Main().run(); | |
System.exit(0); | |
} | |
private Registry registry; | |
void run() throws Exception { | |
registry = LocateRegistry.createRegistry(Registry.REGISTRY_PORT); | |
debug("UnicastRemoteObject", false, "getRegistry", false); | |
server0(); | |
client0(); | |
client0(); | |
debug("UnicastRemoteObject", true, "getRegistry", false); | |
server1(); | |
client0(); | |
client0(); | |
debug("UnicastRemoteObject", false, "getRegistry", true); | |
server0(); | |
client1(); | |
client1(); | |
debug("UnicastRemoteObject", true, "getRegistry", true); | |
server1(); | |
client1(); | |
client1(); | |
} | |
void server0() throws Exception { | |
registry.rebind(name, new RMITest()); | |
} | |
void server1() throws Exception { | |
/** | |
* java.rmi.NoSuchObjectException: no such object in table | |
* 頻出して安定しない | |
*/ | |
registry.rebind(name, UnicastRemoteObject.exportObject(new RMITest(), 0)); | |
} | |
void client0() throws Exception { | |
Remote remote = registry.lookup(name); | |
if (remote instanceof RMI) { | |
System.out.println(((RMI) remote).calc()); | |
} | |
} | |
void client1() throws Exception { | |
Remote remote = LocateRegistry.getRegistry().lookup(name); | |
if (remote instanceof RMI) { | |
System.out.println(((RMI) remote).calc()); | |
} | |
} | |
void debug(Object... o) { | |
System.out.println(Arrays.deepToString(o)); | |
} | |
} | |
interface RMI extends Remote { | |
int calc() throws RemoteException; | |
} | |
class RMITest implements RMI, Serializable { | |
private static final long serialVersionUID = 4023010035712853564L; | |
private int x = 0; | |
@Override | |
public int calc() throws RemoteException { | |
return x++; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
UnicastRemoteObjectが実行毎で失敗したりする原因がよく分からない