You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@karaf.apache.org by Tim Ward <ti...@paremus.com> on 2020/09/02 10:39:40 UTC

Re: Is it possible to activate a DS component only when an HTTP request is completed?

If you’re stuck with the async startup then the best way to approach this sort of thing is to take the “service” bit out of the component and manage the service registration lifecycle yourself. It is much less pretty, but at least DS can manage the service and configuration dependencies for you.

@Component
public class MyComponent {

  AtomicBoolean active = new AtomicBoolean(false);
  AtomicReference<ServiceRegistration<MyService>> reg = new AtomicReference<>();

  @Reference
  MyDependency myDep;

  @Activate
  BundleContext ctx;
  
  @Activate
  Map<String, Object> config;

  @Activate
  void start() {
    active.set(true);
    new Thread(this::asyncStart).run();
  }

  void asyncStart() {
    MyServiceImpl impl = new MyServiceImpl(myDep);
    impl.longRunningSetup();
    If(active.get()) {
      reg.set(ctx.registerService(MyService.class, impl, new Hashtable(config)));
      if(!active.get()) {
        ServiceRegistration sReg = reg.getAndSet(null);
        if(sReg != null) { sReg.unregister(); }
      }
    }
  }

  @Deactivate
  Void stop() {
    active.set(false);
    ServiceRegistration sReg = reg.getAndSet(null);
    if(sReg != null) { sReg.unregister(); }
  }
}


Tim

> On 29 Aug 2020, at 20:16, Steinar Bang <sb...@dod.no> wrote:
> 
>>>>>> Steinar Bang <sb...@dod.no>:
>> Or maybe I don't have to involve the PreHook at all?
> 
>> The data liquibase file doesn't have to be loaded at the same time as
>> the schema, it can be loaded later.
> 
>> But: the schema must be in place before the data is attempted loaded.
> 
> Not a problem: I can create a separate DS component that listens for the
> DataSource produced by jdbc-config, because that won't become active
> until after the PreHook has done its job.
>