是否从Play框架中删除了异步作业?什么是更好的选择? [英] Was asynchronous jobs removed from the Play framework? What is a better alternative?

查看:108
本文介绍了是否从Play框架中删除了异步作业?什么是更好的选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 Job 所以我可以在应用程序启动时启动它们。现在它似乎已完全从Play中移除了?

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.

有任何建议吗?

编辑:如果你要投票,请说明理由。也许我在问题中遗漏了一些东西,也许这不属于这里。至少...

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

推荐答案

在Play 2.0中删除了Job类。

The Job class was removed in Play 2.0.

你有一些选择虽然取决于你的Play版本,如果你需要不同步:

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

对于自Play 2.0以来的所有版本,您可以使用Akka Actors安排一次异步任务/演员,并通过Play Global 类在启动时执行它。

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()
           );
      }  
 }

参见 https://www.playframework.com/documentation/2.3.x/JavaAkka 了解详情。

从Play 2.4开始,你可以急切地将单身人士与Guice绑定

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();
    }
}

StartupConfigurationImpl 将在默认构造函数中完成它。

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");
    }
}

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

这篇关于是否从Play框架中删除了异步作业?什么是更好的选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆