working on it ...

Filters

Explore Public Snippets

Sort by

Found 5 snippets matching: "java rmi"

    public by lbottaro  281543  2  6  3

    Simple RMI Java server

    This code shows a basic RMI server example. A "server" is the class which has a main method that creates an instance of the remote object implementation, exports the remote object and then binds that instance to a name in a RMI registry. Here the server implements the remote interface Hello. The class that contains this main method could be the
    package example.hello;
    	
    import java.rmi.registry.Registry;
    import java.rmi.registry.LocateRegistry;
    import java.rmi.RemoteException;
    import java.rmi.server.UnicastRemoteObject;
    	
    public class Server implements Hello {
    	
        public Server() {}
    
        public String sayHello() {
    	return "Hello, world!";
        }
    	
        public static void main(String args[]) {
    	
    	try {
    	    Server obj = new Server();
    	    Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, 0);
    
    	    // Bind the remote object's stub in the registry
    	    Registry registry = LocateRegistry.getRegistry();
    	    registry.bind("Hello", stub);
    
    	    System.err.println("Server ready");
    	} catch (Exception e) {
    	    System.err.println("Server exception: " + e.toString());
    	    e.printStackTrace();
    	}
        }
    }    

    public by lbottaro  294400  1  6  3

    Simple RMI Java client

    This is a simple RMI client implementation. Here the client program obtains a stub for the registry on the server's host, then looks up the remote object's stub by name in the registry. Finally invokes the sayHello method on the remote object using the stub. Here the client invokes the method exposes by Hello remote interface. Note that if no
    package example.hello;
    
    import java.rmi.registry.LocateRegistry;
    import java.rmi.registry.Registry;
    
    public class Client {
    
        private Client() {}
    
        public static void main(String[] args) {
    
    	String host = (args.length < 1) ? null : args[0];
    	try {
    	    Registry registry = LocateRegistry.getRegistry(host);
    	    Hello stub = (Hello) registry.lookup("Hello");
    	    String response = stub.sayHello();
    	    System.out.println("response: " + response);
    	} catch (Exception e) {
    	    System.err.println("Client exception: " + e.toString());
    	    e.printStackTrace();
    	}
        }
    }    

    public by lbottaro  170265  0  6  0

    Simple RMI Java interface

    A simple RMI interface example. A remote interface extends the interface java.rmi.Remote and declares a set of remote methods. Each remote method must declare java.rmi.RemoteException (or a superclass of RemoteException) in its throws clause, in addition to any application-specific exceptions. This interface declares just one method, sayHello, w
    package example.hello;
    
    import java.rmi.Remote;
    import java.rmi.RemoteException;
    
    public interface Hello extends Remote {
        String sayHello() throws RemoteException;
    } 

    external by hopeful  420  1  3  0

    Run jstatd w/o error 'access denied (java.util.PropertyPermission java.rmi.server.ignoreSubClasses write)'

    Run jstatd w/o error 'access denied (java.util.PropertyPermission java.rmi.server.ignoreSubClasses write)': jstatd.sh
    #!/bin/sh
    policy=${HOME}/.jstatd.all.policy
    [ -r ${policy} ] || cat >${policy} <<'POLICY'
    grant codebase "file:${java.home}/../lib/tools.jar" {
      permission java.security.AllPermission;
    };
    POLICY
    
    jstatd -J-Djava.security.policy=${policy} &
    
    
    

    external by hoshi524  166  0  2  0

    Java RMI test

    Java RMI test: Main.java
    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++;
    	}
    }
    
    

    List Complete ( 5 snippets total )

    • Public Snippets
    • Channels Snippets