Frage

I'm building functionality that will enable users to schedule recurring and non-recurring tasks. I need to save these schedules in the database and read active schedules once daily to see if they should be triggered today.

I am using spring/java. I fear if I write this logic myself it will be lacking and not as efficient as it could be.

Required schedule configurations are just like the Outlook calendar, except for being concerned with time. (probably easier to open outlook recurring meeting than read my rendition):

  • Actual Date
  • every Nth day, based on start date
  • every Nth business day, based on start date
  • every Nth week on one-to-many-selection(Sun,Mon,Tues,etc...), based on start date
  • the (1st,2nd,3rd,4th,last) (Sun,Mon,Tues,etc...) of every Nth month based on start date
  • day X of every y months, based on start date
  • every (jan,feb,mar,apr,etc..) Nth day each year
  • every (1st,2nd,3rd,4th,last) (Sun,Mon,Tues,etc...) of (jan,feb,mar,apr,etc..)


What is simple pattern for implementing this? Thanks for any help

War es hilfreich?

Lösung

Quartz Job Scheduler

What you are looking for can be accomplished easily with the Quartz Job Scheduler Library.

It allows for scheduling of future event, and even with using cron syntax.

Excerpt from project page:

What is Quartz Job Scheduling Library?

Quartz is a richly featured, open source job scheduling library that can be integrated within virtually any Java application - from the smallest stand-alone application to the largest e-commerce system. Quartz can be used to create simple or complex schedules for executing tens, hundreds, or even tens-of-thousands of jobs; jobs whose tasks are defined as standard Java components that may execute virtually anything you may program them to do. The Quartz Scheduler includes many enterprise-class features, such as support for JTA transactions and clustering.

Quartz is freely usable, licensed under the Apache 2.0 license.

See Overview.

See example # 3.

Andere Tipps

Spring

If you do decide to write your own logic and since you are using Spring, you could make use of the some of the functionality provided by Spring (spring-framework-reference)

But I never made use of that, since I face many challenges in getting exactly what I want

QuartzInitializerServlet

Another way would be to make use of the build-in QuartzInitializerServlet. This method is much more flexible in that you can get the scheduler instance and implement your logic from there

A Servlet that can be used to initialize Quartz, if configured as a load-on-startup servlet in a web application.

Using this start-up servlet may be preferred to using the QuartzInitializerListener in some situations - namely when you want to initialize more than one scheduler in the same application.

You'll want to add something like this to your WEB-INF/web.xml file:

 <servlet>
     <servlet-name>
         QuartzInitializer
     </servlet-name>
     <display-name>
         Quartz Initializer Servlet
     </display-name>
     <servlet-class>
         org.quartz.ee.servlet.QuartzInitializerServlet
     </servlet-class>
     <load-on-startup>
         1
     </load-on-startup>
     <init-param>
         <param-name>config-file</param-name>
         <param-value>/some/path/my_quartz.properties</param-value>
     </init-param>
     <init-param>
         <param-name>shutdown-on-unload</param-name>
         <param-value>true</param-value>
     </init-param>
     <init-param>
         <param-name>wait-on-shutdown</param-name>
         <param-value>true</param-value>
     </init-param>
     <init-param>
         <param-name>start-scheduler-on-load</param-name>
         <param-value>true</param-value>
     </init-param>
 </servlet>

A StdSchedulerFactory instance is stored into the ServletContext. You can gain access to the factory from a ServletContext instance like this:

 StdSchedulerFactory factory = (StdSchedulerFactory) ctx
            .getAttribute(QuartzFactoryServlet.QUARTZ_FACTORY_KEY);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top