Skip to content

Instantly share code, notes, and snippets.

Created July 4, 2013 15:55
Show Gist options
  • Save anonymous/5928786 to your computer and use it in GitHub Desktop.
Save anonymous/5928786 to your computer and use it in GitHub Desktop.
Java Remote Method Invocation (RMI) with Secure Socket Layer (SSL) - http://codeoftheday.blogspot.com/2013/07/java-remote-method-invocation-rmi-with.html
/**
* Java Remote Method Invocation (RMI) with Secure Socket Layer (SSL)
* http://codeoftheday.blogspot.com/2013/07/java-remote-method-invocation-rmi-with.html
*/
package smhumayun.codeoftheday.RmiOverSslTest;
import javax.rmi.ssl.SslRMIClientSocketFactory;
import javax.rmi.ssl.SslRMIServerSocketFactory;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
/**
* Implementation of {@link CentralTimeService}
*
* User: smhumayun
* Date: 7/4/13
* Time: 8:25 AM
*/
public class CentralTimeServiceImpl extends UnicastRemoteObject implements CentralTimeService {
/**
* Creates and exports a new UnicastRemoteObject object using an
* anonymous port.
*
* @throws java.rmi.RemoteException if failed to export object
* @since JDK1.1
*/
protected CentralTimeServiceImpl() throws RemoteException {
super(0,
new SslRMIClientSocketFactory(),
new SslRMIServerSocketFactory(null, null, true));
}
/**
* Return current time in milliseconds on server
*
* @return current time on server in milliseconds
* @throws RemoteException
*/
@Override
public long getTime() throws RemoteException {
return System.currentTimeMillis();
}
/**
* Main method to start server
*
* @param args arguments
*/
public static void main(String args[]) {
try {
// Get SSL-based registry
Registry registry = LocateRegistry.getRegistry(null, CentralTimeService.PORT, new SslRMIClientSocketFactory());
//Create new impl object
CentralTimeServiceImpl centralTimeServiceImpl = new CentralTimeServiceImpl();
// Bind this object instance to the name "CentralTimeService"
registry.bind("CentralTimeService", centralTimeServiceImpl);
System.out.println("CentralTimeService bound in registry");
} catch (Exception e) {
System.out.println("CentralTimeServiceImpl Error : " + e.getMessage());
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment