Search code examples
playframeworkjobs

Is it ok to launch jobs from an @OnApplicationStart job?


From such a job, I'm doing:

@OnApplicationStart
public class Bootstrap extends Job {
  Foo foo = new Job<Foo>() {
    ...
    return new Foo();
  }.now().get();
}

And I find that the inner job is never executed, bringing the application to a deadlock, because it blocks on the get().

I'm running Play 1.2.4 in Dev Mode. The job is submitted to JobsPlugins.executor, which is initialized to have 10 threads - more than enough. I tried putting a breakpoint in the Callable inside Job.now() - that breakpoint is never hit.


Solution

  • Yes it is supported and works well. One of the reason to do this could be to control the order of jobs execution.

    For my App, only BootstrapJob is annotated with @OnApplicationStart and controls the order of execution

    public void doJob() {
    
        if (User.count() == 0)
            Fixtures.loadModels("user-data.yml");
    
        Logger.info("Starting synchronous jobs.");
        new StaticDataJob().doJob();
    
        Logger.info("Starting asynchronous jobs.");
        new TransactionJob().now();
        new ReportJob().now();
    
            Logger.info("Boostrap job complete.");
    }