Playframework + Akka:如何避免在关闭应用程序时激活执行的计划任务

时间:2016-06-12 06:01:23

标签: java playframework akka playframework-2.5

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

// 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)

你可以看到它执行了调度程序,忽略了它的原始执行时间,然后关闭了,然后启动了,“调度程序启动后”。

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

我在startScheduler内拨打OnStartup,就像以下问题的答案一样:

java Playframework GlobalSettings deprecation for onStart

修改 以下是重现问题的最小代码:

首先创建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"

1 个答案:

答案 0 :(得分:3)

对于评论来说这有点太长了,但我没有测试这种方法,你可能需要调整它。文档说,组件以与创建它们相反的顺序被销毁。这意味着您可能会在ActorSystem关闭之前取消您的调度程序,因为此类在它之后注册并依赖于它。在OnStartup课程中创建一个这样的关闭钩子,您将在该课程中取消您的日程安排。这意味着在ActorSystem关闭时,将没有执行计划:

@Singleton
public class OnStartup {

    private final Cancellable cancellableSchedule;

    @Inject
    public OnStartup(final ActorSystem system, final ApplicationLifecycle l) {
        cancellableSchedule = startScheduler(system);
        initStopHook(l);
    }

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

    private Cancellable startImagesCleanupScheduler(ActorSystem system) {
        return 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()
        );
    }

    private void initStopHook(ApplicationLifecycle lifecycle) {
       lifecycle.addStopHook(() -> {
            cancellableSchedule.cancel();
        return CompletableFuture.completedFuture(null);
        });
     }

}