Over a million developers have joined DZone.

Java Scheduling OR Quartz

DZone's Guide to

Java Scheduling OR Quartz

· Java Zone ·
Free Resource

Secure your Java app or API service quickly and easily with Okta's user authentication and authorization libraries. Developer accounts are free forever. Try Okta Instead.

A strange thing happened while completing a communications project: we stumbled across simple Java scheduling.

Comparing apples to apples I found simple Java scheduling to be much more scalable, efficient, and simpler than the industry standard Quartz scheduling.

Its ease of use is astounding!

Minimum Jars required, multiple execution, setting up is as easy as using J2EE and JAVA SE standards. No need to use Nexus for Jar storage and the amazing thing is external properties can be used for Scheduling.

Here's how its' done:

1. Set up and load a Servlet in the web.xml – this is used as a trigger.

public class EmailTimerServlet extends HttpServlet {

    public void init(ServletConfig config) throws ServletException {

        ScheduleMain ScheduleTest = new ScheduleMain();
          System.out.println("Inialize Scheduling ...");
          System.out.println("Running The Main Method for Scheduling ...");

2. Create a Main Class TimerSchedular Class – Used to process tasks and all Scheduling timer properties.

public class ScheduleMain {]

 private static Log m_log = LogFactory.getLog(ScheduleMain.class);]

 private final static long fONCE_PER_DAY = 1000*60*60*24;]

 private final static long TEST_PER_DAY = 1000*60*10;]

 private final static int fONE_DAY = 1;]

 private final static int fFOUR_AM = 6;]

 private final static int fZERO_MINUTES = 0;]

 static Timer time = new Timer();]

 public static void mainRun() {]

ScheduledTask st = new ScheduledTask(); // Instantiate SheduledTask class ;]

time.scheduleAtFixedRate(st, new Date(), TEST_PER_DAY);
] m_log.info("Schedule Began @ "+new Date()+" For Every "+TEST_PER_DAY+" minutes :");] }] private static Date getTomorrowMorning4am(){] Calendar tomorrow = new GregorianCalendar();] tomorrow.add(Calendar.DATE, fONE_DAY);]  Calendar result = new GregorianCalendar(tomorrow.get(Calendar.YEAR),] tomorrow.get(Calendar.MONTH),] tomorrow.get(Calendar.DATE),] fFOUR_AM,] fZERO_MINUTES);]  return result.getTime();]  }] }] <

3. Develop your Tasks – This is the integration point to your backend processing, just instantiate and call().

public class ScheduledTask extends TimerTask {]

  private static Log m_log = LogFactory.getLog(ScheduledTask.class);]

// Add your task here]

public void run() {]

Date now = new Date(); ]

Task task = new Task();]

PropertiesMan prop = new PropertiesMan();]

m_log.info("Scheduled Procedures Begin ..." );]

// Display current time]

m_log.info("Email Procedure Begin ...");]


m_log.info("Email Done");]

 // Display current time]

m_log.info("SMS Procedure Begin ...");]


m_log.info("SMS Done");]



public class Task {
  private Log m_log = LogFactory.getLog(Task.class);
  FolderManager folderMan = new FolderManager();
  EmailProcess email    = new EmailProcess();
  SimpleDateFormat sdf = new SimpleDateFormat();
  public void ExecuteEmailProcedure(){

    File[] listOfEmails = folderMan.ProcessEmailPendingFolder();
    String id = email.BulkEmailExecution(listOfEmails);
      m_log.info( "Email Polling Complete At : " + sdf.format( new Date() ) );
      m_log.error( "Email Polling Expired at " + sdf.format( new Date() ) );

And that's it! Don't forget to add the Servlet into your Web.xml

Secure your Java app or API service quickly and easily with Okta's user authentication and authorization libraries. Developer accounts are free forever. Try Okta Instead.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}