Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
431 views
in Technique[技术] by (71.8m points)

java - Was asynchronous jobs removed from the Play framework? What is a better alternative?

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...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...