Quartz cluster configuration (100% success) Let's take a look at Quartz's persistence basics:
Reference
1 We all know quartz the most basic concept is job, in the job call specific service to complete the specific functions, quartz need to store each job, convenient scheduling, quartz storage job mode of three kinds, Our most commonly used is also quartz the default is that Ramjobstore,ramjobstore is the name of the job to store the relevant information in memory, if you configure quartz job information with spring, all information is configured in XML, when SPIRNG When the context starts, it loads the job information in the XML into memory. This nature determines that once the JVM hangs or the container hangs, the in-memory job information disappears and cannot be persisted. The other two methods are Jobstoretx and JOBSTORECMT, for the time being, the difference between the two is not discussed, the use of both Jobstore,quartz through the JDBC Direct Connect or application server Jndi connection database, read the configuration in the database job initialization information, And the job is serialized into the database through Java, so that each job information is persisted, even if the JVM or the container is hung off, but also through the database to sense the status and information of other jobs.
2 Quartz Each node of the cluster is perceived by the same database instance (exactly the same set of tables as the same DB instance).
From the above, we need to create a database table Quartz to use, this SQL file in: Quartz-1.8.6\docs\dbtables. There are SQL files for each database under this folder, MySQL chooses Tables_mysql.sql. Create the appropriate table.
Next, create a new quartz.properties to overwrite this file in the jar package, and the new properties file will be placed in the SRC root directory. Here is the file content:
Java code
- #==============================================================
- #Configure Main Scheduler Properties
- #==============================================================
- Org.quartz.scheduler.instanceName = Quartzscheduler
- Org.quartz.scheduler.instanceId = AUTO
- #==============================================================
- #Configure Jobstore
- #==============================================================
- Org.quartz.jobStore. class = Org.quartz.impl.jdbcjobstore.JobStoreTX
- Org.quartz.jobStore.driverDelegateClass = Org.quartz.impl.jdbcjobstore.StdJDBCDelegate
- Org.quartz.jobStore.tablePrefix = Qrtz_
- org.quartz.jobStore.isClustered = true
- Org.quartz.jobStore.clusterCheckinInterval = 20000
- Org.quartz.jobStore.dataSource = MyDS
- #==============================================================
- #Configure DataSource
- #==============================================================
- Org.quartz.dataSource.myDS.driver = Com.mysql.jdbc.Driver
- Org.quartz.dataSource.myDS.URL = jdbc:mysql://192.168.20.195:3306/database?useunicode=true& Characterencoding=utf-8
- Org.quartz.dataSource.myDS.user = root
- Org.quartz.dataSource.myDS.password = 123456
- Org.quartz.dataSource.myDS.maxConnections =
- #==============================================================
- #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
You can see that in addition to the configuration of the data source, the thread pool, we have specified an scheduler instance with an instance ID of automatic assignment.
Java code
- #==============================================================
- #Configure Main Scheduler Properties
- #==============================================================
- Org.quartz.scheduler.instanceName = Quartzscheduler
- Org.quartz.scheduler.instanceId = AUTO
Additionally, the cluster is specified with the appropriate configuration and the check interval is 20s:
Java code
- org.quartz.jobStore.isClustered = true
- Org.quartz.jobStore.clusterCheckinInterval = 20000
Finally, configure the Applicant-context.xml file. Here are some special notes:
Referencing the MethodInvoking method in the Methodinvokingjobdetailfactorybean class does not support serialization, so the QUARTZ TASK serialization is thrown into the database when it is serialized.
So we have to implement the Methodinvokingjobdetailfactorybean function, which is replaced with Mydetailquartzjobbean.
Java code
- Import Java.lang.reflect.Method;
- Import Org.apache.commons.logging.Log;
- Import Org.apache.commons.logging.LogFactory;
- Import Org.quartz.JobExecutionContext;
- Import org.quartz.JobExecutionException;
- Import Org.springframework.context.ApplicationContext;
- Import Org.springframework.scheduling.quartz.QuartzJobBean;
- Public class Mydetailquartzjobbean extends Quartzjobbean {
- protected Final Log logger = Logfactory.getlog (GetClass ());
- private String TargetObject;
- private String Targetmethod;
- private ApplicationContext CTX;
- @Override
- protected void Executeinternal (Jobexecutioncontext context)
- throws Jobexecutionexception {
- try {
- Logger.info ("execute [" + TargetObject + "] at once>>>>>>");
- Object otargetobject = Ctx.getbean (TargetObject);
- Method m = null;
- try {
- m = Otargetobject.getclass (). GetMethod (Targetmethod, new class[] {jobexecutioncontext. class});
- M.invoke (Otargetobject, new object[] {context});
- } catch (SecurityException e) {
- Logger.error (e);
- } catch (Nosuchmethodexception e) {
- Logger.error (e);
- }
- } catch (Exception e) {
- throw New Jobexecutionexception (e);
- }
- }
- public void Setapplicationcontext (ApplicationContext applicationcontext) {
- this.ctx = ApplicationContext;
- }
- public void Settargetobject (String targetObject) {
- this.targetobject = TargetObject;
- }
- public void Settargetmethod (String targetmethod) {
- This.targetmethod = Targetmethod;
- }
Finally, to configure the spring file.
Java code
- <bean id="Mapscheduler" lazy-init="false" autowire="no"
- class="Org.springframework.scheduling.quartz.SchedulerFactoryBean" >
- <property name="triggers" >
- <list>
- <ref bean="Dailytrigger"/>
- <ref bean="Billcounttrigger"/>
- <ref bean="Useraccttrigger"/>
- </list>
- </property>
- <property name="Applicationcontextschedulercontextkey" value="ApplicationContext"/>
- <property name="configlocation" value="classpath:quartz.properties"/>
- </bean>
- <bean id="dailybilljob" class= "com.***.job. Dailybilljob "/>
- <bean id="Dailybilljobdetail" class="Org.springframework.scheduling.quartz.JobDetailBean" >
- <property name="Jobclass" >
- <value>com.autelan.auteview.lib.util.mydetailquartzjobbean
- </value>
- </property>
- <property name="Jobdataasmap" >
- <map>
- <entry key="TargetObject" value="Dailybilljob"/>
- <entry key="Targetmethod" value="execute"/>
- </map>
- </property>
- </bean>
- <bean id="Dailytrigger" class="Org.springframework.scheduling.quartz.CronTriggerBean" >
- <property name="Jobdetail" >
- <ref bean="Dailybilljobdetail"/>
- </property>
- <property name="Cronexpression" >
- <value> * *?</value>
- </property>
- </bean>
- Reprint please specify the source http://forhope.iteye.com/blog/1398990
Done!
Quartz cluster configuration