Author: athimel Date: 2011-01-11 14:05:58 +0100 (Tue, 11 Jan 2011) New Revision: 456 Url: http://nuiton.org/repositories/revision/sandbox/456 Log: Valid RMI export test without rmic and without external rmiregistry Modified: testStandaloneRmi/src/main/java/org/nuiton/sandbox/rmi/ServiceImpl.java testStandaloneRmi/src/main/java/org/nuiton/sandbox/rmi/ServiceRMIInterface.java testStandaloneRmi/src/test/java/org/nuiton/sandbox/rmi/Client.java Modified: testStandaloneRmi/src/main/java/org/nuiton/sandbox/rmi/ServiceImpl.java =================================================================== --- testStandaloneRmi/src/main/java/org/nuiton/sandbox/rmi/ServiceImpl.java 2011-01-11 10:52:23 UTC (rev 455) +++ testStandaloneRmi/src/main/java/org/nuiton/sandbox/rmi/ServiceImpl.java 2011-01-11 13:05:58 UTC (rev 456) @@ -1,8 +1,11 @@ package org.nuiton.sandbox.rmi; +import java.rmi.ConnectException; +import java.rmi.Remote; import java.rmi.RemoteException; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; +import java.rmi.server.UnicastRemoteObject; /** * @author Arnaud Thimel <thimel@codelutin.com> @@ -16,29 +19,40 @@ public static void main(String[] args) throws Exception { + testRmiConfig(); + ServiceImpl impl = new ServiceImpl(); + Remote stub = UnicastRemoteObject.exportObject(impl, 0); Registry registry = getRegistry(); - registry.bind("MyService", impl); + registry.bind("MyService", stub); System.err.println("Service ready"); - Thread.sleep(60000); } - protected static Registry registry; + protected static void testRmiConfig() { + String rmiHost = System.getProperty("java.rmi.server.hostname"); + if (rmiHost == null || "".equals(rmiHost.trim())) { + System.err.println("Warning, server might not have been initialized properly, please specify '-Djava.rmi.server.hostname=<IP-address>'"); + } + } protected static Registry getRegistry() throws RemoteException { - if (registry == null) { + Registry result; + try { + result = LocateRegistry.getRegistry(PORT); + result.list(); // To test that registry has been created. Exception will be thrown is registry cannot be called + } catch (ConnectException ce) { System.err.println("Registry not found, creating a new one"); - registry = LocateRegistry.createRegistry(PORT); + result = LocateRegistry.createRegistry(PORT); } - return registry; + return result; } @Override - public String testExchange(Integer intValue) throws RemoteException { - System.err.println("Incoming call with value: " + intValue); - return String.format("What is this fucking number (%d) ?", intValue); + public String testExchange(String whoIsCalling, Integer intValue) throws RemoteException { + System.out.println(String.format("Incoming call from %s with value: (%d) - Here I'm (%s)", whoIsCalling, intValue, System.getProperty("java.rmi.server.hostname"))); + return String.format("Hey %s, what is this fucking number '%d' ?", whoIsCalling, intValue); } } Modified: testStandaloneRmi/src/main/java/org/nuiton/sandbox/rmi/ServiceRMIInterface.java =================================================================== --- testStandaloneRmi/src/main/java/org/nuiton/sandbox/rmi/ServiceRMIInterface.java 2011-01-11 10:52:23 UTC (rev 455) +++ testStandaloneRmi/src/main/java/org/nuiton/sandbox/rmi/ServiceRMIInterface.java 2011-01-11 13:05:58 UTC (rev 456) @@ -7,8 +7,8 @@ /** * @author Arnaud Thimel <thimel@codelutin.com> */ -public interface ServiceRMIInterface extends Remote, Serializable { +public interface ServiceRMIInterface extends Remote { - public abstract String testExchange(Integer intValue) throws RemoteException; + public abstract String testExchange(String whoIsCalling, Integer intValue) throws RemoteException; } Modified: testStandaloneRmi/src/test/java/org/nuiton/sandbox/rmi/Client.java =================================================================== --- testStandaloneRmi/src/test/java/org/nuiton/sandbox/rmi/Client.java 2011-01-11 10:52:23 UTC (rev 455) +++ testStandaloneRmi/src/test/java/org/nuiton/sandbox/rmi/Client.java 2011-01-11 13:05:58 UTC (rev 456) @@ -12,8 +12,8 @@ public static void main(String[] args) throws Exception { Registry registry = LocateRegistry.getRegistry("10.1.1.85", PORT); ServiceRMIInterface stub = (ServiceRMIInterface) registry.lookup("MyService"); - String response = stub.testExchange(123); + String response = stub.testExchange(System.getProperty("user.name"), 123); System.out.println("response: " + response); + } - } }