Modify the configuration file - the task scheduler of the job configuration is the cluster task scheduler

  The Quarz used in the task scheduling framework in the project that has been following up, initially used the configuration file -job to configure the single-machine Quarz scheduling. As the demand increases, the task configuration in the configuration file also increases.

Suddenly one day I learned that our system was going to be deployed in cluster mode, which meant that I had to modify Quarz to be in cluster mode, and because I was unwilling to assign jobs configured in the configuration file to save trouble.

Change the job to code form, for example: addJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName, Class jobClass) {,,,}.

So let's start: modify the configuration file - the task scheduler of the job configuration is the cluster task scheduler.

  1. Configuration of stand-alone mode (quarz scheduling information and status of stand-alone mode are stored in memory by default)

   1. Job configuration

           

 

  2. Scheduler scheduling

    

 

  2. Modify the configuration of stand-alone mode to cluster mode configuration

     Note: There are two ways to integrate Quartz with Spring: JobDetailFactoryBean and MethodInvokeJobDetailFactoryBean , the MethodInvokeJobDetailFactoryBean method used in the configuration of the previous stand-alone mode

       However, in cluster mode configuration, the method of MethodInvokeJobDetailFactoryBean must be changed to JobDetailFactoryBean, because the JobDetails created by MethodInvokeJobDetailFactoryBean are not serializable.

         Therefore, it is not suitable for persistent job storage, so for persistent jobs, you need to inherit org.springframework.scheduling.quartz.QuartzJobBean yourself, and modify the Class corresponding to JobDetail to JobDetailFactoryBean

       Of course, you also have to modify the attribute filling corresponding to the JobDetailFactoryBean class, for example, change targetObject to jobClass

    1. Modify the entity job to inherit QuartzJobBean and rewrite executeInternal(), and JobDetailFactoryBean will automatically call executeInternal()

    

 

    2. Modify the job configuration (modify the MethodInvokeJobDetailFactoryBean to JobDetailFactoryBean)

    

   

    3. Scheduler scheduling: Modify the task scheduler (memory scheduling) of the configuration file-job configuration as the cluster task scheduler

      3.1. Cluster quarz-scheduler configuration

        

 

      3.2, job configuration (set the value of the scheduler attribute of TaskManage to commonSchedulerFactoryBean, commonSchedulerFactoryBean is Quarz-scheduler configured in cluster mode)

        

      3.3, TaskManage class (use the configured cluster mode scheduler-scheduler to add tasks to Quarz)

        

 

         Note: One of the things the TaskManage class actually does is to use the cluster-mode scheduler to add tasks for Quarz to schedule.

    4.   Spring injection problem of spring quartzy (dao layer and service layer)

        In the real job implementation class, spring is often used to inject Bean objects. The following code tests of mine often encounter NULL instances of the dao layer and the service layer at first. Code example :

      

       Solution:

        Later, the online query found that the instantiation process of the Job object is carried out in Quartz, and the Bean object is in the Spring container. Spring loads Quarzy first, and then loads the Bean object.

            You can refer to the following two links for details:

             Reference document 1: https://blog.csdn.net/maiyikai/article/details/70199668

               Reference document 2: http://www.cnblogs.com/daxin/p/3608320.html

         ◆ So if we don't specify jobFactory, Spring will use AdaptableJobFactory by default

        

 

         

        

        The AdaptableJobFactory class simply creates a Job without injecting beans.

        

 

        ◆ So the solution is here, we can just inject the properties of the Job here, let's write a class that inherits the AdaptableJobFactory class, and then override the createJobInstance() method

        To inject the Job, first call the method of the parent class to create the Job, and then inject the beans that the Job class depends on:

        

        Next, configure him to Spring

       

      

      Finally, set the jobFactory property in the TaskManage class, and set the jobFactory of SchedulerFactoryBean to our own. The scheduler property of this class mentioned earlier is equal to

      commonSchedulerFactoryBean is the quarz-scheduler bean configuration of the pre-configured cluster, and its type is SchedulerFactoryBean.

      The jobFactory property can be set:

      

     

      In this way, Spring's injection function for Job is completed. It is actually very simple. The principle is to extend the method of JobFactory to create a job, and then perform attribute injection after the Job is created.

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324459654&siteId=291194637