Hi Phillipp,

The OSGi service registry itself won't care about you doing this. The
EventAdmin implementation *might*, but it depends on the
implementation's internals... though they should be able to handle
this scenario cleanly.

Here is what should happen:

1) Your handler is called by the event dispatch thread, which is
usually a thread owned by the EventAdmin implementation, NOT the
original thread in which the client called sendEvent or postEvent.

2) Your handler unregisters itself, this will cause a ServiceEvent to be fired.

3) The EventAdmin implementation will receive the ServiceEvent (NB
this is synchronous so we are still in the event dispatch thread) and
will remove your handler from its internal list of handlers.

4) The next time an event is fired, your handler will not receive it.

Regarding your general question, there is no problem modifying the
service registry during a call to the service, since by that time the
client will have already finished querying the registry.

That is, when a client wants to get a list of services it calls
getServiceReferences()... that returns an array of ServiceReferences,
which is a snapshot at that point in time of the current services. It
is the client's responsibility to handle changes to the registry
during iteration, i.e. it should:

* check that the references in the array are still valid by the time
it reaches them, and;
* possibly listen for new services arriving during the iteration.

This is certainly quite tricky for the client but that's the nature of
iterating over a dynamically changing list.

Regards,
Neil

On Sat, Jun 4, 2011 at 10:13 AM, Philipp Kursawe <[email protected]> wrote:
> Hello,
>
> i wonder how OSGi is supposed to handle the following situation. I
> have an EventHander that needs to unregister itself when a specific
> topic is reported.
> Can the OSGi service registry handle such cases?
>
> class MyEventHandler implements EventHandler {
>
>  private ServiceRegistration reg;
>
>  void handleEvent(Event e) {
>    if (e.getTopic.equals("TOPIC")) {
>      reg.unregister();
>    }
>  }
> }
>
> Would that disturb the list of EventHandler services?
>
> How is that handled generally in the service registry where you
> iterate over a list of services (references) and modify the list of
> services during the calls into the services?
>
> Thanks!
> Phil
> _______________________________________________
> OSGi Developer Mail List
> [email protected]
> https://mail.osgi.org/mailman/listinfo/osgi-dev
>

_______________________________________________
OSGi Developer Mail List
[email protected]
https://mail.osgi.org/mailman/listinfo/osgi-dev

Reply via email to