Springframework.scheduling.quartz Work Scheduler Use (iii)-Custom multithreaded Pool task __ Custom multithreaded Pool

Source: Internet
Author: User
Tags aop

Implementation Scenario One:

1 the task in the frequency H1 (30 seconds) timing, the frequency of the task to perform the time required to exceed the frequency of the time period (30 seconds), the frequency of the task has not been completed, the next execution of the task Midway interrupted;

2 The second 30 second time node (that is, the next frequency) when scheduling tasks, because there is no idle thread (the previous frequency task takes up all the threads), there is a large number of threads waiting in the current scheduling task, resulting in the second 30 Seconds Time node scheduling task execution late. < that is: "Springframework.scheduling.quartz work Scheduler uses (ii)-Custom multithreaded pool Tasks" problems encountered >

Objective: In order to solve the problem that the task of realizing the scene is interrupted in the course of execution, the problems and situations of data loss and the delay of the next frequency dispatch task in carrying out the task work.

Programme II:

Springframework.scheduling.quartz Job Scheduler Configuration task scheduling + background definition multithreaded pool Java.util.concurrent.ThreadPoolExecutor

Configuration file: (Configure only scheduled execution Tasks )

<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:tx= "Http://www.springframework.org/schema/tx" xmlns:aop= " Http://www.springframework.org/schema/aop "xmlns:context=" Http://www.springframework.org/schema/context "xmlns: task= "Http://www.springframework.org/schema/task" xsi:schemalocation= "http://www.springframework.org/schema/ Beans Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd Http://www.springframework.org/sch Ema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd Http://www.springframewo Rk.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd "default-lazy-init= false" &G
	T
	<bean id= "Configholder" class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" > </bean> <context:annotation-config/> <tx:annotation-Driven proxy-target-class= "true"/> <bean id= ruleservice "class=" Com.hp.bon.omc.nms.core.threadPool.RuleServ Ice2 "></bean> <!--timed--> <bean id=" Rulebean "class=" Org.springframework.scheduling.quar Tz. Methodinvokingjobdetailfactorybean "> <property name=" targetobject "ref=" Ruleservice "/> <prope Rty name= "Targetmethod" value= "UpdateRule"/> <property name= "concurrent" value= "false"/> </bean > <bean id= "rule" class= "Org.springframework.scheduling.quartz.CronTriggerFactoryBean" > <propert Y name= "Jobdetail" ref= "Rulebean"/> <!--every 30 seconds--> <property name= "cronexpression" value= "0 /30 * * * * *?
        /> </bean> <!--timed end--> <bean id= "Startquertz" lazy-init= "false" autowire= "no"
          class= "Org.springframework.scheduling.quartz.SchedulerFactoryBean" > <property name= "jobdetails" > <list> <ref bean= "Rulebean"/> </list> </property> <proper Ty name= "triggers" > <list> <ref bean= "rule"/> </list> </ Property> <!--10 seconds to start the task--> <property name= "Startupdelay" value= "ten"/>
T </beans>

Task Scheduler class: RuleService2

Package com.hp.bon.omc.nms.core.threadPool;
Import java.util.ArrayList;
Import java.util.List;
Import Java.util.concurrent.BlockingQueue;
Import Java.util.concurrent.LinkedBlockingQueue;
Import Java.util.concurrent.ThreadPoolExecutor;
Import Java.util.concurrent.TimeUnit;
Import Org.slf4j.Logger;
Import Org.slf4j.LoggerFactory;
Import Org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

Import Org.springframework.stereotype.Service;
	@Service public class RuleService2 {private static final Logger LOG = Loggerfactory.getlogger (Ruleservice2.class);
	public void UpdateRule () {updaterulewiththreadpoolexecutor ();
		public void Updaterulewiththreadpoolexecutor () {Log.info ("task has been scheduled, service RuleService2 started ...");
		blockingqueue<runnable> queue = new linkedblockingqueue<runnable> (300);
		Threadpoolexecutor executor = new Threadpoolexecutor (5, 60,timeunit.seconds, queue);
		Executor.setrejectedexecutionhandler (New Threadpoolexecutor.callerrunspolicy ());list<string> strlist = new arraylist<string> ();
		for (int i = 0; i < i++) {Strlist.add ("test" + i); for (int i = 0; i < strlist.size (); i++) {Executor.execute (new Thread (New StartTaskThread2 (Strlist.get (i)), "Te
		Stthread ". Concat (" "+i)));
 }
	}
}

Background defines multithreaded pool Java.util.concurrent.ThreadPoolExecutor: (Specific work on the dispatch task)

blockingqueue<runnable> queue = new linkedblockingqueue<runnable> (300);
Threadpoolexecutor executor = new Threadpoolexecutor (5, 60,timeunit.seconds, queue);

Custom Threading Class StartTaskThread2 class: Consistent with the Springframework.scheduling.quartz work scheduler using (ii)-Custom multithreaded pool tasks

Output

<!--first task scheduling: The POOL-1 thread pool starts 5 threads, performs a task queue task in 8 seconds each interval, performs 20 tasks-->

2018-04-13 15:44:30.034 INFO [startquertz_worker-1]-tasks have been scheduled, service RuleService2 has started ...
2018-04-13 15:44:30.039 INFO [pool-1-thread-1]-time:2018-04-13 15:44:30...st=test0
2018-04-13 15:44:30.040 INFO [pool-1-thread-2]-time:2018-04-13 15:44:30...st=test1
2018-04-13 15:44:30.042 INFO [pool-1-thread-3]-time:2018-04-13 15:44:30...st=test2
2018-04-13 15:44:30.044 INFO [pool-1-thread-4]-time:2018-04-13 15:44:30...st=test3
2018-04-13 15:44:30.045 INFO [pool-1-thread-5]-time:2018-04-13 15:44:30...st=test4
2018-04-13 15:44:38.041 INFO [pool-1-thread-1]-time:2018-04-13 15:44:38...ST=TEST5
2018-04-13 15:44:38.042 INFO [pool-1-thread-2]-time:2018-04-13 15:44:38...ST=TEST6
2018-04-13 15:44:38.043 INFO [pool-1-thread-3]-time:2018-04-13 15:44:38...st=test7
2018-04-13 15:44:38.046 INFO [pool-1-thread-4]-time:2018-04-13 15:44:38...st=test8
2018-04-13 15:44:38.047 INFO [pool-1-thread-5]-time:2018-04-13 15:44:38...st=test9
2018-04-13 15:44:46.127 INFO [pool-1-thread-2]-time:2018-04-13 15:44:46...st=test10
2018-04-13 15:44:46.127 INFO [pool-1-thread-3]-time:2018-04-13 15:44:46...st=test11
2018-04-13 15:44:46.161 INFO [pool-1-thread-1]-time:2018-04-13 15:44:46...st=test12
2018-04-13 15:44:46.164 INFO [pool-1-thread-4]-time:2018-04-13 15:44:46...st=test13
2018-04-13 15:44:46.164 INFO [pool-1-thread-5]-time:2018-04-13 15:44:46...st=test14
2018-04-13 15:44:54.128 INFO [pool-1-thread-2]-time:2018-04-13 15:44:54...st=test15
2018-04-13 15:44:54.129 INFO [pool-1-thread-3]-time:2018-04-13 15:44:54...st=test16
2018-04-13 15:44:54.163 INFO [pool-1-thread-1]-time:2018-04-13 15:44:54...st=test17
2018-04-13 15:44:54.165 INFO [pool-1-thread-4]-time:2018-04-13 15:44:54...st=test18
2018-04-13 15:44:54.166 INFO [pool-1-thread-5]-time:2018-04-13 15:44:54...st=test19

<!--second task scheduling:
The POOL-2 thread pool starts 5 threads, performs a task queue task in 8 seconds each interval, performs 20 tasks;
After the POOL-1 thread pool waits for 5 threads to be released every 8 seconds, continue to perform the remaining 10 tasks in the task queue until 30 tasks are performed;
-->
2018-04-13 15:45:00.030 INFO [startquertz_worker-2]-tasks have been scheduled, service RuleService2 has started ...
2018-04-13 15:45:00.038 INFO [pool-2-thread-3]-time:2018-04-13 15:45:00...st=test2
2018-04-13 15:45:00.040 INFO [pool-2-thread-4]-time:2018-04-13 15:45:00...st=test3
2018-04-13 15:45:00.045 INFO [pool-2-thread-1]-time:2018-04-13 15:45:00...st=test0
2018-04-13 15:45:00.046 INFO [pool-2-thread-2]-time:2018-04-13 15:45:00...st=test1
2018-04-13 15:45:00.048 INFO [pool-2-thread-5]-time:2018-04-13 15:45:00...st=test4
2018-04-13 15:45:02.129 INFO [pool-1-thread-3]-time:2018-04-13 15:45:02...st=test20
2018-04-13 15:45:02.129 INFO [pool-1-thread-2]-time:2018-04-13 15:45:02...st=test21
2018-04-13 15:45:02.163 INFO [pool-1-thread-1]-time:2018-04-13 15:45:02...st=test22
2018-04-13 15:45:02.166 INFO [pool-1-thread-4]-time:2018-04-13 15:45:02...st=test23
2018-04-13 15:45:02.167 INFO [pool-1-thread-5]-time:2018-04-13 15:45:02...st=test24
2018-04-13 15:45:08.039 INFO [pool-2-thread-3]-time:2018-04-13 15:45:08...ST=TEST5
2018-04-13 15:45:08.040 INFO [pool-2-thread-4]-time:2018-04-13 15:45:08...ST=TEST6
2018-04-13 15:45:08.046 INFO [pool-2-thread-1]-time:2018-04-13 15:45:08...st=test7
2018-04-13 15:45:08.047 INFO [pool-2-thread-2]-time:2018-04-13 15:45:08...st=test8
2018-04-13 15:45:08.048 INFO [pool-2-thread-5]-time:2018-04-13 15:45:08...st=test9
2018-04-13 15:45:10.132 INFO [pool-1-thread-2]-time:2018-04-13 15:45:10...st=test25
2018-04-13 15:45:10.133 INFO [pool-1-thread-3]-time:2018-04-13 15:45:10...st=test26
2018-04-13 15:45:10.372 INFO [pool-1-thread-5]-time:2018-04-13 15:45:10...st=test27
2018-04-13 15:45:10.373 INFO [pool-1-thread-4]-time:2018-04-13 15:45:10...st=test29
2018-04-13 15:45:10.372 INFO [pool-1-thread-1]-time:2018-04-13 15:45:10...st=test28
2018-04-13 15:45:16.040 INFO [pool-2-thread-3]-time:2018-04-13 15:45:16...st=test10
2018-04-13 15:45:16.041 INFO [pool-2-thread-4]-time:2018-04-13 15:45:16...st=test11
2018-04-13 15:45:16.048 INFO [pool-2-thread-1]-time:2018-04-13 15:45:16...st=test12
2018-04-13 15:45:16.049 INFO [pool-2-thread-2]-time:2018-04-13 15:45:16...st=test13
2018-04-13 15:45:16.049 INFO [pool-2-thread-5]-time:2018-04-13 15:45:16...st=test14
2018-04-13 15:45:24.040 INFO [pool-2-thread-3]-time:2018-04-13 15:45:24...st=test15
2018-04-13 15:45:24.041 INFO [pool-2-thread-4]-time:2018-04-13 15:45:24...st=test16
2018-04-13 15:45:24.049 INFO [pool-2-thread-2]-time:2018-04-13 15:45:24...st=test17
2018-04-13 15:45:24.049 INFO [pool-2-thread-1]-time:2018-04-13 15:45:24...st=test18
2018-04-13 15:45:24.050 INFO [pool-2-thread-5]-time:2018-04-13 15:45:24...st=test19

<!--the first task scheduling: Execution process logic as the same
-->
2018-04-13 15:45:30.002 INFO [startquertz_worker-3]-tasks have been scheduled, service RuleService2 has started ....
.....

Conclusion

Springframework.scheduling.quartz Job Scheduler accurately schedules tasks at specified times, which are customized at each interval of the task

Thread pool Java.util.concurrent.ThreadPoolExecutor, complete all work within the scheduling task independently.

Disadvantage: Once a task is called every other time, it customizes a multiple thread pool java.util.concurrent.ThreadPoolExecutor, resulting in a large number of multi-threaded pools appearing on the server.





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.