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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…