Spring timer combined with thread pool

Source: Internet
Author: User

Requirements: Spring timer combined with thread pool processing work order

A. Timed sweep library to detect a certain number of work orders to be processed

B. Turn on work orders that have been detected by the threading process

1, create a task for processing work orders

@Component ("Appworkordertask") @Scope ("Prototype") Public classAppworkordertaskImplementsRunnable { Public Static FinalString bean_name = "Appworkordertask"; @ResourcePrivateIappworkorderservice Appworkorderservice; PrivateString Appworkorderid;  Public voidExecute () {//process work Order logic code    }     PublicString Getappworkorderid () {returnAppworkorderid; }     Public voidSetappworkorderid (String appworkorderid) { This. Appworkorderid =Appworkorderid; } @Override Public voidRun () {execute (); }}

2, create a sweep library timer


Public classAppworkorderschedulerextendsTimerTask {PrivateLogger Logger = Logger.getlogger (Appworkorderscheduler.class); @ResourcePrivateIappworkorderservice Appworkorderservice; @Resource (Name= "Appworkordertaskexecutor") PrivateThreadpooltaskexecutor Taskexecutor; @Override Public voidRun () {//Determine thread pool capacity intSize = Taskexecutor.getmaxpoolsize ()-Taskexecutor.getactivecount (); if(Size > 0) {
Find work order list<String> appworkorderids = appworkorderservice.getappworkorderlist (20); Logger.info ("Find" + appworkorderids.size () + "task (s) to execute ..."); for(String appworkorderid:appworkorderids) {appworkordertask task=NewTask (Appworkorderid);Taskexecutor.execute (Task); } } } Privateappworkordertask NewTask (String appworkorderid) {appworkordertask task=(Appworkordertask) beanfactory.getinstance (). Getbeanbyname (Appworkordertask.bean_name);
Get Appworkordertask public class from ApplicationContext no longer affixed Task.setappworkorderid (Appworkorderid); returntask; }}

3. Configuration files

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:tx= "Http://www.springframework.org/schema/tx"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-2.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring- Aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd " Default-autowire= "ByName"Default-lazy-init= "true">    <!--Apply a work order asynchronous thread executor -    <BeanID= "Appworkordertaskexecutor"class= "Org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">        <!--The thread pool maintains a minimum number of threads -        < Propertyname= "Corepoolsize"value= "Ten"/>        <!--maximum number of threads maintained by the thread pool -        < Propertyname= "Maxpoolsize"value= " the"/>        <!--buffer queue used by the thread pool -        < Propertyname= "Queuecapacity"value= " the" />    </Bean>        <Beanclass= "Org.springframework.scheduling.timer.TimerFactoryBean"Lazy-init= "false">        < Propertyname= "Scheduledtimertasks">            <List>                <refBean= "Appworkorderscheduledtimertask" />            </List>        </ Property>    </Bean>        <!--application WorkOrder Scheduler Timer -    <BeanID= "Appworkorderschedulertask"class= "Com.test.timer.scheduler.AppWorkOrderScheduler" />    <BeanID= "Appworkorderscheduledtimertask"class= "Org.springframework.scheduling.timer.ScheduledTimerTask">         <!--delay 10000ms (10s) during Operation -          < Propertyname= "Delay"value= "10000" />         <!--Run once every 90s -          < Propertyname= "Period"value= "90000" />        < Propertyname= "TimerTask"ref= "Appworkorderschedulertask" />    </Bean></Beans>

Spring timer combined with thread pool

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.