tomcat load balancing three, regular task processing

Posted May 25, 20203 min read

  1. The project uses the quartz framework to do timing tasks, so what is done is quart distributed to solve the problem of timing tasks under load balancing

  2. Need to persist the scheduled task to the database, in database corresponding sql statement find the corresponding database and generate the corresponding database table
    image.png

  3. Configure scheduled tasks in the project

  4. Create a quartz.properties file with the following content

    org.quartz.scheduler.instanceName = TestScheduler1
    org.quartz.scheduler.instanceId = AUTO

    org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
    org.quartz.threadPool.threadCount = 10
    org.quartz.threadPool.threadPriority = 5
    org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true

    org.quartz.jobStore.misfireThreshold = 60000
    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.maxMisfiresToHandleAtATime = 10
    org.quartz.jobStore.isClustered = true
    org.quartz.jobStore.clusterCheckinInterval = 20000

    org.quartz.jobStore.dataSource = quartz
    org.quartz.dataSource.quartz.provider = hikaricp
    org.quartz.dataSource.quartz.driver = com.mysql.jdbc.Driver
    org.quartz.dataSource.quartz.URL = jdbc:mysql:///purchase_order? serverTimezone = UTC database address
    org.quartz.dataSource.quartz.user = root username
    org.quartz.dataSource.quartz.password = xx password
    org.quartz.dataSource.quartz.maxConnections = 5

  5. Create a task

    @PersistJobDataAfterExecution
    @ DisallowConcurrentExecution //Do not allow concurrent execution
    public class QuartzDemo extends QuartzJobBean {

     @Override
     protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
         System.out.println("----------- 111111111 ----------");
     }

    }

Configuration tasks

  <bean id = "updateArrivedJobDetail" class = "org.springframework.scheduling.quartz.JobDetailFactoryBean">
        <property name = "jobClass">
            <value> com.creatooor.order.quartz.QuartzDemo </value>
        </property>
        <property name = "requestsRecovery" value = "true" />
        <property name = "durability" value = "true" />
    </bean>

    <bean id = "updateArrivedJobDetailTrigger" class = "org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <!-Give the task class description to the trigger->
        <property name = "jobDetail" ref = "updateArrivedJobDetail"> </property>
        <!-Cron expression, timing rules->
        <property name = "cronExpression" value = "0 20 13 * *?"> </property>
    </bean>

    <!-Total management container->
    <bean name = "quartzScheduler" class = "org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name = "dataSource">
            <ref bean = "dataSource" />
        </property>
        <property name = "applicationContextSchedulerContextKey" value = "applicationContextKey" />
        <property name = "configLocation" value = "classpath:quartz.properties" />
<!-Rewrite the task configuration information so that when the task execution time is modified, it will take effect without clearing the database table, and the program will automatically modify the execution time of the corresponding task->
        <property name = "overwriteExistingJobs" value = "true" />

        <property name = "triggers">
            <list>
                <!-<ref bean = "updatePriceTrigger" />->
                <!-<ref bean = "updateArrivedJobDetailTrigger" />->
                <ref bean = "updateArrivedJobDetailTrigger" />
            </list>
        </property>
    </bean>

The result:
image.png
Only one tomcat starts to perform tasks