Why does OSGi select the service with lowest ID if the ID does not need to be ascending timewise?

StackOverflow https://stackoverflow.com/questions/23085193

  •  04-07-2023
  •  | 
  •  

Вопрос

I'm reading a book (OSGi in practice) and there is a part about the ranking of services which explains the rules which are used:

The first is to look at a special service.ranking property on the services, which can be referenced in code as Constants.SERVICE_RANKING. The value of the property is an integer between Integer.MIN_VALUE (i.e. -2,147,483,648) and Integer.MAX_VALUE (2,147,483,647). The service with the highest ranking is selected — services that do not have an explicit ranking property take the implicit value of zero. If this rule produces a tie, then the service with the lowest service ID is selected. The second rule is somewhat arbitrary, but it tends to result in the “oldest” service being selected, since in most framework implementation service IDs are allocated from an incrementing counter (although this behaviour is not part of the specification and cannot be relied upon).

Why does OSGi not enforce the ascending order (timewise) of service ID s?

Why does OSGi select the servcie with the lowest ID if their rank is the same?

Это было полезно?

Решение

Service IDs are assigned by the framework upon service registration. Each new id must be larger than all previously used ids (within that framework launch cycle). This is mandated by the OSGi specification. See section 5.2.5 in the Core spec (so I guess that book is wrong on this topic).

So a lower service id represents a service which was registered earlier than a service with a higher service id. So when ranking is equal, the earlier registered service is chosen.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top