Quartz cluster configuration

Source: Internet
Author: User

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
  1. #==============================================================
  2. #Configure Main Scheduler Properties
  3. #==============================================================
  4. Org.quartz.scheduler.instanceName = Quartzscheduler
  5. Org.quartz.scheduler.instanceId = AUTO
  6. #==============================================================
  7. #Configure Jobstore
  8. #==============================================================
  9. Org.quartz.jobStore. class = Org.quartz.impl.jdbcjobstore.JobStoreTX
  10. Org.quartz.jobStore.driverDelegateClass = Org.quartz.impl.jdbcjobstore.StdJDBCDelegate
  11. Org.quartz.jobStore.tablePrefix = Qrtz_
  12. org.quartz.jobStore.isClustered = true
  13. Org.quartz.jobStore.clusterCheckinInterval = 20000
  14. Org.quartz.jobStore.dataSource = MyDS
  15. #==============================================================
  16. #Configure DataSource
  17. #==============================================================
  18. Org.quartz.dataSource.myDS.driver = Com.mysql.jdbc.Driver
  19. Org.quartz.dataSource.myDS.URL = jdbc:mysql://192.168.20.195:3306/database?useunicode=true& Characterencoding=utf-8
  20. Org.quartz.dataSource.myDS.user = root
  21. Org.quartz.dataSource.myDS.password = 123456
  22. Org.quartz.dataSource.myDS.maxConnections =
  23. #==============================================================
  24. #Configure ThreadPool
  25. #==============================================================
  26. Org.quartz.threadPool. class = Org.quartz.simpl.SimpleThreadPool
  27. Org.quartz.threadPool.threadCount = Ten
  28. org.quartz.threadPool.threadPriority = 5
  29. 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
    1. #==============================================================
    2. #Configure Main Scheduler Properties
    3. #==============================================================
    4. Org.quartz.scheduler.instanceName = Quartzscheduler
    5. Org.quartz.scheduler.instanceId = AUTO


Additionally, the cluster is specified with the appropriate configuration and the check interval is 20s:
Java code
    1. org.quartz.jobStore.isClustered = true
    2. 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
  1. Import Java.lang.reflect.Method;
  2. Import Org.apache.commons.logging.Log;
  3. Import Org.apache.commons.logging.LogFactory;
  4. Import Org.quartz.JobExecutionContext;
  5. Import org.quartz.JobExecutionException;
  6. Import Org.springframework.context.ApplicationContext;
  7. Import Org.springframework.scheduling.quartz.QuartzJobBean;
  8. Public class Mydetailquartzjobbean extends Quartzjobbean {
  9. protected Final Log logger = Logfactory.getlog (GetClass ());
  10. private String TargetObject;
  11. private String Targetmethod;
  12. private ApplicationContext CTX;
  13. @Override
  14. protected void Executeinternal (Jobexecutioncontext context)
  15. throws Jobexecutionexception {
  16. try {
  17. Logger.info ("execute [" + TargetObject + "] at once>>>>>>");
  18. Object otargetobject = Ctx.getbean (TargetObject);
  19. Method m = null;
  20. try {
  21. m = Otargetobject.getclass (). GetMethod (Targetmethod, new class[] {jobexecutioncontext.   class});
  22. M.invoke (Otargetobject, new object[] {context});
  23. } catch (SecurityException e) {
  24. Logger.error (e);
  25. } catch (Nosuchmethodexception e) {
  26. Logger.error (e);
  27. }
  28. } catch (Exception e) {
  29. throw New Jobexecutionexception (e);
  30. }
  31. }
  32. public void Setapplicationcontext (ApplicationContext applicationcontext) {
  33. this.ctx = ApplicationContext;
  34. }
  35. public void Settargetobject (String targetObject) {
  36. this.targetobject = TargetObject;
  37. }
  38. public void Settargetmethod (String targetmethod) {
  39. This.targetmethod = Targetmethod;
  40. }


Finally, to configure the spring file.
Java code
  1. <bean id="Mapscheduler" lazy-init="false" autowire="no"
  2. class="Org.springframework.scheduling.quartz.SchedulerFactoryBean" >
  3. <property name="triggers" >
  4. <list>
  5. <ref bean="Dailytrigger"/>
  6. <ref bean="Billcounttrigger"/>
  7. <ref bean="Useraccttrigger"/>
  8. </list>
  9. </property>
  10. <property name="Applicationcontextschedulercontextkey" value="ApplicationContext"/>
  11. <property name="configlocation" value="classpath:quartz.properties"/>
  12. </bean>
  13. <bean id="dailybilljob" class= "com.***.job. Dailybilljob "/>
  14. <bean id="Dailybilljobdetail" class="Org.springframework.scheduling.quartz.JobDetailBean" >
  15. <property name="Jobclass" >
  16. <value>com.autelan.auteview.lib.util.mydetailquartzjobbean
  17. </value>
  18. </property>
  19. <property name="Jobdataasmap" >
  20. <map>
  21. <entry key="TargetObject" value="Dailybilljob"/>
  22. <entry key="Targetmethod" value="execute"/>
  23. </map>
  24. </property>
  25. </bean>
  26. <bean id="Dailytrigger" class="Org.springframework.scheduling.quartz.CronTriggerBean" >
  27. <property name="Jobdetail" >
  28. <ref bean="Dailybilljobdetail"/>
  29. </property>
  30. <property name="Cronexpression" >
  31. <value> * *?</value>
  32. </property>
  33. </bean>
  34. Reprint please specify the source http://forhope.iteye.com/blog/1398990


Done!

Quartz cluster configuration

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.