Implementation of quartz and spring integration for thermal deployment (II.)

Source: Internet
Author: User

Spring's Org.springframework.scheduling.quartz.JobDetailBean provides the job serializable implementation (concrete implementation can see the source code)

At this point, our original job can inherit Quartzjobbean, it will be automatically serialized to the database, quartz the specific configuration file as follows

<?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:jee= "Http://www.springframework.org/schema/jee"Xmlns:tx= "Http://www.springframework.org/schema/tx" xmlns:context= "Http://www.springframework.org/schema/context"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/TX http://www.springframework.org/schema/tx/spring-tx-2.5.xsd/HTTP Www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd/HTTP Www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd "default-lazy-init= "true" > <description>quartz configuration </description> <!--quartz using the data source configuration--<bean Id= "Quartzdatasource"class= "Org.springframework.jdbc.datasource.DriverManagerDataSource" > <property name= "driverclassname" value= "${ Quartz.jdbc.driver} "/> <property name=" url "value=" ${quartz.jdbc.url} "/> <property name=" Usern Ame "value=" ${quartz.jdbc.username} "/> <property name=" password "value=" ${quartz.jdbc.password} "/> &L T;/bean> <!--Another transaction manager, JDBC Single data source transaction--<bean id= "Quartztransactionmanager"class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager" > <property name= "DataSource" ref= " Quartzdatasource "/> </bean> <bean name=" Quartzscheduler "class= "Org.springframework.scheduling.quartz.SchedulerFactoryBean" > <property name= "DataSource" ref= "        Quartzdatasource "/> <property name=" Applicationcontextschedulercontextkey "value=" ApplicationContext "/> <property name= "configlocation" value= "classpath:quartz.properties"/><!--This is a must, Quartzscheduler delay start, after the application started Quartzscheduler restart--<property name= "Startupdelay" value= "/><!--This is optional, Quartzscheduler update the job when it is started, so you do not have to delete the Qrtz_job_details table corresponding record after each modification targetobject--<property name= "Overwriteexistingjobs" value= "true"/> <property name= "Jobdetails" > <list> <ref bean= "Serverhealthjobdetail"/> </list> </prope Rty> </bean> <!--run count--<bean id= "Serverhealthjobdetail"class= "Org.springframework.scheduling.quartz.JobDetailBean" > <!--the Requestsrecovery property is true, when the quartz service is aborted, When you start a task again, you try to resume all tasks that were not completed before you performed--<property name= "Requestsrecovery" value= "true"/> <!--identity job is persistent, delete trigger is not removed-<property name= "Durability" value= "true"/> <property name= "Jobclass" value= "Cn.yzzn . Hvac.quartz.job.ServerHealthJob "/> </bean> </beans>

We see <property name= "Applicationcontextschedulercontextkey" value= "ApplicationContext"/> This label

Spring will help us automatically inject applicationcontext. Not in the previous article, get applicationcontext manually by using spring's tools.

The Serverhealthjob code is as follows

 Public Abstract classJobsupportextendsQuartzjobbean {PrivateBuildingmanager Buildingmanager; PrivatePlcmanager Plcmanager; Private StaticLogger Logger = Loggerfactory.getlogger (jobsupport.class); PrivateApplicationContext ApplicationContext; /*** ApplicationContext injected from Schedulerfactorybean. */     Public voidSetapplicationcontext (ApplicationContext applicationcontext) { This. ApplicationContext =ApplicationContext; }      Public<T> T Getbean (String beanname, class<t>clazz) {        return  This. Applicationcontext.getbean (Beanname, clazz); } @Overrideprotected voidexecuteinternal (jobexecutioncontext context)throwsjobexecutionexception {if(objectutils.isnulloremptystring (Buildingmanager)) {Buildingmanager= Getbean ("Buildingmanager", Buildingmanager.class); }        if(objectutils.isnulloremptystring (Plcmanager)) {Plcmanager= Getbean ("Plcmanager", Plcmanager.class); } List<Building> buildinglist =buildingmanager.getbuildings (); intSize =buildinglist.size ();  for(inti = 0; i < size; i++) {Building Building=Buildinglist.get (i); Set<Plc> Plcset =Building.getplcs ();  for(plc plc:plcset) {Inneriter (Building, PLC, I, size); }        }    }      Public Abstract voidInneriter (Building Building, PLC plc,intIndexintsize); } Public classServerhealthjobextendsJobsupport {@Override Public voidInneriter (Building Building, PLC plc,intIndexintsize) {        //TODO auto-generated Method Stub             }     }

Serverhealthjob can be easily serialized to the database by spring. The tedious operation in the last post was solved.

Jobsupport is my system business needs, students can customize the implementation as long as the inheritance Quartzjobbean instantly.

The secondary jobdetail already exists in the database

Triggermanager didn't change much, either.

/*** Quartz Scheduler Management class * Methods with no GroupName parameters are automatically populated as default groups Scheduler.default_group *@authorpigwing **/ Public classSchedulermanagerimplImplementsSchedulermanager {PrivateQuartzdao Quartzdao; PrivateScheduler Scheduler; Private StaticLogger Logger = Loggerfactory.getlogger (Usercontroller.class); /*** * Add a trigger task with default group form*/     Public voidAddtrigger (String triggername, String jobName, string cronexpression)throwsschedulerexception, ParseException {addtrigger (triggername, Scheduler.default_group, JobName, Scheduler.DEFA    Ult_group, cronexpression); }     /*** * Add a Trigger task*/     Public voidAddtrigger (String triggername, String triggergroupname, String jobName, String jobgrourpname, String crone Xpression)throwsschedulerexception, ParseException {if(Stringutils.isempty (triggername)) {Throw NewRuntimeException ("Triggername can is not NULL"); }                 Try{jobdetail Jobdetail=Scheduler.getjobdetail (JobName, jobgrourpname); if(Jobdetail! =NULL) {scheduler.addjob (Jobdetail,true); Crontrigger Crontrigger=NewCrontrigger (Triggername, Triggergroupname, Jobdetail.getname (), jobgrourpname);                Crontrigger.setcronexpression (cronexpression);                Scheduler.schedulejob (Crontrigger);            Scheduler.reschedulejob (Crontrigger.getname (), Crontrigger.getgroup (), Crontrigger); }Else{logger.error ("Cant not find jobdetail:" +jobgrourpname); }        }Catch(schedulerexception e) {logger.error (E.getmessage ()); Throwe; }    }      /*** Return all trigger information*/     PublicList<map<string, object>>getalltriggers () {returnquartzdao.getquartztriggers (); }     /*** Stop Trigger*/     Public voidParsetrigger (String triggername, string groupName)throwsschedulerexception {Try{Scheduler.pausetrigger (triggername, groupName); }Catch(schedulerexception e) {logger.error (E.getmessage ()); Throwe; }             }     /*** Stop trigger with default group form*/     Public voidParsetrigger (String triggername)throwsschedulerexception {parsetrigger (triggername, Scheduler.default_group); }     /*** Restart Trigger*/     Public voidResumetrigger (String triggername, string groupName)throwsschedulerexception {Try{Scheduler.resumetrigger (triggername, groupName); }Catch(schedulerexception e) {logger.error (E.getmessage ()); Throwe; }              }     /*** Restart Trigger in default group form*/     Public voidResumetrigger (String triggername)throwsschedulerexception {resumetrigger (triggername, Scheduler.default_group); }     /*** Removing triggers*/     Public BooleanRemovetrigger (String triggername, string groupName)throwsschedulerexception {Try{Parsetrigger (triggername, groupName); returnscheduler.unschedulejob (Triggername, groupName); }Catch(schedulerexception e) {logger.error (E.getmessage ()); Throwe; }    }     /*** Remove triggers with default group form*/     Public BooleanRemovetrigger (String triggername)throwsschedulerexception {Try {            returnRemovetrigger (Triggername, Scheduler.default_group); }Catch(schedulerexception e) {logger.error (E.getmessage ()); Throwe; }    }     /*** Return all task names*/     PublicString[] Getjobnames (String groupName)throwsschedulerexception {returnscheduler.getjobnames (groupName); }      PublicString[] Getjobnames ()throwsschedulerexception {returnscheduler.getjobnames (Scheduler.default_group); } @Autowired Public voidSetquartzdao (Quartzdao Quartzdao) { This. Quartzdao =Quartzdao; } @Autowired Public voidSetscheduler (Scheduler Scheduler) { This. Scheduler =Scheduler; }      }

This article transferred from: http://www.cnblogs.com/pigwing/archive/2011/07/12/2104002.html

Quartz implementation of hot deployment with spring Integration (ii)

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.