简体   繁体   English

仅当满足特定条件时,如何激活 OSGI 包?

[英]How to activate an OSGI bundle only when the certain condition is satisfied?

I'm a bit new in OSGI and I want the following: to activate my bundle only when some prerequisites are satisfied (which, btw, we get form a native library, but that's another story).我对 OSGI 有点陌生,我想要以下内容:仅在满足某些先决条件时激活我的包(顺便说一句,我们从本机库中获得,但这是另一回事)。 AFAIK it could be achieved through the @Reference DS, but I don't get the idea fully. AFAIK 它可以通过@Reference DS 实现,但我没有完全理解这个想法。 I mean if I write something like this before my @Activate DS:我的意思是,如果我在@Activate DS 之前写下这样的内容:

@Reference
public AnotherService as

@Activate
public void activate() {
   //some code 
}

this actually means, that my bundle won't be activated until the AnotherService is activated.这实际上意味着,在激活另一个服务之前,我的包不会被激活。 But could I write in AnotherService or in my bundle something like this?:但是我可以在AnotherService或我的包中写这样的东西吗?:

@Activate
public void activate() {
   if (condition){
       deactivate()
   }
   //some code 
}

@Deactivate
public void deactivate() {
//some code
}

As far as I understand, that's impossible.据我了解,这是不可能的。 Then the questions arises: how could I control the activation of my bundle or its references depending on certain condition(s)?然后问题出现了:我如何根据某些条件控制我的包或其引用的激活? Ie I want my bundle to be either activated, when the condition is satisfied ( before activation ) or deactivated, when not.即,我希望我的捆绑包在条件满足时(激活之前)被激活,或者在不满足时被停用。 It won't suit for me the way: "just make if-statement, if it is not satisfied, do nothing, but still be activated", because the 'activiy' of this bundle is very resource heavy.它不适合我的方式:“只做 if 语句,如果不满足,什么也不做,但仍然被激活”,因为这个包的 'activiy' 资源非常重。 Maybe I just have a fully wrong idea of OSGI.也许我只是对 OSGI 有一个完全错误的想法。

This is not something you should do.这不是你应该做的事情。 Your bundle should always be activated as long as it can be resolved (ie all of its imports and other static dependencies are satisfied).只要可以解析您的捆绑包,就应该始终激活它(即,它的所有导入和其他 static 依赖项都得到满足)。

In fact, unless you are coding OSGi at an advanced level, you should not write BundleActivators at all.事实上,除非您在高级级别编写 OSGi,否则您根本不应该编写 BundleActivators。 If you are reading an OSGi tutorial in 2020 that tells you to write a BundleActivator, stop reading... that tutorial is junk.如果您正在阅读 2020 年的 OSGi 教程,该教程告诉您编写 BundleActivator,请停止阅读……该教程是垃圾。

You should use Declarative Services components.您应该使用声明式服务组件。 A component is an object managed by OSGi and can have its lifecycle bound to the availability of a service.组件是由 OSGi 管理的 object,并且可以将其生命周期绑定到服务的可用性。 For example:例如:

@Component
public class MyComponent implements MyService {

  private final AnotherService service;

  @Activate
  public MyComponent(@Reference AnotherService service) {
    this.service = service;
  }

  @Override
  public void doSomething() {
    // ...
  }
}

In this example, an instance of MyComponent will be created and published as a service of type MyService only when a service of type AnotherService is available.在此示例中,AnotherService类型的服务可用时,才会创建MyComponent的实例并将其作为MyService类型的服务发布。 You will be able to invoke the AnotherService via the final service field.您将能够通过最终service字段调用AnotherService This is a mandatory service reference, which is the default in DS, but it's also possible to create optional and dynamic references (ie references that can re-bind to other service instances during the lifetime of your component), but don't worry about those use-cases until later in your OSGi learning.这是一个强制服务引用,是 DS 中的默认值,但也可以创建可选和动态引用(即可以在组件生命周期内重新绑定到其他服务实例的引用),但不必担心这些用例直到你学习 OSGi 的后期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JavaEE和OSGI:如何为某些osgi软件包获取BeanManager - JavaEE and OSGI:How to get BeanManager for certain osgi bundle 如何在单个线程中执行 OSGI 包的激活和停用方法 - How to execute activate and deactivate methods of OSGI bundle in a single thread 当所有引用都满足时,为什么我的Karaf OSGI包标记为UNSATISFIED? - Why is my Karaf OSGI bundle marked UNSATISFIED, when all references are satisfied? 有什么方法可以从另一个正在运行的捆绑软件中激活/运行osgi Bundle? - Is there any way to activate/run osgi Bundle form another running bundle? 如何创建OSGI包? - How to create OSGI bundle? 如何在仅由反射引用的OSGi / Bndtools中访问包? - How to access bundle in OSGi/Bndtools that only referenced by reflections? 使用Maven进行构建时如何在OSGI捆绑包中导入javax.smartcardio? - How to import javax.smartcardio in a osgi bundle when building with maven? 在与maven一起使用OSGI包时如何隐藏嵌入式依赖项? - How to hide embedded dependencies when using an OSGI bundle with maven? OSGI:Activate 和 Bind 方法在重新启动捆绑包后更改启动顺序 - OSGI: Activate and Bind methods changes starting order after restarting the bundle 在Netbeans和Karaf中开发OSGI捆绑包时如何管理依赖项? - How to manage dependencies when developing OSGI bundle in Netbeans and Karaf?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM