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)