Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@Schedule annotation run every few minutes (or seconds)

Tags:

java

ejb

schedule

I would like to try to use the @Schedule annotation in the following way:

public class MyTestServlet extends HttpServlet {     private static JcanLogger LOG = JcanLoggerFactory.getLogger(ServiceTestServlet.class);      @EJB CronService cronService;      public void service(HttpServletRequest req, HttpServletResponse resp) throws .... {     ....     cronService.iLive();  } ---     @Local // because the ejb is in a servlet (there is no other jvm) public interface CronService {      public void iLive();     public void runsEveryMinute(); } --- @Singleton public class CronServiceBean implements CronService {     private static final JcanLogger LOG = JcanLoggerFactory.getLogger(CronServiceBean.class);      @Schedule(minute="*")     public void runsEveryMinute() {         LOG.info(" runs EveryMinute ");     }      public void iLive() {         LOG.info("iLive");      }  ---  LOG  ...   CronServiceBean:34  ] iLive 

Based on the log, the CronService live and well, but the scheduled task 'runsEveryMinute' doesnt work.

How should it work using an EJB scheduled task?

like image 319
cscsaba Avatar asked Jul 14 '12 10:07

cscsaba


People also ask

What is fixed delay in spring scheduler?

Schedule a Task at Fixed Delay In this case, the duration between the end of the last execution and the start of the next execution is fixed. The task always waits until the previous one is finished. This option should be used when it's mandatory that the previous execution is completed before running again.

How do I schedule a cron job in spring boot?

Java Cron ExpressionThe @EnableScheduling annotation is used to enable the scheduler for your application. This annotation should be added into the main Spring Boot application class file. The @Scheduled annotation is used to trigger the scheduler for a specific time period.

How do you schedule a task in Java?

One of the methods in the Timer class is the void schedule(Timertask task, Date time) method. This method schedules the specified task for execution at the specified time. If the time is in the past, it schedules the task for immediate execution.


2 Answers

As per the Javadoc for the @Schedule annotation, the default values are:

  • * for all fields except hour, minute, and second; and
  • 0 for hour, minute, and second, by default.

By specifying minute="*" and leaving hour at its default of 0, it requests that the timer run every minute after midnight for one hour (i.e., 00:00, 00:01, 00:02, ..., 00:59) and then not again until the next day. Instead, use:

@Schedule(hour="*", minute="*") 

To run every few seconds (e.g., 10 seconds), you can use a cron-like syntax:

@Schedule(hour = "*", minute = "*", second = "*/10", persistent = false) 

By default, the scheduler persists events. Setting persistent = false will prevent them from building up over time, if so desired.

like image 151
Brett Kail Avatar answered Oct 09 '22 02:10

Brett Kail


Please find the following details for the scheduler configuration.

(1) To run every 1 min

@Schedule(hour = "*", minute = "*/1", persistent = false) 

(2) To run every 5 mins

@Schedule(hour = "*", minute = "*/5", persistent = false) 

(3) To run every 30 seconds

@Schedule(hour = "*", minute = "*", second = "*/30", persistent = false) 

(4) To run every day at 6:00 am

@Schedule(hour = "6", minute = "0", second = "0", persistent = false) 

(5) To run on every Friday at 2:00 pm

@Schedule(dayOfWeek = "Fri", hour = "14", persistent = false) 

(6) To run on the first day of every month at 5:00 am

@Schedule(dayOfMonth="1", hour = "5", persistent = false) 

I hope this information will help you to configure the scheduler as per your requirement.

like image 38
Radadiya Nikunj Avatar answered Oct 09 '22 03:10

Radadiya Nikunj