线程池--spring配置,静态上下文获取以及调用

@ImportResource({"classpath:dubbo.xml","classpath*:applicationContext.xml"})

定义applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"

xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx.xsd

http://www.springframework.org/schema/task

http://www.springframework.org/schema/task/spring-task.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop.xsd">

<!-- 用于接收报表生成请求,批量生成报表 -->

<bean id ="rptGenExecutor"  class ="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor" >

<!-- 线程池维护线程的最少数量 -->

<property name ="corePoolSize" value ="1"/>

<!-- 线程池维护线程所允许的空闲时间 -->

<property name ="keepAliveSeconds" value ="120"/>

<!-- 线程池维护线程的最大数量 -->

<property name ="maxPoolSize" value ="100"/>

<!-- 线程池所使用的缓冲队列 -->

<property name ="queueCapacity" value ="10000" />

</bean>

</beans>

定义名称 引用定义的线程池

@Autowired

@Qualifier("rptGenExecutor")

private ThreadPoolTaskExecutor rptExec;

使用:

Runnable action = new RptGenTask(conditcionParamVO, reportType);

rptExec.execute(action);

RptGenTask 实现runable接口

添加带参数的构造函数来传递需要的值

重写 run方法,

定义静态上下文:

@Component

public class RptEntrance implements ApplicationContextAware {

private static ApplicationContext applicationContext;

@Override

public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

this.applicationContext = applicationContext;

ServiceBuilder.initAppCtx(applicationContext);

}

public static ApplicationContext getContext() {

return applicationContext;

}

}

package com.nttdata.elearning.report.service;

import org.springframework.context.ApplicationContext;

public class ServiceBuilder {

private static ApplicationContext ctx;

static void initAppCtx(ApplicationContext appCtxIn){

ctx = appCtxIn;

}

public static <T> Object getService(String serviceName, Class<T> serviceClazz){

return ctx.getBean(serviceName, serviceClazz);

}

}

class RptGenTask implements Runnable {

private RptRequest req = null;

private String rptTemplNo = null;

RptGenTask(RptRequest rptReq, String rptType){

this.req = rptReq;

this.rptTemplNo = rptType;

}

@Override

public void run() {

}

注入bean

ApplicationContext ctx=RptEntrance.getContext();

//TODO 从当前的Spring ApplicationContext中,获取命名的Service/Component;

this.reportService = (ReportService) ctx.getBean("reportService");

时间: 2024-07-30 10:14:15

线程池--spring配置,静态上下文获取以及调用的相关文章

记录一次线程池的在项目中的实际应用,讲解一下线程池的配置和参数理解。

前言:最近项目中与融360项目中接口对接,有反馈接口(也就是我们接收到请求,需要立即响应,并且还要有一个接口推送给他们其他计算结果),推送过程耗时.或者说两个接口不能是同时返回,有先后顺序. 这时我想到了把自己Controller立即返回接受成功,中间添加一个新的线程去做其他耗时的操作(线程池配置和参数测试讲解请阅读第5步). 1.Controller代码如下: @Autowiredprivate CallThreadDemo worker; @RequestMapping("/bandBank

SpringMVC整合TaskExecutor线程池的配置/使用

一.配置jdbc.properties添加: #------------ Task ------------ task.core_pool_size=5 task.max_pool_size=50 task.queue_capacity=1000 task.keep_alive_seconds=60 二.配置Spring的applicationContext.xml添加: <bean id="taskExecutor" class="org.springframewor

05 - Tomcat 线程池的配置与优化

添加 Executor 在server.xml中的Service节点里面,增加executor节点,然后配置connector的executor属性,如下: <Executor name="tomcatThreadPool" namePrefix="catalina-exec-" maxThreads="3000" minSpareThreads="5" maxSpareThreads="20" ac

[email&#160;protected]异步线程池的配置及应用

示例: 1. 配置 @EnableAsync @Configuration public class TaskExecutorConfiguration { @Autowired private TaskExecutorProperties taskExecutorProperties; @Bean public Executor routeGen() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); execut

Spring配置静态目录

mvc-dispatcher-servlet.xml文件 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="ht

Spring+shiro session与线程池的坑

在java web编程中,经常使用shiro来管理session,也确实好用 shiro来获取session的方式 SecurityUtils.getSubject().getSession() 其中SecurityUtils的getSubject代码如下 /** * Returns the currently accessible {@code Subject} available to the calling code depending on * runtime environment.

spring 线程池 的一个坑。

问题简述: 配置的队列初始化的消费者线程占满了线程池.导致其他的再使用此线程池中线程不运行.不报错,不抛异常.线程的数量仅为为线程池的配置中的最小值. <task:executor pool-size="100-150" queue-capacity="250" > 同时schema描述中写道: The size of the executor's thread pool as either a single value or a range    (e

Spring使用ThreadPoolTaskExecutor自定义线程池及实现异步调用

多线程一直是工作或面试过程中的高频知识点,今天给大家分享一下使用 ThreadPoolTaskExecutor 来自定义线程池和实现异步调用多线程. 一.ThreadPoolTaskExecutor 本文采用 Executors 的工厂方法进行配置. 1.将线程池用到的参数定义到配置文件中 在项目的 resources 目录下创建 executor.properties 文件,并添加如下配置: # 异步线程配置 # 核心线程数 async.executor.thread.core_pool_si

Java线程池的理论与实践

前段时间公司里有个项目需要进行重构,目标是提高吞吐量和可用性,在这个过程中对原有的线程模型和处理逻辑进行了修改,发现有很多基础的多线程的知识已经模糊不清,如底层线程的运行情况.现有的线程池的策略和逻辑.池中线程的健康状况的监控等,这次重新回顾了一下,其中涉及大量java.util.concurrent包中的类.本文将会包含以下内容:Java中的Thread与操作系统中的线程的关系线程切换的各种开销ThreadGroup存在的意义使用线程池减少线程开销Executor的概念ThreadPoolEx