Usage of Spring Scheduled Timer

Source: Internet
Author: User

Usage of Spring Scheduled Timer
Abstract: A Timer is often used in coding. It specifies to execute a code segment every one hour, or at every day. If java. util. Timer is used to do this kind of thing, there is no need to repeat the wheel. Fortunately, the timer is encapsulated in Spring, and it is very easy to use. It is configured in the form of annotations to execute a code segment at a certain moment. I used it once in my previous project. I will share the code and configuration with you. Key words: Spring, JAVA, Scheduled, Timer 1. First write a Handler interface ("timer Handler "), the processing logic of the class implementing this interface is driven by a timer. Copy code 1/* 2 * file name: ITimeFlickerHandler. java 3 * copyright: Wang Anqi 4 * Description: clock message processing interface 5 * modification time: 6 * modification content: add 7 */8 9 public interface ITimeFlickerHandler {10 11 void handle (); 12} copy code 2. Write the timer driver and set the code to be executed at a certain moment, to drive the "timer Handler ". Copy code 1/* 2 * file name: TimeFlicker. java 3 * copyright: Wang Anqi 4 * Description: clock trigger source 5 * modification time: 6 * added 7 */8 9 import java. util. arrayList; 10 import java. util. list; 11 12 import org. slf4j. logger; 13 import org. slf4j. loggerFactory; 14 import org. springframework. scheduling. annotation. scheduled; 15 16/** 17 * clock trigger source 18*19 * @ author wanganqi20 * 21 */22 public class TimeFlicker {23 24 private static final Logger logger = L OggerFactory25. getLogger (LocatingSourceFilter. class); 26 27 private List <ITimeFlickerHandler> handlers = new ArrayList <ITimeFlickerHandler> (); 28 29/** 30 * get the clock message processing class to be called through the configuration file 31*32 * @ return33 */34 public List <ITimeFlickerHandler> getHandlers () {35 return handlers; 36} 37 38 public void setHandlers (List <ITimeFlickerHandler> handlers) {39 this. handlers = handlers; 40} 41 42 // each hour 30 minutes and 00 seconds for processing 43 @ SC Heduled (cron = "0 30 ***? ") 44 public void run () {45 for (ITimeFlickerHandler handler: handlers) {46 handler. handle (); 47} 48} 49} copy code 3. Implement the "timer Handler" interface to implement the processing logic when the timer event is triggered. Copy code 1/* 2 * file name: Monitor. java 3 * copyright: Wang Anqi 4 * Description: Monitoring processing 5 * modification time: 2013-12-10 6 * modification content: Added 7 */8 9 import org. slf4j. logger; 10 import org. slf4j. loggerFactory; 11 12 public class Monitor implements ITimeFlickerHandler {13 14 private static final Logger logger = LoggerFactory15. getLogger (LocatingSourceFilter. class); 16 17/** 18 * the scheduled time is up, and all electronic barrier caches are updated 19*20 * @ see ITimeFlickerHandler # handle () 21 */22 @ Override23 pub Lic void handle () {24 EF [] res = null; 25 try {26 res = service. getEF (); 27} catch (Exception e) {28 logger. error ("This is error message"); 29} 30 EFB. setBuffer (res); 31} 32} copy Code 4. Configure Spring, inject "timer Handler", Spring configuration file bean. xml is as follows. Copy code 1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <beans xmlns = "http://www.springframework.org/schema/beans" 3 xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" 4 xmlns: task = "http://www.springframework.org/schema/task" 5 xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/task 6 http://www.springframework.org/schema/task/spring -Task-3.1.xsd "> 7 8 <! -- Processor related --> 9 <task: annotation-driven/> 10 <bean id = "Monitor" 11 class = "com. imp. monitor "/> 12 <bean id =" TimeFlicker "class =" com. imp. timeFlicker "> 13 <property name =" handlers "> 14 <list> 15 <ref bean =" Monitor "/> 16 </list> 17 </property> 18 </bean> 19 </beans> copy Code 5. Generate and obtain the Spring application context, and write test cases ....... Copy code 1 import org. springframework. context. support. abstractApplicationContext; 2 import org. springframework. context. support. fileSystemXmlApplicationContext; 3 4 public class AppContext {5 6 private static AbstractApplicationContext dispatchContext; 7 8/** 9 * obtain the Context 10*11 * @ return context 12 */13 public static AbstractApplicationContext getDispatchContext () {14 if (dispatchContext = null) in singleton Mode) 15 disp AtchContext = new FileSystemXmlApplicationContext (16 "classpath: bean. xml "); 17 return dispatchContext; 18} 19} The Code copy timer is concise and extremely useful, so far it has not found any shortcomings, I don't know if there is such a small tool in C # that can improve the efficiency of code writing. You can tell me that.

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.