Quarkus CDI: lookup and startup event

31 Views Asked by At

Starting from the code in the official documentation, I have created the following structure:

interface Service {
  String name();
}

@LookupIfProperty(name = "service.foo.enabled", stringValue = "true")
@ApplicationScoped
class ServiceFoo implements Service {

  public String name() {
    return "foo";
  }
}

@ApplicationScoped
class ServiceBar implements Service {

  @Startup
  public void onStart() { // startup logic }

  public String name() {
    return "bar";
  }
}

The code in ServiceBar.onStart() gets always executed even if the bean does not get injected. Is there a way to execute the ServiceBar startup logic only when ServiceFoo is disabled?

1

There are 1 best solutions below

0
Fabrizio Fortino On

As already described by @ladicek, LookupIfProperty does not affect enablement. As a workaround, I have changed the code in the following way:

interface Service {
  String name();
}

@LookupIfProperty(name = "service.foo.enabled", stringValue = "true")
@ApplicationScoped
class ServiceFoo implements Service {

  public String name() {
    return "foo";
  }
}

@ApplicationScoped
class ServiceBar implements Service {

  @ConfigProperty(name = "service.foo.enabled", defaultValue = "false")
  Boolean isFooEnabled;

  @Startup
  public void onStart() { 
    if (!isFooEnabled) {
      // startup logic
    }
  }

  public String name() {
    return "bar";
  }
}

In this way, the startup logic gets executed only when ServiceFoo is not enabled. Is not super elegant, but it works.