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

Asked

Viewed 132 times

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.

Problema compilação


(Edit: I’m running on a common Tomcat)

  • 1

    Yes, you need one main to run something from the command line. It is the entry point. You also need a method (be it main or any other) to your code, as you noticed by build errors it is not possible to run statements as today.set... directly in the class body.

2 answers

2

The most standard way to do this is to use timers of Javaee.

In your example, you could do so:

@Singleton
public class ScheduleBases {

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

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

Thus the "scheduledTask" method would run at 02:30 am, and after that, every 24 hours again.

  • Thank you very much for the Reply, unfortunately I am at the end of business hours and I can only test tomorrow morning.

  • I would just like to point out that I am using common Tomcat, not tomcateEE, not glassfish, I don’t know if I have support for this type of Schedule, but I will test

  • really, by using the most common Tomcat I don’t have access to these tools, I’ll see if I can find another way... Thank you!

1


I arrived at 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 14:31 exactly.

  • 1

    Out of curiosity, instead of WebListener why not an application stand-alone and an entry into the cron (*Nix) or control schedtasks (Windows) I understand the advantages of centralizing the entire code in a single project, but from the point of view of modularity, robustness and resource utilization it is worth separating things.

  • @Anthonyaccioly Hi, I had initially thought about making a stand-alone app for this task, but it turns out I work in a company that loves for barriers whenever you want to adopt any "new" technology or communication between various apps. As I have a surrealist Danish to fulfill, I went in the easiest, which is to integrate everything. Having said that, in the future when greater performance is needed, I believe I will have to use an app stand alone, in fact!

  • Hello @user94991. I understand the situation. Feel free to accept your own answer. This is a good indication for users who arrive at the site with a similar problem.

  • can leave @Anthonyaccioly , but the site only allows me to do this tomorrow, tomorrow mark the answer as correct, thank you!

Browser other questions tagged

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