3

I have an OSGi component:

@Component(immediate=true)
public class SomeComponent implements SomeInterface {...}

Note that the component is immediate. Also, since the component class is implementing an interface, I understand that a service will be registered automatically upon (as part of the) activation of the component.

Now, I would like to do be able to dynamically interrupt the component activation and service registration if certain conditions are met. Throwing a 'ComponentException' in a component activator doesn't seem to do the job:

@Activate
public void activate() {
  if (notReady)
    throw new ComponentException("Component not ready");
}

Any suggestions? Thanks!

medalik
  • 139
  • 1
  • 10

1 Answers1

4

You cannot preempt the service registration which happens before activation. You would be better off with two components. One which is immediate with no service which decides whether to enable or disable the second component which has the service. This second component can be disabled by default.

BJ Hargrave
  • 9,324
  • 1
  • 19
  • 27