Do I really need to start from a main to run a schedule in java?

1

I'm developing a web application, and now I've come to a point where I'll have a process running in parallel in my application (in the background) every day at an x time. To test the process, I did a class inside the project with a main, and circled it separately, cool everything worked as I expected.

But when I tried to leave the code working normally, deleting the main and leaving only one class, I had the following error: "Syntax Error, ConstructorHeaderName Expected instead."

Schedulates the process.

package backgroundProcesses;

import java.sql.*;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import com.microsoft.sqlserver.jdbc.*;

public class ScheduleBases {

    public static void main(String[] args) {

        Calendar today = Calendar.getInstance();
        today.set(Calendar.HOUR_OF_DAY, 17);
        today.set(Calendar.MINUTE, 24);
        today.set(Calendar.SECOND, 0);


          // creating timer task, timer
        Timer timer = new Timer();
        tarefaDiaria td = new tarefaDiaria();

          // scheduling the task at fixed rate delay
          timer.scheduleAtFixedRate(td,today.getTime(), TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS));      
       }
}

Action that will run at the specified time

package backgroundProcesses;

import java.util.TimerTask;

public class tarefaDiaria extends TimerTask  {

    @Override
    public void run() {
        System.out.println("OPA FUNCINOU!");

    }

}

Moral of the story, I need to "turn on" my server and run my web application, this process is activated, and run at the time I specified ... I tried to run leaving main, but it does not run on time , and when I take out main I get the error.

(Edit: I'm running on a regular Tomcat)

    
asked by anonymous 28.03.2018 / 22:34

2 answers

1

I came up with the solution using a webListener:

@WebListener
public class ScheduleBases implements ServletContextListener {


    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        System.out.println("entrei no schedule valendo!");
        Calendar today = Calendar.getInstance();
        today.set(Calendar.HOUR_OF_DAY, 14);
        today.set(Calendar.MINUTE, 31);
        today.set(Calendar.SECOND, 0);

        // creating timer task, timer
        Timer timer = new Timer();
        tarefaDiaria td = new tarefaDiaria();

        // scheduling the task at fixed rate delay
        timer.scheduleAtFixedRate(td,today.getTime(), TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS));
    }



    @Override
    public void contextDestroyed(ServletContextEvent arg0) {




    }        

}

My thread started at exactly 14:31.

    
29.03.2018 / 19:34
2

The most standard way to do this is to use JavaEE timers .

In your example, you could do this:

@Singleton
public class ScheduleBases {

@Schedule(hour="02/24", minute="30")
public void scheduledTask() {

    System.out.println("OPA FUNCINOU!");
}

So the "scheduledTask" method would run at 02:30 in the morning, and after that, every 24 hours again.

    
28.03.2018 / 22:57