Home‎ > ‎Java‎ > ‎

Sample Java remote observer based on RMI



Remote observer interface

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface RemoteObserver extends Remote {

    void update(Object observable, Object updateMsg) throws RemoteException;

}

RMI client program implementing the interface

import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class RmiClient extends UnicastRemoteObject implements RemoteObserver {
    protected RmiClient() throws RemoteException {
        super();
    }

    private static final long serialVersionUID = 1L;

    public static void main(String[] args) {
        if (System.getSecurityManager() == null)
            System.setSecurityManager(new RMISecurityManager());
        try {
            RmiService remoteService = (RmiService) Naming
                    .lookup("//localhost:9999/RmiService");

            RmiClient client = new RmiClient();
            remoteService.addObserver(client);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public void update(Object observable, Object updateMsg)
            throws RemoteException {
        System.out.println("got message:" + updateMsg);
    }
}


RMI server (the observable)

import java.io.Serializable;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.Date;
import java.util.Observable;
import java.util.Observer;

public class RmiServer extends Observable implements RmiService {

    private class WrappedObserver implements Observer, Serializable {

        private static final long serialVersionUID = 1L;

        private RemoteObserver ro = null;

        public WrappedObserver(RemoteObserver ro) {
            this.ro = ro;
        }

        @Override
        public void update(Observable o, Object arg) {
            try {
                ro.update(o.toString(), arg);
            } catch (RemoteException e) {
                System.out
.println("Remote exception removing observer:" + this); o.deleteObserver(this); } } } @Override public void addObserver(RemoteObserver o) throws RemoteException { WrappedObserver mo = new WrappedObserver(o); addObserver(mo); System.out.println("Added observer:" + mo); } Thread thread = new Thread() { @Override public void run() { while (true) { try { Thread.sleep(5 * 1000); } catch (InterruptedException e) { // ignore } setChanged(); notifyObservers(new Date()); } }; }; public RmiServer() { thread.start(); } private static final long serialVersionUID = 1L; public static void main(String[] args) { if (System.getSecurityManager() == null) System.setSecurityManager(new RMISecurityManager()); try { Registry rmiRegistry = LocateRegistry.createRegistry(9999); RmiService rmiService = (RmiService) UnicastRemoteObject .exportObject(new RmiServer(), 9999); rmiRegistry.bind("RmiService", rmiService); } catch (Exception ex) { ex.printStackTrace(); } } }

RMI service interface

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface RmiService extends Remote {

    void addObserver(RemoteObserver o) throws RemoteException;

}

Security file


grant
{
    Permission java.security.AllPermission;
};


Running the server/client


java -Djava.security.licy=security.txt RmiServer
java -Djava.security.licy=security.txt RmiClient

Comments

The gadget spec URL could not be found



Comments