服务顺序与声明式服务的绑定

服务顺序与声明式服务的绑定

本文介绍了服务顺序与声明式服务的绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以找到或确定与声明式服务的服务绑定顺序?我的问题是我的OSGI中有一个运行日志记录的服务以及其他服务.现在,我的组件将绑定此日志记录服务和其他几个服务.每当服务绑定或未绑定时,我都想写一条日志消息.但是,如果将服务A绑定到我的日志记录服务之前,则无法写入A的日志条目.

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.

推荐答案

我同意以下有关使用slf4j的人的意见.

I agree with the person below about using slf4j.

很可能不是仅声明式服务.

Most likely not with Declaritive Services alone.

这听起来像是ServiceTracker的不错的选择.只需保持关闭状态,直到您的日志记录服务绑定.幸运的是,它与声明式服务完全兼容.

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.

还有pax日志记录,您无需绑定到LogService,而是使用log4j/slf4j API,因此您不必担心LogService的生命周期.

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.

还有第三个选项,其中您为绑定中的每个服务发布OSGI事件,然后有一个事件侦听器,该事件侦听器将在LogService上打印事件,或将事件排队直到LogService可用.这比您可能想要的要多得多.

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.

这篇关于服务顺序与声明式服务的绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 12:01