首页 \ 问答 \ Playframework + Akka:如何避免在关闭应用程序时激活执行的计划任务(Playframework + Akka: how to avoid excuting scheduled tasks executed when shutdown application)

Playframework + Akka:如何避免在关闭应用程序时激活执行的计划任务(Playframework + Akka: how to avoid excuting scheduled tasks executed when shutdown application)

我在调试应用程序服务器启动时启动了调度程序的问题,但是一旦应用程序关闭,它就会触及以下部分代码:

// firstDay something like 1 = monday 
private void startScheduler(final ImageService imageService,
                            final ActorSystem system) {
    startImagesCleanupScheduler(imageService, system);
    Logger.info("Schedulers started");
}

我的问题是Runnable块立即开始执行而不是取消任务。

澄清代码:

以下方法启动Scheduler

private void startImagesCleanupScheduler(ImageService imageService, ActorSystem system) {
    system.scheduler().schedule(
            Duration.create(0, TimeUnit.MILLISECONDS), //Initial delay
            Duration.create(1, TimeUnit.DAYS),     //Frequency 1 days
            () -> {
                int rows = imageService.cleanupInactiveImages();
                Logger.info(String.format("%d inactive unused images cleaned from db", rows));

            },
            system.dispatcher()
    );
}

我关机时的日志请注意这里的第一行:

[info] - application - 1 inactive unused images cleaned from db
[info] - application - Shutting down connection pool.
[info] - application - Creating Pool for datasource 'default'
...
[info] - application - Schedulers started
[info] - play.api.Play - Application started (Prod)

您可以看到它执行Scheduler忽略其原始执行时间,然后关闭,然后启动,然后“Schedulers started”。

问题是,如何取消调度程序或阻止播放在关机前运行? 这是Akka的错误吗?

我在OnStartup调用startScheduler ,就像下面问题的答案一样:

java Playframework对于onStart的GlobalSettings弃用

编辑:以下重现问题的最小代码:

首先创建OnStartup类:

@Singleton
public class OnStartup {

    @Inject
    public OnStartup(final ActorSystem system) {
        startScheduler(system);
    }

    private void startScheduler(final ActorSystem system) {
        startImagesCleanupScheduler(system);
        Logger.info("Schedulers started");
    }

    private void startImagesCleanupScheduler(ActorSystem system) {
        system.scheduler().schedule(
                Duration.create(0, TimeUnit.MILLISECONDS), //Initial delay
                Duration.create(1, TimeUnit.DAYS),     //Frequency 1 days
                () -> {
                    //int rows = imageService.cleanupInactiveImages();
                    rows = 1;
                    Logger.info(String.format("%d inactive unused images cleaned from db", rows ));
                },
                system.dispatcher()
        );
    }

}

然后创建模块:

public class OnStartupModule extends AbstractModule {
    @Override
    public void configure() {
        bind(OnStartup.class).asEagerSingleton();
    }
}

最后在application.conf中启用模块:

play.modules.enabled += "modules.OnStartupModule"

I have issue with scheduler that I start when the play application server start, but once application got shutdown, its hit the following part of code :

// firstDay something like 1 = monday 
private void startScheduler(final ImageService imageService,
                            final ActorSystem system) {
    startImagesCleanupScheduler(imageService, system);
    Logger.info("Schedulers started");
}

My problem that the Runnable block start to execute immediately instead of just cancel the task.

To clarify code:

The following method to start the Scheduler:

private void startImagesCleanupScheduler(ImageService imageService, ActorSystem system) {
    system.scheduler().schedule(
            Duration.create(0, TimeUnit.MILLISECONDS), //Initial delay
            Duration.create(1, TimeUnit.DAYS),     //Frequency 1 days
            () -> {
                int rows = imageService.cleanupInactiveImages();
                Logger.info(String.format("%d inactive unused images cleaned from db", rows));

            },
            system.dispatcher()
    );
}

The log when I shutdown note the first line here :

[info] - application - 1 inactive unused images cleaned from db
[info] - application - Shutting down connection pool.
[info] - application - Creating Pool for datasource 'default'
...
[info] - application - Schedulers started
[info] - play.api.Play - Application started (Prod)

You can see it executed the Scheduler ignoring its original execution time, then got shutting down, and then started, and "Schedulers started" after.

What the problem, how I cancel the scheduler or prevent the play to run it before shutdown? is it a bug for Akka?

I'm calling startScheduler inside OnStartup like the answer of the following question :

java Playframework GlobalSettings deprecation for onStart

Edit: The following the minimal code to reproduce issue:

First Create OnStartup class:

@Singleton
public class OnStartup {

    @Inject
    public OnStartup(final ActorSystem system) {
        startScheduler(system);
    }

    private void startScheduler(final ActorSystem system) {
        startImagesCleanupScheduler(system);
        Logger.info("Schedulers started");
    }

    private void startImagesCleanupScheduler(ActorSystem system) {
        system.scheduler().schedule(
                Duration.create(0, TimeUnit.MILLISECONDS), //Initial delay
                Duration.create(1, TimeUnit.DAYS),     //Frequency 1 days
                () -> {
                    //int rows = imageService.cleanupInactiveImages();
                    rows = 1;
                    Logger.info(String.format("%d inactive unused images cleaned from db", rows ));
                },
                system.dispatcher()
        );
    }

}

Then create module:

public class OnStartupModule extends AbstractModule {
    @Override
    public void configure() {
        bind(OnStartup.class).asEagerSingleton();
    }
}

Finally enable module in application.conf :

play.modules.enabled += "modules.OnStartupModule"

原文:https://stackoverflow.com/questions/37771194
更新时间:2024-04-03 16:04

最满意答案

我遇到过同样的问题。 经过努力,我发现在之前的视图中我正在使用

[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"title-bar-bg.png"] forBarMetrics:UIBarMetricsDefault];

我改成了

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"title-bar-bg.png"] forBarMetrics:UIBarMetricsDefault];

问题解决了。


I had the same issue. After much effort I found out that in the previous view I was using

[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"title-bar-bg.png"] forBarMetrics:UIBarMetricsDefault];

I changed it to

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"title-bar-bg.png"] forBarMetrics:UIBarMetricsDefault];

and the problem was solved.

相关问答

更多
  • 这里的问题似乎是绑定最初计算为null ,因此不会触发评估和状态更改所需的更改通知。 我用以下子类修复了它: public class FixedDataStateBehavior: DataStateBehavior { protected override void OnAttached() { base.OnAttached(); AssociatedObject.Loaded += (sender, routedEventArgs) => ...
  • 我遇到过同样的问题。 经过努力,我发现在之前的视图中我正在使用 [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"title-bar-bg.png"] forBarMetrics:UIBarMetricsDefault]; 我改成了 [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"title-bar- ...
  • 当用户开始输入时,您需要keyup来触发验证: $('input').on('keyup', function() { $(this).validate(); }); $(document).ready(function() { $.validate({ "form": "#testForm" }); });