|
Replies:
1
-
Pages:
1
-
Last Post:
Apr 25, 2001 11:00 PM
by: import-bot
|
|
|
Posts:
20,296
Registered:
12/6/03
|
|
|
|
RMI Callback - Code
Posted:
Jan 16, 2001 11:00 PM
|
|
[Originally posted by coltrane]
Here is RMI callback code which works on a PC:
Note: the ClientInterface.java extends java.rmi.Remote (not extending Remote will work on Unix, if I remember correctly, but not on a PC). Also notice the line "UnicastRemoteObject.exportObject(client);".
Note: the ServerImpl.java is a little different that the book - I have implemented Runnable and created a thread which simply changes an integer value every second and notifies the Client of this.
Note: after compiling the java files "rmic ServerImpl Client" which of course will create a skeleton and stub for each of ServerImpl and Client. Then just "start rmiregistry 4000", "start java ServerImpl" and "java Client"
All should now work like clockwork!!
Client.java file:
import java.rmi.*; import java.rmi.server.*;
public class Client implements ClientInterface {
public static void main(String[] args) { Client client=new Client(); try { UnicastRemoteObject.exportObject(client); ServerInterface si=(ServerInterface)Naming.lookup("//:4000/ServerImpl"); si.register(client); } catch(Exception e) { e.printStackTrace(); System.exit(2); } }
public synchronized void update(int i) throws RemoteException { System.out.println("New callback integer: "+i); } }
ClientInterface.java file:
import java.rmi.*;
public interface ClientInterface extends java.rmi.Remote { void update(int i) throws RemoteException; }
ServerInterface.java file:
import java.rmi.*;
public interface ServerInterface extends java.rmi.Remote { void register(ClientInterface ci) throws RemoteException; void unregister(ClientInterface ci) throws RemoteException; }
ServerImpl.java file:
import java.rmi.*; import java.rmi.server.*; import java.rmi.registry.*; import java.util.*;
public class ServerImpl extends UnicastRemoteObject implements ServerInterface, Runnable { private Vector clients=new Vector();
public ServerImpl() throws RemoteException {}
public void run() { int i=0; while(true) { for(Enumeration e=clients.elements(); e.hasMoreElements(); ) { ClientInterface client=(ClientInterface)e.nextElement(); try { client.update(i); } catch(Exception ex) { try { unregister(client); } catch(Exception ex2) { ex2.printStackTrace(); } } } i++; try { Thread.sleep(1000); } catch (InterruptedException e) { } } }
public synchronized void register(ClientInterface ci) throws RemoteException { if(!(clients.contains(ci))) { clients.addElement(ci); System.out.println("Registered new client"+ci); } }
public synchronized void unregister(ClientInterface ci) throws RemoteException { if(clients.removeElement(ci)) { System.out.println("Client "+ci+" removed."); } else { System.out.println("Unregister: Client "+ci+" not registered."); } }
public static void main(String[] args) { System.setSecurityManager(new RMISecurityManager()); try { LocateRegistry.createRegistry(4000); ServerImpl si=new ServerImpl(); Naming.rebind("//:4000/ServerImpl",si); System.out.println("ServerImpl bound and ready."); Thread i=new Thread(si, "ServerImpl"); i.start(); } catch(Exception e) { e.printStackTrace(); } } }
|
|
Posts:
20,296
Registered:
12/6/03
|
|
|
|
Re: RMI Callback - Code
Posted:
Apr 25, 2001 11:00 PM
in response to:
import-bot
|
|
[Originally posted by ryuanchen]
Thanks, it worked. I had to do two things first though:
1 ) I commented out in ServerImpl.java //LocateRegistry.createRegistry(4001):
2 ) And I had to use a policy file like this java -Djava.security.policy=policy.all ServerImpl
Where policy.all simply contains code I got one of the messages in this forum:
grant { permission java.security.AllPermission "", ""; };
These are specific to my machine, but thought I'd share to save other's time too in case they ran into the same problems.
Ryan
|
|
|
Legend
|
|
Gold: 300
+
pts
|
|
Silver: 100
- 299
pts
|
|
Bronze: 25
- 99
pts
|
|
Manning Author
|
|
Manning Staff
|
|