3

I wanted to use Job so I can kick them off on the start of application. Now it seems like it has been removed from Play completely?

I saw some samples where people create a Global class, but not entirely sure if/how I should use that to replace Job.

Any suggestions?

Edit: If you gonna downvote, give a reason. Maybe I'm missing something in the question, maybe this doesn't belong here. At least something...

iCodeLikeImDrunk
  • 17,085
  • 35
  • 108
  • 169

1 Answers1

7

The Job class was removed in Play 2.0.

You have some alternatives though depending on your Play version and if you need asynchrony or not:

Akka Actors

For all version since Play 2.0 you can use Akka Actors to schedule an asynchronous task/actor once and execute it on startup via Play Global class.

public class Global extends GlobalSettings {

    @Override
    public void onStart(Application app) {
           Akka.system().scheduler().scheduleOnce(
               Duration.create(10, TimeUnit.MILLISECONDS),
               new Runnable() {
                    public void run() {
                        // Do startup stuff here
                        initializationTask();
                    }
               },
               Akka.system().dispatcher()
           );
      }  
 }

See https://www.playframework.com/documentation/2.3.x/JavaAkka for details.

Eager Singletons

Starting with Play 2.4 you can eagerly bind singletons with Guice

import com.google.inject.AbstractModule;
import com.google.inject.name.Names;

public class StartupConfigurationModule extends AbstractModule {
    protected void configure() {

        bind(StartupConfiguration.class)
            .to(StartupConfigurationImpl.class)
            .asEagerSingleton();
    }
}

The StartupConfigurationImpl would have it's work done in the default constructor.

@Singleton
public class StartupConfigurationImpl implements StartupConfiguration {
    @Inject
    private Logger log;

    public StartupConfigurationImpl() {
        init();
    }

    public void init(){
        log.info("init");
    }
}

See https://www.playframework.com/documentation/2.4.x/JavaDependencyInjection#Eager-bindings

Alexander B
  • 1,023
  • 7
  • 20
  • With Akka Actors I agree, but I just can't see what dependency injection with Guice has to do with task/job management? – Kris Jun 24 '15 at 11:10
  • It's a way of implementing some logic/task at application startup which was part of the question. Eager bound components are created automatically at application start up. If you declare them as singletons they are instantiated only once (at start up). Actors would be the preferred way for asynchronous task where you don't need the outcome of the job for specific components to work. Eager Singletons would probably be preferable if you need the outcome of your start up task to be available to your other components like controllers via injection. – Alexander B Jun 24 '15 at 11:26
  • Ah, I understand, I didn't read the 'at application startup' part. :) – Kris Jun 24 '15 at 11:42
  • @AlexanderBuchholtz i will be working with the latest version, 2.4.1. i'll look over what you posted, thank you! – iCodeLikeImDrunk Jun 24 '15 at 16:01