How to run a java program daily at a given time automatically

Asked

Viewed 3,550 times

1

Hello, I need to run a program in java daily at a certain time, I found some things about timer.Schedule, but I would like to see some examples, thanks!

  • 7

    This question has already been answered here on stack, http://answall.com/questions/67086/executar-m%C3%A9all-java-of-time-in-time-run-periodically/67963#67963

  • This will depend on whether your program runs on desktop or web.

  • @Shura16 why it depends?

  • Emir Marques' answer shows well. The operation of the technologies is a little different.

  • who marked as duplicate know q the response posted in the other session uses a bat and scheduled windows tasks, nut solution!

1 answer

4

Web application:

 public  class EnviaEmail implements Job {
      public void execute(JobExecutionContext context) {
        System.out.println("enviando email para evisar mudanca de senha...");
        // acessar api de e-mail aqui
      }   
    }

If we want this procedure to be performed once a day, at midnight, we use the Quartz API:

SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sched = sf.getScheduler();

JobDetail job = new JobDetail("dispara_email", "grupo", EnviaEmail.class);
Trigger aMeiaNoite = TriggerUtils.makeDailyTrigger(0, 0);
sched.scheduleJob(job, aMeiaNoite);

sched.start();

We could also have used expressions in cron format, using Crontrigger, such as triggering Job every 20 seconds:

CronTrigger trigger = 
   new CronTrigger("20_segundos", "grupo", "0/20 * * * * ?");

The Enviaemail class will then be instantiated at midnight, and will have its method run(Jobexecutioncontext context) invoked.

There are also other alternatives to Quartz itself for task scheduling. One of them is to subvert Hudson, using it not only as a continuous integration server, but also to manage its tasks, taking advantage of its features, fault log management and UI. Or even use cron directly by triggering requests through Curl or similar. In the Google App Engine, for example, there is an own cron service that works in a similar way, triggering requests to a certain URL at the scheduled time.

Source: http://blog.caelum.com.br/agendamento-de-tarefas-em-aplicacoes-web-um-truque-com-quartz/

Desktop application:

In the case of desktop application you can use the OS task scheduler itself to run a jar file at a given time. To do so you can create a file . bat put a code similar to the below:

javaw -Xmx200m -jar C:\Path\to\jarfile\TheJar.jar

Note that there is a path that points to the directory of the jar. Then this bat can be registered in the task scheduler.

To be registered, in Windows for example, follow the steps below:

Click Start > All Programs > Accessories > System Tools > Scheduled Tasks

  1. Double-click Add Scheduled Task to start Wizard from addition of tasks, and then click next on the first box of dialogue.
  2. The next dialog box shows the list of programs that are installed on your computer, both those that are part of the Windows XP or some program that has been installed.
  3. Use one of these procedures:

    If the program you want to run is listed, click the program and then click next. If you want to run a program, a script, a document or even a file. bat that is not listed, click in Open, then click the directory and the file you want schedule.

  4. Type the task name and then choose one of the following options:

    Daily Weekly Monthly Only Once When the computer start (before user log in) When I log in

  5. Click next, specify the information about the day and time that you want the task to run and then click next.
  6. Enter the name and password of the user who is associated with the task. It is I need to select a user who has permission to run that task. By default Wizard selects the user name currently logged in.
  7. Click Next and make sure all settings are as desired.
  • I’ll try the second option since my application is desktop. thanks let’s see if it’s right ^^

  • The second solution is not the most appropriate, since it is a large application, just for the record I used Quartz.

  • Legal, particularly also precise the use of QUARTZ

  • It is pretty cool the Quartz, now I’m facing other problems, time to generate the runnable jar as I use sql server I’m having problems with the dependencies of Maven.

  • Pay attention to your pom.xml if dependencies are under the Scope flag. Search on google and if you need a tap

  • thanks Emir Marques ta solved, jdbc sql server does not have in public repository, I managed here downloading the dependency and creating local repository

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.