I've been gradually working my way through Core Jini (2nd ed.), and I've hit a problem with the lookup cache, as in it always returns null. I have been able to locate the service item successfully using the SericeDiscoveryManager lookup method, but when I create a lookup cache and then later search this for the same service item, it's always returned null. Why does the cache not work?
Below is a basic description of what I'm doing:
--------------------------------------------------------------------------- - -------------------------------------------------------------------------- Class es: Service, ServiceProvider, Client
Ports: HttpServer - port 8080 Rmid - default port 1098(?) Service HttpServer - port 8086
VM Params: -Drmi.server.codebase=http:\\hostname:8086 -Djava.security.policy={position of policy file, which grants all Permissions} --------------------------------------------------------------------------- -- -------------------------------------------------------------------------
The service is registered with reggie in ServiceProvider using JoinManager:
LookupDiscoveryManager discoveryManager = new LookupDiscoveryManager(new String[] { //group name }, null, null); // the parameter 'this' is the ServiceIDListener, which is implemented // by ServiceProvider. JoinManager joinManager = new JoinManager(this.service, null, this, discoveryManager, null);
I know this works because I have a log which prints out the service Id, thus proving that the service has been successfully registered. I've narrowed down the problem to the lookup cache...
- First initialise the lookup cache:
LookupDiscoveryManager discoveryManager = new LookupDiscoveryManager(new String[] { //group name }, null, null); serviceDiscoveryManager = new ServiceDiscoveryManager(discoveryManager, null); template = new ServiceTemplate(null,new Class[]{ Service.class }, null);
// CachingListener is an inner class which implements the // ServiceDiscoveryListener interface. lookupCache = serviceDiscoveryManager.createLookupCache(template, null, new CachingListener());
- Then look for a service:
while(true){ try { // give it 20 seconds so as to rule out the possibility of // not having enough time. Thread.sleep(20000); ServiceItem serviceItem = lookupCache.lookup(null); logger.info("Service item found in cache = " + serviceItem); } catch (InterruptedException e) { } }
The problem is that when a lookup on a cache occurs, the ServiceItem returned is always null. However if the lookup is called directly on the ServiceDiscoveryManager, the ServiceItem is returns as the service that was registered through the JoinManager.
while(true){ try { // give it 20 seconds so as to rule out the possibility of // not having enough time. ServiceItem service = serviceDiscoveryManager.lookup(template, null); logger.info("Service item found without cache = " + service); } catch (InterruptedException e) { } }
Can anyone see why the use of LookupCache, as above, fails to locate a service that I know is registered?
The example above assumes that you have two HTTP servers, one listening on port 8082, serving your application-specific JAR files, and one listening on port 8080, serving the JAR files provided with the Jini Technology Starter Kit.