Spring Timer Simple to use

Source: Internet
Author: User

Move from spring timed task

Spring Scheduled task is very powerful, the last simple application, to share the next, I hope we have a lot of communication!

This is a timed print time console, which is a simple scheduled task!

Please look at the program's Running source code:

First create a new class: Tellingthetimejob This class is inherited from Spring rewrite executeinternal this method.

package jobs;    import org.quartz.jobexecutioncontext;  import  org.quartz.jobexecutionexception;  import org.springframework.scheduling.quartz.quartzjobbean;   import service. itellingthetimeservice;      /**  *  @ProjectName: Chime demo      *  @ClassName:tellingthetimejob     *  @Description:      *  @author:sheep  *  @date:2012-4-19  pm 03:58:11      *  @Modifier:   *  @Modify  date:    *   @Modify  Note:     *  @version   */  public  class tellingthetimejob extends quartzjobbean {             private itellingthetimeservice tellingthetimeservice = null;         @Override       protected  void executeinternal (jobexecutioncontext arg0)  throws jobexecutionexception {           //Call Chime Method            this.tellingthetimeservice.tellingthetime ();      }         public itellingthetimeservice gettellingthetimeservice ()  {          return tellingTheTimeService;       }        public void  Settellingthetimeservice (               Itellingthetimeservice tellingthetimeservice)  {           this.tellingthetimeservice = tellingthetimeservice;      }  }   Establish an interface Itellingthetimeservice, function in order to implement timed call [java] view plaincopypackage service;       /**  *  @ProjectName: Chime demo   * @ classname:itellingthetimeservice     *  @Description:      *  @author:sheep  *  @date:2012-4-19  pm 03:59:37     *   @Modifier:   *  @Modify  Date:    *  @Modify  note:      *  @version   */  public interface  itellingthetimeservice {            /**       *  @Title:  tellingthetime       *   @Description:  Current Time        &Nbsp;    *  @throws       */       void tellingthetime ();   }

Establish an interface Itellingthetimeservice function in order to implement a timed call

package service;      /**  *  @ProjectName: Chime demo    *  @ClassName:itellingthetimeservice     *  @Description:      *  @author:sheep  *  @date:2012-4-19  pm 03:59:37      *  @Modifier:   *  @Modify  Date:    *  @Modify  Note:     *  @version   */  public interface  itellingthetimeservice {            /**       *  @Title:  tellingthetime       *   @Description:  Current Time             * @ Throws      */      void tellingthetime ();   }

Implementation class

Package service.impl;    import java.text.simpledateformat;  import  java.util.calendar;  import java.util.date;    import service. itellingthetimeservice;      /**  *  @ProjectName: Chime demo   *  @ClassName:tellingthetimeserviceimpl     *  @Description:      *  @author:sheep  *  @date:2012-4-19  pm 03:59:06      *  @Modifier:   *  @Modify  date:    * @ modify note:     *  @version   */  public class  TellingTheTimeServiceImpl implements ITellingTheTimeService {         /** @Description:  Chime       *  @see  service. Itellingthetimeservice#tellingthetime ()       */      public void  Tellingthetime ()  {          // TODO  auto-generated method stub          calendar  Now = calendar.getinstance ();           System.out.println ("Now is Beijing Time:" +this.format (Now.gettime ()));      }         public string format (date date)  {           simpledateformat format = new simpledateformat (" Yyyy-mm-dd hh:mm:ss ");          return  Format.format (date);       }  }

applicationcontext  configuration file

<?xml version= "1.0"  encoding= "UTF-8"? >    <beans xmlns= "http// Www.springframework.org/schema/beans "      xmlns:xsi=" http://www.w3.org/2001/ Xmlschema-instance "      xmlns:context=" http://www.springframework.org/schema/ Context "      xmlns:tx=" Http://www.springframework.org/schema/tx "       xsi:schemalocation= "http://www.springframework.org/schema/beans http:// www.springframework.org/schema/beans/spring-beans-2.5.xsd                   http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-2.5.xsd                   http://www.springframework.org/schema/ Tx http://www.springframework.org/scHema/tx/spring-tx-2.5.xsd ">        <bean id=" Tellingthetimeservice " class=" Service.impl.TellingTheTimeServiceImpl "/>             <!--  Configure a job -->       <bean id= "Tellthetimejob"  class= "Org.springframework.scheduling.quartz.JobDetailBean" >           <property name= "JobClass"  value= "jobs. Tellingthetimejob "/>          <property name=" Jobdataasmap ">              <map >                   <entry key= "Tellingthetimeservice"  value-ref= "Tellingthetimeservice" ></entry>               </map>           </property>      </bean>             <!--  Simple flip-flop  -->      <bean  id= "Simpletellthetimetrigger"  class= "Org.springframework.scheduling.quartz.SimpleTriggerBean" >           <property name= "JobDetail" >               <ref bean= "TellTheTimeJob"  />          </property>           <!--  in milliseconds, one-minute trigger after startup  -->           <property name= "Startdelay" >               <value>60000</value>           </property>          <!--  triggers a  --every minute of the interval >          <property name= "RepeatInterval" >               <value>60000</ value>          </property>       </bean>        <!--  Complex triggers  -->       <bean id= "Complextellthetimetrigger"  class= " Org.springframework.scheduling.quartz.CronTriggerBean ">           <property name= "Jobdetail" >               <ref bean= "TeLlthetimejob "/>          </property>           <property name= "Cronexpression" >               <!--  Here is the focus, you can customize the expression to implement timed triggering. The following meanings are triggered once per minute  -->               <value>0 0/1 * * * ?</value>           </property>      </bean>             <!-- spring Trigger Factory  -->       <bean class= "Org.springframework.scheduling.quartz.SchedulerFactoryBean" >           <property name= "Triggers" >               <list>                   <ref bean= "Complextellthetimetrigger"/>                   <!--&nbsp, ..... You can continue to add additional triggers below  -->               </list>          </property>       </bean>  </beans>

Web. XML

<?xml version= "1.0"  encoding= "UTF-8"? >  <web-app version= "2.5"         xmlns= "Http://java.sun.com/xml/ns/javaee"         xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"        xsi: Schemalocation= "http://java.sun.com/xml/ns/javaee       http://java.sun.com /xml/ns/javaee/web-app_2_5.xsd ">          <!--  Configuring the Spring xml Context path  -->    <context-param>       <param-name>contextConfigLocation</param-name>      < Param-value>classpath:applicationcontext*.xml</param-value>    </context-param >              <listener>       <listener-class>org.springframework.web.context.contextloaderlistener</listener-class>     </listener>      </web-app>

This allows you to implement spring timed tasks, the referenced jar is: Commons-collections.jar,commons-logging-1.0.4.jar, Log4j-1.2.15.jar,quartz-1.6.1-rc1.jar and Spring.jar (2.5).


Summarize

Main steps:

First step: Create a new class and integrate Quartzjobbean and override the Executeinternal method ()

Step Two: Configure the Applicationcontent.xml file

(1) <!--  Configure a job -->      <bean id= "Tellthetimejob"   class= "Org.springframework.scheduling.quartz.JobDetailBean" >           <property name= "Jobclass"  value= "jobs. Tellingthetimejob "/>          <property name=" Jobdataasmap ">              <map >                   <entry key= "Tellingthetimeservice"  value-ref= "Tellingthetimeservice" ></entry>               </map>           </property>      </bean >   (2)  <!--  Complex trigger  -->  &nbsP;   <bean id= "Complextellthetimetrigger"  class= " Org.springframework.scheduling.quartz.CronTriggerBean ">           <property name= "Jobdetail" >               <ref bean= "Tellthetimejob"/>           </property>          <property  name= "Cronexpression" >               <!--  Here is the focus, you can customize the expression to implement timed triggering. The following meanings are triggered once per minute  -->               <value>0 0/1 * * * ?</value>           </property>      </bean>   (3) <!--  spring Trigger Factory  --&Gt;      <bean class= " Org.springframework.scheduling.quartz.SchedulerFactoryBean ">           <property name= "Triggers" >               <list>                   <ref bean= "Complextellthetimetrigger"/>                   <!--&nbsp, ..... You can continue to add additional triggers below  -->               </list>          </property>       </bean>

Step Three: Configure the spring context in XML

<!--Configure the spring XML context path--<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext*.xml</param-value> </context-param> <l Istener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> & Lt;/listener>




Spring Timer Simple to use

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.