Re: Unique ID per client JVM
Gregg Wonderly <[email protected]>
| Newsgroups | gmane.comp.java.sun.jini |
|---|---|
| Message-ID | <[email protected]> |
cowwoc wrote:
> I am experimenting with alternatives to PreferredClassLoader. Using
> plain RMI I've got a single server exporting a single object, then I
> have with multiple clients calling into it. I was wondering whether
> there is some RMI mechanism for retrieving a unique id per client JVM
> connecting to the server. I know about RemoteServer.getClientHost() but
> a single host may contain multiple JVMs and I want different IDs per JVM.
Since you are not using JERI, one way is to use a smart proxy that contains a
unique ID that the server assigns when it returns the smart proxy to the client.
This would require the clients to use some sort of factory method on the
server so that it could hand out the ID's separately.
Your service interface might be
public interface MyService extends Remote {
public ClientInterface getImpl() throws RemoteException;
}
And you might define
public interface ClientInterface extends Remote {
public void method1( String arg ) throws RemoteException;
public int method2( String arg, Foobar other ) throws RemoteException;
}
public interface ServiceInterface extends Remote {
public void method1( long id, String arg ) throws RemoteExeption;
public int method2( long id, String arg, Foobar other
) throws RemoteException;
}
Your service would then be
public class MyServiceImpl implements MyService {
ServiceInterface impl;
public MyServiceImpl() {
impl = createImpl(); // Do something to export an implementation
}
public ClientInterface getImpl() {
return new ClientInterfaceSmartProxy( impl, nextID() );
}
}
public class ClientInterfaceSmartProxy implements ClientInterface {
private ServiceInterface impl;
long id;
public ClientInterfaceSmartProxy( ServiceInterface impl, long id ) {
this.id = id;
this.impl = impl;
}
public void method1( String arg ) throws RemoteException {
impl.method1( id, arg );
}
public int method2( String arg, Foobar other ) {
return impl.method2( id, arg, other );
}
}
the createImpl() method needs to create and export and implementation of
ServiceInterface that does what needs to be done with the id and the other
arguments.
If you were using JERI and Jini Configuration, you could just write a custom
BasicInvocationHandler subclass and BasicInvocationDispatcher subclass which are
created in your configuration, and you could pass the ID into the
Configuration.getEntry() call to get an exporter and have it create the
BasicInvocationHandler subclass with the id value to pass through to the
BasicInvocationDispatcher, which could then hide the id in a ThreadLocal for
your code to check for and use, or call a different method that included that
argument etc.
And of course there are other ways as well...
Gregg Wonderly
--------------------------------------------------------------------------
Getting Started: http://www.jini.org/wiki/Category:Getting_Started
Community Web Site: http://jini.org
jini-users Archive: http://archives.java.sun.com/archives/jini-users.html
Unsubscribing: email "signoff JINI-USERS" to [email protected]