Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9240385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:10:50+00:00 2026-06-18T08:10:50+00:00

I need to execute a piece of code 1 time everyday in playframework2.0.4 when

  • 0

I need to execute a piece of code 1 time everyday in playframework2.0.4 when I try to do with the class extends GlobalSettings it works. But it works for every instance requesting. I want it works when server starts and does its duty everyday 1 time.

package controllers;
import java.util.concurrent.TimeUnit;
import akka.util.Duration;
import play.Application;
import play.GlobalSettings;
import play.libs.Akka;

public class ParserJobApp extends GlobalSettings{
@Override
public void onStart(Application app) {
    Akka.system().scheduler().schedule(Duration.create(0, TimeUnit.MILLISECONDS),Duration.create(6, TimeUnit.SECONDS), new Runnable() { 
        @Override
        public void run() {
            System.out.println("AAA ---    "+System.currentTimeMillis());
        }
    });
}
}

And this is my controller where start the class above

public class Application extends Controller {

public static Result index() { 
  ParserJobApp pr=new ParserJobApp();
  pr.onStart(null);
  System.out.println("sfsdfsdf");
return ok(index.render("Your new "));

}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:10:52+00:00Added an answer on June 18, 2026 at 8:10 am

    Scheduler tasks should be placed only in Global class. Create two tasks, schedule only once first with initialDelay = 0 milliseconds.

    For the second task, you need to calculate seconds between current DateTime and next planned occurrence (ie. tomorrow at 8:00 o’clock) using common date/time classes, then set this difference as initialDelay and also set frequency to 24 hours.

    In result, it will start at the application start and will schedule the task for execution each day at required hour.

    Edit

    There’s complete sample, (save/edit the class: /app/Global.java):

    import akka.util.Duration;
    import org.joda.time.DateTime;
    import org.joda.time.Seconds;
    import play.Application;
    import play.GlobalSettings;
    import play.Logger;
    import play.libs.Akka;
    import java.util.concurrent.TimeUnit;
    
    public class Global extends GlobalSettings {
    
        @Override
        public void onStart(Application application) {
    
    
            Akka.system().scheduler().scheduleOnce(
                    Duration.create(0, TimeUnit.MILLISECONDS),
                    new Runnable() {
                        @Override
                        public void run() {
                            Logger.info("ON START ---    " + System.currentTimeMillis());
                        }
                    }
            );
    
            Akka.system().scheduler().schedule(
                    Duration.create(nextExecutionInSeconds(8, 0), TimeUnit.SECONDS),
                    Duration.create(24, TimeUnit.HOURS),
                    new Runnable() {
                        @Override
                        public void run() {
                            Logger.info("EVERY DAY AT 8:00 ---    " + System.currentTimeMillis());
                        }
                    }
            );
        }
    
        public static int nextExecutionInSeconds(int hour, int minute){
            return Seconds.secondsBetween(
                    new DateTime(),
                    nextExecution(hour, minute)
            ).getSeconds();
        }
    
        public static DateTime nextExecution(int hour, int minute){
            DateTime next = new DateTime()
                    .withHourOfDay(hour)
                    .withMinuteOfHour(minute)
                    .withSecondOfMinute(0)
                    .withMillisOfSecond(0);
    
            return (next.isBeforeNow())
                    ? next.plusHours(24)
                    : next;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to execute some piece of code before every JUnit test method. To
I have a piece of code that involves multiple inserts but need to execute
I need to execute a piece of code ONCE every first day of the
I need execute some code before Windows shutdown process each time. So, I want
I need to execute a piece of JavaScript code say, each 2000 milliseconds. setTimeout('moveItem()',2000)
I need to do something like this: Execute a piece of code Start to
I need to execute code at intervals, sometimes 10 seconds, sometimes 5 minutes. The
I have a piece of code that invokes an instance of the bash terminal
I need to test that a piece of code executes two sql statements, which
Please forgive my attempts at necromancy, but I actually need to write some code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.