Spring timed tasks. Thread pool, custom multithreaded configuration _quartz

Source: Internet
Author: User

Timed tasks and multithreaded configuration XML

<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" Xmlns:xs
       I= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" Xmlns:mvc= "Http://www.springframework.org/schema/mvc" xsi:schemalocation= "Http://www.springframework.org/schema /beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd Http://www.springframework.org/schem A/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.or G/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd "> <!--job Start--&gt
    
    ; <!--timed--> <bean id= "Rulebean" class= "Org.springframework.scheduling.quartz.MethodInvokingJobDetailF" Actorybean "> <property name=" targetobject "ref=" Ruleservice "/> <property" name= targetMethod " Value= "UpdateRule"/> </bean> <bean id=" rule "class=" org.springframework.scheduling.quartz.CronTriggerFact Orybean "> <property name=" jobdetail "ref=" Rulebean "/> <!--daily One o'clock in the morning--> <proper Ty name= "cronexpression" value= "0 0 1 * *?"
        /> </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>
        <property name= "triggers" > <list> <ref bean= "rule"/> </list>
    </property> <!--10 seconds to start the task--> <property name= "Startupdelay" value= "/>" </bean> <!--job end--> <bean id= "Threadpooltaskexecutor" class= "Org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor" > <!--core number of threads, default to 1--> <prope Rty name= "Corepoolsize" value= "1"/> <!--maximum number of threads, the default is Integer.max_value--> <property name= "Maxpools
          ize "value="/> <!--queue maximum length, generally need to set the value >=notifyScheduledMainExecutor.maxNum; default to Integer.max_value <property name= "queuecapacity" value= "1000"/>--> <!--thread pool to maintain the idle time allowed by the thread, by default 60s--> <propert Y name= "keepaliveseconds" value= "AbortPolicy"/> <!--thread pool processing policy for reject tasks (wireless threads available), only for the time being, the latter is supported by default- > <property name= "Rejectedexecutionhandler" > <!--abortpolicy: Direct throw JAVA.UTIL.CONCURRENT.REJECTE
          Dexecutionexception exception--> <!--callerrunspolicy: The main thread performs the task directly, and attempts to add the next task to the thread pool can effectively reduce the speed at which tasks are added to the thread pool--> <!--discardoldestpolicy: Discard old tasks, temporarily do not support, will cause the discarded task can not be executed again--> <!--discardpolicy: Discard the current task, temporarily do not support;
     The discarded task cannot be executed again-->     <bean class= "Java.util.concurrent.threadpoolexecutor$callerrunspolicy"/> </property> </bean > <bean id= "contextutil" class= "Com.mythopoet.util.ContextUtil" ></bean> </beans>

Timed Tasks Java class

Ruleservice.java

@Service public
class Ruleservice {public
    void UpdateRule () {
    
      System.out.println ("scheduled task started");
    
    }

Multithreaded configuration

Starttaskthread.java

public class Starttaskthread implements Runnable {
     private list<string> List;
    

    Public Starttaskthread (list<string> list) {
       this.list= list;
    }
    @Override public
    synchronized void Run () {
           System.out.println (list.size ());
    }

Modify the Timer task class, assign the data, and publish it to each thread.

Ruleservice.java

@Service public
class Ruleservice {public

    void UpdateRule () {
        int nums = 5;//Open 5 threads list<string> strlist= new arraylist<string> ();
        for (int i = 0; i < i++) {Strlist.add ("test" +i);
        } map<integer, list<string>> Map = new Hashmap<integer, list<string>> ();
            for (int i = 0; i < nums i++) {list<string> List = new arraylist<string> ();
        Map.put (i, list); int st = 0; beginning int avg = strlist.size ()/nums;
                Average int count =0;//record value for (int i = 0; i < nums; i++) {if (i==nums-1) {
                    for (int j = count; J <strlist.size (); j + +) {map.get (i). Add (Strlist.get (j));
                count++;
                New Thread (New Starttaskthread (Map.get (i)). Start ();
            Break
             for (int j = St; J < (ST+AVG); j + +) {map.get (i). Add (Strlist.get (j));   count++; } St=st+avg;
            New Thread (New Starttaskthread (Map.get (i)). Start ();
} }

Thread Configuration Tool class, if you need to manipulate DAO within a thread, you need to actively obtain the bean for the DAO class injected by the spring

Contextutil.java

Import org.springframework.beans.BeansException;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.ApplicationContextAware;

public class ContextUtil implements applicationcontextaware{the

    private static applicationcontext context

    ; @Override public
    void Setapplicationcontext (ApplicationContext applicationcontext)
            throws Beansexception { Context
        = ApplicationContext;
    }

    public static ApplicationContext GetContext () {return context
        ;
    }

    public static Object Getbean (String beanname) {return
        Context.getbean (beanname);
    }

}

Get DAO notation

Ruledao Ruledao = (Ruledao) contextutil.getbean ("Ruledao");
                

Using a thread pool

Starttaskthread2.java

public class Starttaskthread implements Runnable {
     private String st;
    

    Public Starttaskthread (String St) {
       this.st= st;
    }
    @Override public
    Void Run () {
           System.out.println (ST);
    }
}

Thread pool

Ruleservice2.java

@Service public
class Ruleservice {public

    void UpdateRule () {

        Threadpooltaskexecutor tpte = ( Threadpooltaskexecutor) Contextutil.getbean ("Threadpooltaskexecutor");
        list<string> strlist= new arraylist<string> ();
        for (int i = 0; i < i++) {
            strlist.add ("Test" +i);
        }
        
        for (int i = 0; i < strlist.size (); i++) {
            tpte.execute (new StartTaskThread2 (Strlist.get (i));
        }
}

 

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.