[play 2.5.1] Cron tasks

1,142 views
Skip to first unread message

Łukasz Umbras-Nichnerowicz

unread,
Mar 30, 2016, 9:18:38 AM3/30/16
to play-framework

I'm in need to use cron sheduled taks in my play application. In play 2.3 I've used akka-quartz-scheduler and akka actors for this reason. In play 2.4 / 2.5 dependency injection have been added and static acces to some controllers and application configuration have beed discarded. I've no idea how to implement corn sheduled tasks that uses play application configuration in play 2.5. Could anybody help me with this issue?

Julian Modesto

unread,
Mar 30, 2016, 3:28:34 PM3/30/16
to play-framework
I'd love a Play task scheduling component, like how Spring offers a job management component, that offers both an API in Scala and Java.

I don't want to resort to writing my own cron scheduler implementation with Akka, Camel and Quartz in Java. By the Single Responsibility Principle, I'd be using my plain old shell's crontab, but I'd prefer a scheduler in the application layer.

I guess this is my Play feature request.

Will Sargent

unread,
Mar 30, 2016, 4:57:53 PM3/30/16
to play-fr...@googlegroups.com
Hi Julian,

This isn't Play related -- it's Akka on the backend, so it'd be an Akka feature request to have a quartz like scheduler in Java.

Will.

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/8f764b6f-4dbb-4ee0-8667-9f8a0464ed85%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Andy Czerwonka

unread,
Mar 30, 2016, 11:30:54 PM3/30/16
to play-framework
Message has been deleted

Łukasz Umbras-Nichnerowicz

unread,
Mar 31, 2016, 1:46:07 AM3/31/16
to play-framework
I've came to solution using https://github.com/enragedginger/akka-quartz-scheduler with full support of Guice dependency injection in Play 2.5. See it below:

Module.java:

public class Module extends AbstractModule implements AkkaGuiceSupport {

    @Override
    public void configure() {
        // Use the system clock as the default implementation of Clock
        bind(Clock.class).toInstance(Clock.systemDefaultZone());
      
        bindActor(SendNotificationActor.class, "send-notification-actor");
        bind(Scheduler.class).to(SchedulerImpl.class).asEagerSingleton();
    }

}

Scheduler interface:

public interface Scheduler {

}

Scheduler implementation class:

@Singleton
public class SchedulerImpl implements Scheduler {

    /** */
    private final ApplicationLifecycle appLifecycle;
  
    /** */
    private final ActorSystem actorSystem;

    @Inject
    public SchedulerImpl(ActorSystem actorSystemInj, ApplicationLifecycle appLifecycleInj, @Named("send-notification-actor") ActorRef sendNotificationActor) {
      
        Logger.debug("Starting SchedulerImpl");
      
        actorSystem = actorSystemInj;
        appLifecycle = appLifecycleInj;
      
        QuartzSchedulerExtension scheduler = (QuartzSchedulerExtension) QuartzSchedulerExtension.get(actorSystem);
      
        scheduler.schedule("SendNotification", sendNotificationActor, "");
      
        appLifecycle.addStopHook(() -> {
          
            Logger.debug("Shutting down: SchedulerImpl");
          
            scheduler.shutdown(true);
            return CompletableFuture.completedFuture(null);
        });
    }

}

Actor cron configuration:

akka {
  threadPool {
        threadCount = 3
        threadPriority = 5
        daemonThreads = true
    }
  quartz {
    schedules {
        
      SendNotification {
        description = "Send notification"
        expression = "0 * * ? * *"

Julian Modesto

unread,
Mar 31, 2016, 2:30:07 PM3/31/16
to play-framework
Thanks Lukas, this is what I should've done, super helpful.

Mario

unread,
Oct 28, 2016, 4:03:20 PM10/28/16
to Play Framework
Hello,
Thank you for your solution. Work fine.
But the problems begin when a try to use jpa.

Here is an exemple of SendNotificationAktor :

public class SendNotificationAktor extends UntypedActor {
    @Inject
    JPAApi jpaApi;
   
    @Override
    
     public void onReceive(Object message) throws Throwable {
        
                jpaApi.withTransaction("default",false,()->{
… some calls…
        });
    
      }

}


jpaApi.withTransaction throw : 

 akka.actor.OneForOneStrategy - No EntityManager bound to this thread. Try wrapping this call in JPAApi.withTransaction, or ensure that the HTTP context is setup on this thread.
java.lang.RuntimeException: No EntityManager bound to this thread. Try wrapping this call in JPAApi.withTransaction, or ensure that the HTTP context is setup on this thread.

Any solutions?

Thanks!
Reply all
Reply to author
Forward
0 new messages