This discussion is archived
2 Replies Latest reply: Nov 1, 2010 1:31 PM by 809490 RSS

RMI observer problem

809490 Newbie
Currently Being Moderated
Hello

I want to write a client server application using rmi and eclipse, where many clients are able to register to the server. The server has a string attribute and a client should be able to pass a string object to the server and the server replaces the value of his attribute with the sent string and notifies all registered clients of the new value. In a later step the server writes to a mysql database and notifies the clients about changes in the database, but that is later...

I use eclipse for developing.

Here is the remoteinterface of the server:
package com.iz.rmi.server;
import java.rmi.*;
import java.rmi.server.*;

import com.iz.rmi.client.IObserver;

public interface ISubject extends Remote
{
     public void registerClient(IObserver obs) throws RemoteException, ServerNotActiveException;
     public void notifyObervers() throws RemoteException, ServerNotActiveException;
}
the remoteinterface of the client:
package com.iz.rmi.client;
import java.rmi.*;
import java.rmi.server.*;

public interface IObserver extends Remote
{
     public void sendNotify(String notification) throws RemoteException, ServerNotActiveException; 
}
the implementation of the server interface:
package com.iz.rmi.server;
import java.net.MalformedURLException;
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;
import java.util.*;

import com.iz.rmi.client.*;

public class Subject extends UnicastRemoteObject implements ISubject
{
     private Vector<IObserver> obs;
     private String service;
     
     public Subject() throws java.rmi.RemoteException
     {
          super();
          this.obs = new Vector<IObserver>();
     }
     
     @Override
     public void notifyObervers() throws RemoteException, ServerNotActiveException 
     {
          Iterator<IObserver> obsIt = this.obs.iterator();
          while(obsIt.hasNext())
          {
               IObserver o = obsIt.next();
               try 
               {
                    o.sendNotify("blabla");
               } 
               catch (Exception e) 
               {
                    e.printStackTrace();
               }
          }
     }
     
     @Override
     public void registerClient(IObserver obs) throws RemoteException, ServerNotActiveException 
     {
          System.out.println("client registered");
          this.obs.add(obs);
     }
     
     public static void main(String[] args) 
     {
          if (System.getSecurityManager() == null) 
          {
            System.setSecurityManager(new SecurityManager());
        }
          
          try 
          {
            String name = "Observable";
            ISubject engine = new Subject();
            //ISubject stub = (ISubject) UnicastRemoteObject.exportObject(engine, 0);
            LocateRegistry.createRegistry(1099);
            Registry registry = LocateRegistry.getRegistry(1099);
            registry.rebind(name, engine);
            System.out.println("ComputeEngine boundlll");
        } 
          catch (Exception e) 
          {
            System.err.println("ComputeEngine exception:");
            e.printStackTrace();
        }

     }
}
and the implementation of the client interface
package com.iz.rmi.client;

import java.rmi.*;
import java.rmi.server.*;
import com.iz.rmi.server.*;

public class Observer extends UnicastRemoteObject implements IObserver
{
     private String host;
     private String service;
     private ISubject sub;
     
     public Observer(String host, String service) throws RemoteException
     {
          this.host = host;
          this.service = service;
          System.out.println("Service: " + service);
          try
          {
               this.sub = (ISubject) Naming.lookup(this.service);
               this.sub.registerClient(this);
               System.out.println(" istered");
          }
          catch(Exception e)
          {
               System.out.println("Unable to connect and register with subject.");
               e.printStackTrace();
          }
     }
     
     @Override
     public void sendNotify(String notification) throws RemoteException,
               ServerNotActiveException 
     {
          
     }
     
     public static void main(String[] args) 
     {
          try {
               new Observer("192.168.1.34:1099", "Observable");
          } catch (RemoteException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
          }
     }

}
Both programs get started with this vm-argument:
-Djava.security.policy=C:\daten\noSpring\Obsv\wideopen.policy

where wideopen.policy looks like this for testing:
grant {
     // Allow everything for now
     permission java.security.AllPermission;
};
when I start the server all looks fine, but when I start the a client i become just the output.
Service: Observable
 istered
instead of
Service: Observable
client registerd
 istered
There are no exceptions or something like that, but it seems that the registerClient()-method does not get called on the server when the client starts. There were many changes since the last time I used rmi, so I don't know what's wrong here. I hope some one can help me with this problem.

Kind regards,
Michael

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points