You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@river.apache.org by Rupert Smith <ru...@googlemail.com> on 2010/12/03 14:06:48 UTC

How do I get notified of changes to services?

I am now using a LookupCache to attempt to receive notification on the
availability of services. Here is the code I use to create the cache:

        // Locate the Jini registry.
        LookupLocator lookup = new LookupLocator(jiniRegistryURL);
        LookupLocatorDiscovery discovery = new LookupLocatorDiscovery(new
LookupLocator[] { lookup });
        discovery.addDiscoveryListener(this);

        // Create a service discovery manager for the JMX Connector
services.
        ServiceDiscoveryManager serviceDiscoveryManager =
            new ServiceDiscoveryManager(discovery, new
LeaseRenewalManager());

        Class[] classes = new Class[] { JMXConnector.class };
        Entry[] serviceAttrs =
                new Entry[] { new
com.sun.jini.lookup.entry.BasicServiceType(JMX_CONNECTOR_SERVICE_NAME) };
        ServiceTemplate template = new ServiceTemplate(null, classes,
serviceAttrs);

        // Register to be notified of changes to the available JMX Connector
services.
        LookupCache cache =
serviceDiscoveryManager.createLookupCache(template, null, this);

It works, but... Only if the requested service has already been registered
(by a seperate app) _before+ this code is run. Also when a service lease is
cancelled, the 'serviceRemoved' listener method is not invoked. I must be
doing something wrong.

I was expecting that after the above is run, the cache is now running, and
new service registrations/expiries will continue to result in calls to the
local listener methods. Have I misconfiguration things? Also I just used
'new LeaseRenewalManager()' with no-args, to fill in one parameter without
really understanding what I am doing there. Could that be the problem?

Thanks for the help so far, I feel like I am almost there now.

Rupert

=================

Here is the code the apps use, on being shutdown to deregister their
services (the lease is cancelled), I presume thats the right way to do it?

private void unregisterRMIConnector()
    {
        if (rmiConnectorRegistration != null)
        {
            log.fine("Unregistering ServiceID: " +
rmiConnectorRegistration.getServiceID().toString());

            try
            {
                rmiConnectorRegistration.getLease().cancel();
            }
            catch (UnknownLeaseException e)
            {
                // Best effort has been made to cancel the registration,
compensating action, registration is nulled.
                e = null;
            }
            catch (RemoteException e)
            {
                // Best effort has been made to cancel the registration,
compensating action, registration is nulled.
                e = null;
            }

            rmiConnectorRegistration = null;
        }
    }

Re: How do I get notified of changes to services?

Posted by Rupert Smith <ru...@googlemail.com>.
Hi, I meant to respond to this a little sooner... I'm way to busy with other
stuff to fully implement something like this, but I will attach my JMX/Jini
code to the JIRA once its fully working.

Essentially there are two parts: MBeanJiniPublisher and MBeanJiniListener.
Publish and MBean on the publisher and the corresponding listener will find
it (with the right query set up on it). You could publish the JVM Mbean and
use it to find out the kinds of things mentioned in the Jira, but the
publisher/listener pair I have written is not exactly what the Jira
describes. Might make an ok starting point though.

Rupert

On 3 December 2010 18:39, Tom Hobbs <tv...@googlemail.com> wrote:

> Hi Rupert,
>
> We were talking on the developer list about getting JMX (and other
> information) available as a built-in part of River.
>
> https://issues.apache.org/jira/browse/RIVER-376
>
> I wonder if you'd be interested in being involved in this effort at all?
>
> Cheers,
>
> Tom
>
>
>
> On Fri, Dec 3, 2010 at 1:17 PM, Rupert Smith
> <ru...@googlemail.com> wrote:
> > Hi,
> >
> > I got it working. The problem was that I am using a security manager, and
> a
> > permission was missing:
> >
> >   permission com.sun.jini.thread.ThreadPoolPermission
> "getUserThreadPool";
> >
> > Which I presume the LookupCache needed. It resulted in an uncaught
> > exception, which was logged, and I wouldn't have spotted it, if I hadn't
> set
> > of Log4J and Chainsaw. Seems to be logged but swallowed. I usually add a
> > top-level error handler to all threads in my code, that logs and calls
> > System.exit(-1), on any uncaught exceptions. Drastic, but at least you
> find
> > out quickly about the problem.
> >
> > Thanks again Tom and Dennis for the replies. Got this all ready to fly to
> > now!
> >
> > Rupert
> >
> > On 3 December 2010 13:06, Rupert Smith <ru...@googlemail.com>
> wrote:
> >
> >> I am now using a LookupCache to attempt to receive notification on the
> >> availability of services. Here is the code I use to create the cache:
> >>
> >>         // Locate the Jini registry.
> >>         LookupLocator lookup = new LookupLocator(jiniRegistryURL);
> >>         LookupLocatorDiscovery discovery = new
> LookupLocatorDiscovery(new
> >> LookupLocator[] { lookup });
> >>         discovery.addDiscoveryListener(this);
> >>
> >>         // Create a service discovery manager for the JMX Connector
> >> services.
> >>         ServiceDiscoveryManager serviceDiscoveryManager =
> >>             new ServiceDiscoveryManager(discovery, new
> >> LeaseRenewalManager());
> >>
> >>         Class[] classes = new Class[] { JMXConnector.class };
> >>         Entry[] serviceAttrs =
> >>                 new Entry[] { new
> >> com.sun.jini.lookup.entry.BasicServiceType(JMX_CONNECTOR_SERVICE_NAME)
> };
> >>         ServiceTemplate template = new ServiceTemplate(null, classes,
> >> serviceAttrs);
> >>
> >>         // Register to be notified of changes to the available JMX
> >> Connector services.
> >>         LookupCache cache =
> >> serviceDiscoveryManager.createLookupCache(template, null, this);
> >>
> >> It works, but... Only if the requested service has already been
> registered
> >> (by a seperate app) _before+ this code is run. Also when a service lease
> is
> >> cancelled, the 'serviceRemoved' listener method is not invoked. I must
> be
> >> doing something wrong.
> >>
> >> I was expecting that after the above is run, the cache is now running,
> and
> >> new service registrations/expiries will continue to result in calls to
> the
> >> local listener methods. Have I misconfiguration things? Also I just used
> >> 'new LeaseRenewalManager()' with no-args, to fill in one parameter
> without
> >> really understanding what I am doing there. Could that be the problem?
> >>
> >> Thanks for the help so far, I feel like I am almost there now.
> >>
> >> Rupert
> >>
> >> =================
> >>
> >> Here is the code the apps use, on being shutdown to deregister their
> >> services (the lease is cancelled), I presume thats the right way to do
> it?
> >>
> >> private void unregisterRMIConnector()
> >>     {
> >>         if (rmiConnectorRegistration != null)
> >>         {
> >>             log.fine("Unregistering ServiceID: " +
> >> rmiConnectorRegistration.getServiceID().toString());
> >>
> >>             try
> >>             {
> >>                 rmiConnectorRegistration.getLease().cancel();
> >>             }
> >>             catch (UnknownLeaseException e)
> >>             {
> >>                 // Best effort has been made to cancel the registration,
> >> compensating action, registration is nulled.
> >>                 e = null;
> >>             }
> >>             catch (RemoteException e)
> >>             {
> >>                 // Best effort has been made to cancel the registration,
> >> compensating action, registration is nulled.
> >>                 e = null;
> >>             }
> >>
> >>             rmiConnectorRegistration = null;
> >>         }
> >>     }
> >>
> >>
> >>
> >
>

Re: How do I get notified of changes to services?

Posted by Tom Hobbs <tv...@googlemail.com>.
Hi Rupert,

We were talking on the developer list about getting JMX (and other
information) available as a built-in part of River.

https://issues.apache.org/jira/browse/RIVER-376

I wonder if you'd be interested in being involved in this effort at all?

Cheers,

Tom



On Fri, Dec 3, 2010 at 1:17 PM, Rupert Smith
<ru...@googlemail.com> wrote:
> Hi,
>
> I got it working. The problem was that I am using a security manager, and a
> permission was missing:
>
>   permission com.sun.jini.thread.ThreadPoolPermission "getUserThreadPool";
>
> Which I presume the LookupCache needed. It resulted in an uncaught
> exception, which was logged, and I wouldn't have spotted it, if I hadn't set
> of Log4J and Chainsaw. Seems to be logged but swallowed. I usually add a
> top-level error handler to all threads in my code, that logs and calls
> System.exit(-1), on any uncaught exceptions. Drastic, but at least you find
> out quickly about the problem.
>
> Thanks again Tom and Dennis for the replies. Got this all ready to fly to
> now!
>
> Rupert
>
> On 3 December 2010 13:06, Rupert Smith <ru...@googlemail.com> wrote:
>
>> I am now using a LookupCache to attempt to receive notification on the
>> availability of services. Here is the code I use to create the cache:
>>
>>         // Locate the Jini registry.
>>         LookupLocator lookup = new LookupLocator(jiniRegistryURL);
>>         LookupLocatorDiscovery discovery = new LookupLocatorDiscovery(new
>> LookupLocator[] { lookup });
>>         discovery.addDiscoveryListener(this);
>>
>>         // Create a service discovery manager for the JMX Connector
>> services.
>>         ServiceDiscoveryManager serviceDiscoveryManager =
>>             new ServiceDiscoveryManager(discovery, new
>> LeaseRenewalManager());
>>
>>         Class[] classes = new Class[] { JMXConnector.class };
>>         Entry[] serviceAttrs =
>>                 new Entry[] { new
>> com.sun.jini.lookup.entry.BasicServiceType(JMX_CONNECTOR_SERVICE_NAME) };
>>         ServiceTemplate template = new ServiceTemplate(null, classes,
>> serviceAttrs);
>>
>>         // Register to be notified of changes to the available JMX
>> Connector services.
>>         LookupCache cache =
>> serviceDiscoveryManager.createLookupCache(template, null, this);
>>
>> It works, but... Only if the requested service has already been registered
>> (by a seperate app) _before+ this code is run. Also when a service lease is
>> cancelled, the 'serviceRemoved' listener method is not invoked. I must be
>> doing something wrong.
>>
>> I was expecting that after the above is run, the cache is now running, and
>> new service registrations/expiries will continue to result in calls to the
>> local listener methods. Have I misconfiguration things? Also I just used
>> 'new LeaseRenewalManager()' with no-args, to fill in one parameter without
>> really understanding what I am doing there. Could that be the problem?
>>
>> Thanks for the help so far, I feel like I am almost there now.
>>
>> Rupert
>>
>> =================
>>
>> Here is the code the apps use, on being shutdown to deregister their
>> services (the lease is cancelled), I presume thats the right way to do it?
>>
>> private void unregisterRMIConnector()
>>     {
>>         if (rmiConnectorRegistration != null)
>>         {
>>             log.fine("Unregistering ServiceID: " +
>> rmiConnectorRegistration.getServiceID().toString());
>>
>>             try
>>             {
>>                 rmiConnectorRegistration.getLease().cancel();
>>             }
>>             catch (UnknownLeaseException e)
>>             {
>>                 // Best effort has been made to cancel the registration,
>> compensating action, registration is nulled.
>>                 e = null;
>>             }
>>             catch (RemoteException e)
>>             {
>>                 // Best effort has been made to cancel the registration,
>> compensating action, registration is nulled.
>>                 e = null;
>>             }
>>
>>             rmiConnectorRegistration = null;
>>         }
>>     }
>>
>>
>>
>

Re: How do I get notified of changes to services?

Posted by Rupert Smith <ru...@googlemail.com>.
Hi,

I got it working. The problem was that I am using a security manager, and a
permission was missing:

   permission com.sun.jini.thread.ThreadPoolPermission "getUserThreadPool";

Which I presume the LookupCache needed. It resulted in an uncaught
exception, which was logged, and I wouldn't have spotted it, if I hadn't set
of Log4J and Chainsaw. Seems to be logged but swallowed. I usually add a
top-level error handler to all threads in my code, that logs and calls
System.exit(-1), on any uncaught exceptions. Drastic, but at least you find
out quickly about the problem.

Thanks again Tom and Dennis for the replies. Got this all ready to fly to
now!

Rupert

On 3 December 2010 13:06, Rupert Smith <ru...@googlemail.com> wrote:

> I am now using a LookupCache to attempt to receive notification on the
> availability of services. Here is the code I use to create the cache:
>
>         // Locate the Jini registry.
>         LookupLocator lookup = new LookupLocator(jiniRegistryURL);
>         LookupLocatorDiscovery discovery = new LookupLocatorDiscovery(new
> LookupLocator[] { lookup });
>         discovery.addDiscoveryListener(this);
>
>         // Create a service discovery manager for the JMX Connector
> services.
>         ServiceDiscoveryManager serviceDiscoveryManager =
>             new ServiceDiscoveryManager(discovery, new
> LeaseRenewalManager());
>
>         Class[] classes = new Class[] { JMXConnector.class };
>         Entry[] serviceAttrs =
>                 new Entry[] { new
> com.sun.jini.lookup.entry.BasicServiceType(JMX_CONNECTOR_SERVICE_NAME) };
>         ServiceTemplate template = new ServiceTemplate(null, classes,
> serviceAttrs);
>
>         // Register to be notified of changes to the available JMX
> Connector services.
>         LookupCache cache =
> serviceDiscoveryManager.createLookupCache(template, null, this);
>
> It works, but... Only if the requested service has already been registered
> (by a seperate app) _before+ this code is run. Also when a service lease is
> cancelled, the 'serviceRemoved' listener method is not invoked. I must be
> doing something wrong.
>
> I was expecting that after the above is run, the cache is now running, and
> new service registrations/expiries will continue to result in calls to the
> local listener methods. Have I misconfiguration things? Also I just used
> 'new LeaseRenewalManager()' with no-args, to fill in one parameter without
> really understanding what I am doing there. Could that be the problem?
>
> Thanks for the help so far, I feel like I am almost there now.
>
> Rupert
>
> =================
>
> Here is the code the apps use, on being shutdown to deregister their
> services (the lease is cancelled), I presume thats the right way to do it?
>
> private void unregisterRMIConnector()
>     {
>         if (rmiConnectorRegistration != null)
>         {
>             log.fine("Unregistering ServiceID: " +
> rmiConnectorRegistration.getServiceID().toString());
>
>             try
>             {
>                 rmiConnectorRegistration.getLease().cancel();
>             }
>             catch (UnknownLeaseException e)
>             {
>                 // Best effort has been made to cancel the registration,
> compensating action, registration is nulled.
>                 e = null;
>             }
>             catch (RemoteException e)
>             {
>                 // Best effort has been made to cancel the registration,
> compensating action, registration is nulled.
>                 e = null;
>             }
>
>             rmiConnectorRegistration = null;
>         }
>     }
>
>
>