Spring多线程最佳实践

时间:2022-06-17 23:19:52
1、配置文件web.xml中关于applicationContext.xml位置的配置,若不配置则applicationContext.xml的默认位置为src\main\webapp\WEB-INF\conf下
      <servlet>
           <servlet-name>service</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <init-param>
                  <param-name>contextConfigLocation</param-name>
                 <param-value>/WEB-INF/applicationContext.xml</param-value>
         </init-param>
     </servlet>


2、bean的配置文件applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd


http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd">

<context:component-scan base-package="com.suning" />
<context:component-scan base-package="com.suning.app" />
<context:component-scan base-package="com.suning.commonlib" />
<context:component-scan base-package="com.suning.snRedis" />
<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
           <!-- 线程池维护线程的最少数量 -->
           <property name="corePoolSize" value="5" />
           <!-- 允许的空闲时间 -->
           <property name="keepAliveSeconds" value="300" />
           <!-- 线程池维护线程的最大数量 -->
           <property name="maxPoolSize" value="10" />
           <!-- 缓存队列 -->
           <property name="queueCapacity" value="20" />
           <!-- 对拒绝task的处理策略 -->
           <property name="rejectedExecutionHandler">
              <bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy" />
           </property>
    </bean>
    <bean id="taskExecutorUtil" class="com.suning.commonlib.TaskExecutorUtil">    
           <!-- <constructor-arg ref="taskExecutor" />  --> 
           <property name="taskExecutor" ref="taskExecutor" /> 
    </bean> 
    <!-- 托管线程 -->
    <bean id="setRedisTask" class="com.suning.snRedis.SetRedisTask">    
    </bean>
</beans>


3、在Action类中
public class RelatedQueryGetter{
     @Autowired
    TaskExecutorUtil taskExecutorUtil;
    public static void main(String[] args){
       taskExecutorUtil.setRedis(processedQuery,result);
    }



4、其他类:
package com.suning.commonlib;


import org.springframework.core.task.TaskExecutor;


import com.suning.snRedis.SetRedisTask;


public class TaskExecutorUtil {
private TaskExecutor taskExecutor;
public TaskExecutor getTaskExecutor() {
        return taskExecutor;
    }
public void setTaskExecutor(TaskExecutor taskExecutor) {
        this.taskExecutor = taskExecutor;
    }
public void setRedis(String processedQuery, String result) {
taskExecutor.execute(new SetRedisTask(processedQuery, result));
    }
}






package com.suning.snRedis;


public class SetRedisTask implements Runnable {
private String processedQuery;
private String result;
static private RedisAccess redisAccess = new RedisAccess();
    public SetRedisTask() {


    }
    public SetRedisTask(String processedQuery, String result) {
        this.processedQuery = processedQuery;
        this.result = result;
    }
    @Override
    public void run() {
        redisAccess.hset(processedQuery, "suning_rqss", result);
    }
}