Use JDring to set schedule reminders similar to cron

Source: Internet
Author: User

 


The UNIX operating system uses the cron daemon to run scripts at specific times and dates. Java Development Kit (JDK) 1.3 java. util. Timer class allows developers to set to execute some tasks every N milliseconds, but does not have a cron-like structure to specify a specific time in a day or week.

This gap is now blocked by the JDring package. It is a Java schedule reminder program similar to cron written by Olivier Dedieu. The JDring.zip file contains the following parts:

Source code: This is not required, you can ignore it.
Javadoc document: put this in your docs directory.
Jar file: This file contains compiled classes. Put it in your classpath.
There are two steps to use JDring. The first step is to create an AlarmListener, which is an interface containing a method:

Void handleAlarm (AlarmEntry entry );

The AlarmEntry parameter provides the details of the Schedule reminder settings at what time. The following is a simple example of using AlarmListener:
Import com. jalios. jdring. AlarmEntry;
Import com. jalios. jdring. AlarmListener;

Public class Buzzing implements AlarmListener {

Private String buzz;

Public Buzzing (String buzz ){
This. buzz = buzz;
}

Public void handleAlarm (AlarmEntry entry ){
System. err. println ("dropped ...... ");
System. err. println (buzz );
}

}
The second step of using JDring is to notify a central manager of when AlarmListener should ring. This manager is an instance of AlarmManager and has an empty constructor. Calling schedule reminders at specific times is similar to cron, as shown below:
AlarmManager. addAlarm (minute, hour, day of month, month, day of week, year,
AlarmListener)
The following sample code demonstrates a calendar reminder that is set to start logging in 20th minutes every hour:
Import com. jalios. jdring. AlarmManager;
Import com. jalios. jdring. PastDateException;

Public class SetAlarm {

Static public void main (String [] args ){
AlarmManager mgr = new AlarmManager ();
Mgr. addAlarm (20,-1,-1,-1,-1,-1, new Buzzing ());
}

}
This example demonstrates how to set a reminder at five o'clock P.M. every Friday:
Manager. addAlarm (00, 17,-1,-1, Calendar. FRIDAY,-1, new Buzzing ());
// Java. util. Calendar
JDring can also be used to remember an anniversary, as shown below:
Manager. addAlarm (00, 12, 20, Calendar. MARCH,-1,-1,
New Buzzing ("remember tomorrow is the anniversary! "));

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.