问题
Is there a way to find out or to determine the order of service binding with declarative services? My problem is that I have a logging service running in my OSGI among other services. Now, my component binds this logging service and a couple of other services. I want to write a log message each time a service is bound or unbound. However, if service A is bound before my logging service, no log entry for A can be written.
Can I configure the binding order somehow? I read about setting the binding policy of a service to "static", which is supposed to ensure this service to be bound first of all. But that isn't exactly what I want. I do not want my component to be recreated each time my logging service disappears due to some reason.
回答1:
I agree with the person below about using slf4j.
Can I configure the binding order somehow?
Most likely not with Declaritive Services alone.
This sounds like a good candidate for ServiceTracker. Just keep it closed until your logging service binds. Luckily that's pretty compatible with Declarative Services.
There is also pax-logging, where you don't bind to the LogService but instead use log4j/slf4j APIs so you might not have to worry about the lifecycle of the LogService as much.
There is also a third option where you publish an OSGI Event for each service on bind, then have an event listener which will print the events on LogService, or queue them up until the LogService is available. This is more moving parts than you may like though.
回答2:
Your DS runtime may have some optional logging or tracing options which may help to see what is going on under the hood.
来源:https://stackoverflow.com/questions/19589622/order-of-service-binding-with-declarative-services