Reputation: 17806
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...
Upvotes: 3
Views: 1194
Reputation: 1033
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:
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.
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
Upvotes: 7