Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Java Scheduling OR Quartz

DZone's Guide to

Java Scheduling OR Quartz

· Java Zone
Free Resource

Learn how to troubleshoot and diagnose some of the most common performance issues in Java today. Brought to you in partnership with AppDynamics.

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 {

        super.init(config);
        ScheduleMain ScheduleTest = new ScheduleMain();
          System.out.println("Inialize Scheduling ...");
        ScheduleTest.mainRun();
          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 ...");]


task.ExecuteEmailProcedure();]


m_log.info("Email Done");]


 // Display current time]


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


task.ExecuteSmsProcedure();]


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);
    if(id.equalsIgnoreCase("1")){
      m_log.info( "Email Polling Complete At : " + sdf.format( new Date() ) );
    }else{
      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

Understand the needs and benefits around implementing the right monitoring solution for a growing containerized market. Brought to you in partnership with AppDynamics.

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}