(Office) interfaces to access other systems httpClient, asynchronous access

  Access to the other system interface httpClient, but are synchronized , synchronization means the current thread is blocked , in order to carry out the next request only after completion of this request ; asynchronous means that all requests can be inserted into the buffer at the same time, without blocking the current thread ;

     httpClient request , access other systems may become unresponsive , locked , access requests on-line problems , these problems as flies buzzed in your ear , then the solution he can set the timeout , but an interface to access nearly 1 Fenzhong , this should not happen , so we should request asynchronous interfaces to other systems , we can use multiple threads to do , to create a thread pool , designated only 10 threads , then call the method. ( Good idea )

   For springmvc The following is an example :

    1. springmvc in to add a thread pool

application.xml里添加
 xmlns:task="http://www.springframework.org/schema/task"
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
        <!-- 核心线程数 -->
        <property name="corePoolSize" value="10" />
        <!-- 最大线程数 -->
        <property name="maxPoolSize" Value = " 10 " /> 
        <-! Maximum queue length -> 
        <Property name = " queueCapacity " value = " 500 " /> 
        <-! Thread pool maintenance thread allowed idle time, the default is 60s - -> 
        <Property name = " keepAliveSeconds " value = " 200 is " /> 
    </ the bean>

     2. Operation:

// custom thread pool: 
@Resource
 Private of TaskExecutor, taskExecutor;
 the try { 
      taskExecutor.execute ( new new the Runnable () {
         public  void RUN () {
           // Here write processing business code 
// Note that when the method of transmission parameters, to make use of the basic types value.
} }); } the catch (Exception E) { e.printStackTrace (); }

 

Guess you like

Origin www.cnblogs.com/historylyt/p/11140019.html