1
I am developing a web application, and now I have reached a point where I will have a process running in parallel in my application(in background) every day at a time x. To test the process, I made a class within the project with a main, and I ran it separately, Everything worked out as I expected.
But when I went to try to keep the code running normally, deleting the main and leaving only one class, I had the following error "Syntax Error, Constructorheadername Expected Instead."
Schedula 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 that when I "turn on" my server and run my web application, this process is activated, and run at the time I specified... I tried to rotate leaving the main, but it does not rotate on time, and when taking the main got the error.
(Edit: I’m running on a common Tomcat)
Yes, you need one
main
to run something from the command line. It is the entry point. You also need a method (be itmain
or any other) to your code, as you noticed by build errors it is not possible to runstatements
astoday.set...
directly in the class body.– Anthony Accioly