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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了线程池--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");



以上是关于线程池--spring配置,静态上下文获取以及调用的主要内容,如果未能解决你的问题,请参考以下文章

Spring异步方法注解 @Async

线程池的调用

spring中的线程池

Spring Cloud Gateway中netty线程池优化

Spring异步线程池—传递线程上下文(TaskDecorator实现)

普通java类调用spring中的bean