线程池--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");




      本文转自tianjian_0913 51CTO博客,原文链接:http://blog.51cto.com/tianjian/1965127,如需转载请自行联系原作者




相关文章
|
4天前
|
SQL Java 数据库连接
(自用)Spring常用配置
(自用)Spring常用配置
12 0
|
26天前
|
SQL Java 数据库连接
挺详细的spring+springmvc+mybatis配置整合|含源代码
挺详细的spring+springmvc+mybatis配置整合|含源代码
33 1
|
4天前
|
JSON Java 数据库连接
属性注入掌握:Spring Boot配置属性的高级技巧与最佳实践
属性注入掌握:Spring Boot配置属性的高级技巧与最佳实践
11 1
|
4天前
|
Java 数据库连接 Spring
简化配置,提高灵活性:Spring中的参数化配置技巧
简化配置,提高灵活性:Spring中的参数化配置技巧
14 0
|
4天前
|
Java Shell 测试技术
一次配置,多场景适用:Spring Boot多套配置文件的深度剖析
一次配置,多场景适用:Spring Boot多套配置文件的深度剖析
16 0
一次配置,多场景适用:Spring Boot多套配置文件的深度剖析
|
8天前
|
Java 容器
SpringBoot使用配置注解开启自动配置功能&整合spring-boot-configuration-processor
SpringBoot使用配置注解开启自动配置功能&整合spring-boot-configuration-processor
11 0
|
26天前
|
Java 数据库连接 Spring
Spring5深入浅出篇:Spring配置⽂件参数化
该文档介绍了Spring配置文件参数化的概念和步骤。目的是将经常需要修改的配置,如数据库连接参数,从主配置文件中分离到一个单独的`.properties`文件,以便于管理和维护。步骤包括创建小型配置文件(如`db.properties`),在`applicationContext.xml`中引入该文件,并使用`${key}`语法在Spring配置文件中引用这些参数。最终通过测试验证配置文件参数化的成功。
|
27天前
|
消息中间件 SpringCloudAlibaba Java
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(八)Config服务配置+bus消息总线+stream消息驱动+Sleuth链路追踪
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(八)Config服务配置+bus消息总线+stream消息驱动+Sleuth链路追踪
771 0
|
1月前
|
消息中间件 运维 监控
|
2月前
|
Java
logback配置,命名为logback-spring.xml
logback配置,命名为logback-spring.xml