2012-11-20 24 views
13

नाटक के 2.0.x डॉक में आप देख सकते हैं कि अतुल्यकालिक कार्य शेड्यूल करने के लिए:खेलने ढांचा 2.1 - शेड्यूलिंग async कार्य

http://www.playframework.org/documentation/2.0.4/ScalaAkka

Akka.system.scheduler.schedule(0 seconds, 30 minutes, testActor, "tick") 

हाल ही में कब releades withthe खेलते हैं 2.1 आप भी वही करता है सकते हैं? ??

पूरे अक्का एपीआई बदल दिया है लगता है ...

मैं देख लिया है:

https://github.com/playframework/Play20/wiki/Highlights https://github.com/playframework/Play20/wiki/Migration और भी http://doc.akka.io/docs/akka/2.1.0-RC1/project/migration-guide-2.0.x-2.1.x.html

भी यहाँ से पूछा: https://groups.google.com/d/topic/play-framework/7VcwNea6QlM/discussion

उत्तर

20

sample code औरका उपयोग करना 10 मैंने तेज़ परीक्षण किया, मेरे लिए काम करता है।

2.0.4 और 2.1RC1 के बीच कोड की तुलना मैं वहाँ देखने रहे सकते हैं अनुसूचक के मामले में केवल दो बदलाव:

  1. प्रतिस्थापित आयात

    // import akka.util.duration._ 
    import scala.concurrent.duration._ 
    
  2. जोड़ा आयात:

    import play.api.libs.concurrent.Execution.Implicits._ 
    

app/controllers/Application.scala

package controllers 

import play.api._ 
import play.api.mvc._ 
import play.libs.Akka 

import akka.actor._ 
import scala.concurrent.duration._ 
import play.api.libs.concurrent.Execution.Implicits._ 

object Application extends Controller { 

    def index = Action { 

    // say hello 
    Logger.info("hello, index action started") 

    val Tick = "tick" 
    val Tack = "tack" 

    val tickActor = Akka.system.actorOf(Props(new Actor { 
     def receive = { 
     case Tick => Logger.info("that still ticks!") 
     case Tack => Logger.warn("... 7 seconds after start, only once") 
     } 
    })) 

    // Repeat every 5 seconds, start 5 seconds after start 
    Akka.system.scheduler.schedule(
     5 seconds, 
     5 seconds, 
     tickActor, 
     Tick 
    ) 

    // do only once, 7 seconds after start 
    Akka.system.scheduler.scheduleOnce(7 seconds, tickActor, Tack) 

    Ok(views.html.index("Your new application is ready.")) 
    } 

} 

संपादित

नोटा लाभ, जैसा कि मैंने समूह पर जुलिएन की पोस्ट से देख सकते हैं, कि आयात करने के लिए defaultContext केवल काफी है:

import play.api.libs.concurrent.Execution.Implicits.defaultContext 
9

biesior's answer महान है

हालांकि, आप पर ग्राम नहीं हैं यदि आप नहीं चाहते हैं तो एक अभिनेता के माध्यम से।

Akka.system.scheduler.schedule(5 minutes, 5 minutes, new Runnable { 
    def run() { 
    Logger.info("that still ticks!") 
    } 
}) 
Akka.system.scheduler.scheduleOnce(7 minutes, new Runnable { 
    def run() { 
    Logger.warn("... 7 seconds after start, only once") 
    } 
}) 
2

उदाहरण के लिए जावा में एक कार्य हर शनिवार को चलाने पर 15 AM:

DateTime now = new DateTime(); 

DateTime plannedStart = new DateTime() 
    .withDayOfWeek(DateTimeConstants.SATURDAY) 
    .withHourOfDay(15) 
    .withMinuteOfHour(0) 
    .withSecondOfMinute(0) 
    .withMillisOfSecond(0); 

DateTime nextRun = (now.isAfter(plannedStart)) 
    ? plannedStart.plusDays(7) 
    : plannedStart; 

Long nextRunInSeconds = (long) secondsBetween(now, nextRun).getSeconds(); 

Akka.system().scheduler().schedule(
    Duration.create(nextRunInSeconds, TimeUnit.SECONDS), 
    Duration.create(7, TimeUnit.DAYS) , 
    new Runnable() { 
     public void run() { 
      Logger.info("-------------------------scheduler start : " + new DateTime()); 
     } 
    }, 
    Akka.system().dispatcher() 
); 
यहाँ एक ही जवाब का उपयोग कर अच्छे पुराने java.lang.Runnable है