SMS message Notification Service-spring Scheduled task Quartz (i)

Source: Internet
Author: User

Integrated SMS message and platform services, I hope to be able to work on the table in the daily schedule schedule to obtain relevant data to each day on duty personnel, to achieve the smooth and timely message. Among the technologies involved are spring, quartz, and so on. The specific steps are as follows:

First, the preparation of business class:

Package com.greedc.czd;
Import org.apache.log4j.* Public
class Taskjob {public
       static Logger log = Logger.getlogger (Taskjob.class);
       public void Tellmethetruth () {
              //TODO auto-generated method stub
              try {
                     log.info ("Process task start ...");
                     The business logic code calls
                     System.out.println ("Time [" + New Java.util.Date (). toLocaleString () + "]---> Hello. ");
                     Log.info ("Process task end!");
              catch (Exception e) {
                     log.error ("Processing Task occurrence exception", e);}}}

Second, configure spring's Applicationcontext-quartz.xml file:

2.1 Configuration Task Jobdetailbean
2.2 Configuring Triggers Crontriggerbean
2.3 Configuration Scheduler Schedulerfactorybean

<beans> <bean name= "Taskjob" class= "Com.greedc.czd.TaskJob"/> <!--configuration task--> <bean id= "
       Methodinvokingjobdetail "class=" Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean "> <property name= "TargetObject" > <ref bean= "taskjob"/> </property> <property

    Name= "Targetmethod" > <value>TellMeTheTruth<alue> </property> </bean>
        <!--configuration triggers--> <bean id= "Crontrigger" class= "Org.springframework.scheduling.quartz.CronTriggerBean" > <!--It is not possible to refer to taskjob directly in the attribute Jobdetail because he is asking for a jobdetail type of object, so we have to turn by Methodinvokingjobdetailfactorybean > <property name= "Jobdetail" > <!--point to our mission--> <ref bean= "Methodinvokingjob Detail "/> </property> <!--daily 8 to 21 points per 1-minute trigger, specific instructions see Appendix--> <property name=" Cronexpres Sion "> <value>0 * 08-21 * *?<alue> </property> </bean> <!--configuration Scheduler--> <bean class= "Org.spri Ngframework.scheduling.quartz.SchedulerFactoryBean "> <!--add triggers--> <property name=" triggers "
           ;
       <list> <!--triggers list--> <ref local= "Crontrigger"/> <st> </property> </bean> </beans>

Three, need to use the jar package:

Spring.jar,quartz.jar,commons-logging.jar,commons-dbcp.jar,commons-pool-1.3.jar

Add here that the version of Quartz is not fixed, Pro-Test 1.6.x and 2.1.x are no problem, but encounter a tricky problem: Can not find commons.collections.setutils.orderset this class, and then check that there is a jar packet conflict, remove the lower version of the Commons-collections.jar I'll be right there.


Four, put the quartz.properties under the class path:

#============================================================================ # Configure Main Scheduler Properties  #============================================================================ Org.quartz.scheduler.instanceName =
Defaultquartzscheduler org.quartz.scheduler.instanceId = AUTO Org.quartz.scheduler.rmi.export = False Org.quartz.scheduler.rmi.proxy = False Org.quartz.scheduler.wrapJobExecutionInUserTransaction = False #============= =============================================================== # Configure ThreadPool #========================== ================================================== #org. Quartz.threadPool.class = Org.quartz.simpl.SimpleThreadPool Org.quartz.threadPool.threadCount = Ten org.quartz.threadPool.threadPriority = 5 # Org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = True #============================== ============================================== # Configure Jobstore #============================================= =============================== #org. quartz.jobStore.class = Org.quartz.impl.jdbcjobstore.JobStoreTX #
Org.quartz.jobStore.driverDelegateClass = Org.quartz.impl.jdbcjobstore.oracle.OracleDelegate Org.quartz.jobStore.misfireThreshold = 60000 #org. quartz.jobStore.useProperties = False #
Org.quartz.jobStore.tablePrefix = Qrtz_ #org. Quartz.jobStore.dataSource = myDS #org. quartz.jobStore.isClustered = True #org. Quartz.jobStore.clusterCheckinInterval = 15000 #============================================================ ================ # Configure DataSource #========================================================================= = = = Org.quartz.dataSource.myDS.driver = Com.mysql.jdbc.Driver Org.quartz.dataSource.myDS.URL = jdbc:mysql://
Localhost/test Org.quartz.dataSource.myDS.user = root Org.quartz.dataSource.myDS.password = root Org.quartz.dataSource.myDS.maxConnections = 10

Five, test execution of the class, as long as the spring configuration file loaded to see the timing of the task run. such as: Testapp.java:

Package com.greedc.czd;

Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.FileSystemPathXmlApplicationContext;

public class TestApp {public


static void Main (string[] args) {

     System.out.println ("Load Spring configuration file ...");
     ApplicationContext context = new Filesystemxmlapplicationcontext ("Webroot/web-inf/applicationcontext-quartz.xml");
     SYSTEM.OUT.PRINTLN ("Load configuration file Complete!");   ApplicationContext context2 = new Classpathxmlapplicationcontext ("Test/timertask/quartztimer.xml");

}
VI. Appendix

Appendix: cronexpression Expression Explanation:

Special characters allowed by field allowed values

Seconds 0-59,-*/
Cent 0-59,-*/
Hour 0-23,-*/
Date 1-31,-*? /L W C
Month 1-12 or JAN-DEC,-*/
Week 1-7 or Sun-sat,-*? /L C #
Year (optional) leave blank, 1970-2099,-*/

0 0 * *?---------------triggered every day 12:00 noon
0 15 10? * *---------------Trigger every 10:15
0 * *?---------------triggered 10:15 daily
0 15 10 * *? *---------------Trigger every 10:15
0 15 10 * *? 2007---------------triggered 10:15 every day in 2007 years.
0 * * *?---------------each day from 2:00 to 2:59 every minute
0 0/5 * *?---------------is triggered every 5 minutes between 2:00 and 2:59 daily
0 0/5 14,18 * *?---------------is triggered every 5 minutes between 2:00 to 2:59 and 6:00 to 6:59 every day
0 0-5 * *?---------------each day from 2:00 to 2:05 per minute
0 10,44 14? 3 WED---------------every March Wednesday at 2:00 and 2:44
0 15 10? * Mon-fri---------------triggers from Monday to Friday daily 10:15
0 *?---------------triggered every 15 days of the month at 10:15
0 L *?---------------triggered at 10:15 on the last day of each month
0 15 10? * 6L---------------triggered at the last Friday of every month, 10:15
0 15 10? * 6L 2012-2015---------------at the end of the month of Friday, 2013, 2014 and 2015, triggered by 10:15
0 15 10? * 6#3---------------triggered on the third Friday of every month, 10:15
0 0 1/5 *?---------------from the first day of each month, every 5 days 12:00 noon.
0 Each?---------------triggered every 11:11 of November 11
Vii. Supplementary Notes (reproduced):

In spring, it is not convenient to use the JDK Timer class library for task scheduling, and the key is not to specify a specific year, month, day, time, and time as a cron service. You can only pass the time to it by converting it into microseconds. If the task is performed once a day, you need to configure it in spring as follows:
<bean id= "Scheduledtask" class= "Org.springframework.scheduling.timer.ScheduledTimerTask" >
<!--delay time when the program starts to perform tasks-->
<property name= "delay" value= "0"/>
<!--every other day, =24x60x60x1000 microseconds a day-->
<property name= "period" value= "86400000"/>
<!--Business Statistics Report Bean-->
<property name= "TimerTask" ref= "Businessreport"/>
</bean>
Where period is the number of microseconds in a day. If you run it once a month for 1st, it's complicated and you don't know how to configure it. Because the month has big, the moon's cent, the monthly microsecond number is different.
The Quartz class library not only has the same configuration as the Timer class library of the JDK, but more importantly, it has the configuration of a cron service similar to UNIX. Therefore, we used the interface of the Quartz class library in the migration.

Viii. supplementary corrections, acquisition of time classes



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.