JAVA Web implementation can control timers

Source: Internet
Author: User

The majority of Java Web requests are made using the Configure listener implementation in XML!

Here I wrote a timer can also meet the same requirements! And you can control the start or shut down, and can be set within 1 days to execute the timer at 6,9,12 Point, feeling

Listen to the flexible!

What kind?



The background code is as follows:

/** * * * * * @author Qiyulin * * * */@Controller () @Scope ("prototype") public class MapAction extends genericaction{ Private static final Long Serialversionuid = -8570260939683969514l;private static MyTimer mt;//Timer Data synchronization private String msg; Public String Timer () {String on = Request (). GetParameter ("on"); String times = Request (). GetParameter ("Times"), if (times) times = "1", if (!empty (ON) && "yes". Equals (ON)) {//Start timer MT =new MyTimer (times); Mt.start (new Mytimercallback () {public void finshed (int status) {switch (status) {Case 1: SYSTEM.ERR.PRINTLN ("Timer: triggered"); Break;case 0:system.err.println ("Timer: not triggered"); break;}}); Msg= "Timer has been started";} else if (!empty (ON) && "No", Equals (ON)) {//Turn off Timer if (mt!=null) {mt.end (new Mytimercallback () {public void finshed ( int status) {//do somethingSystem.out.println ("timer: Closed");}}); Msg= "timer has been closed";} Else{msg= "Welcome to Timer";} return appconstants.return_type_freemarker;} Null to determine Private Boolean empty (Object obj) {if (null!=obj&&! "". Equals (obj)) {return false;} Return true;} Inner class Mytimer{private string hours;private timer timer = new Timer (true);p ublic MyTimer (string hours) {this.hours = h Ours;} public void Start (final Mytimercallback callback) {Timer.schedule (new TimerTask () {@SuppressWarnings ("deprecation") @ overridepublic void Run () {Calendar cal = Calendar.getinstance (); String hs[] = Hours.split (","); int hour= cal.gettime (). GetHours (); Boolean b=arrays.aslist (HS). Contains (string.valueof (hour)); if (b) {callback.finshed (1);} else{callback.finshed (0);}}, 0,10*1000);} public void End (Mytimercallback callback) {timer.cancel (); callback.finshed (2);}} Callback Interface mytimercallback{void finshed (int status);} Public String getmsg () {return msg;} public void Setmsg (String msg) {this.msg = msg;}}


The page code is as follows:


Feel very good, haha, work ....

Original: Http://blog.csdn.net/qilin001cs

JAVA Web implementation can control timers

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.