码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-09-14 11:53:00      阅读:205      评论:0      收藏:0      [点我收藏+]

标签: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

原文地址:http://tianjian.blog.51cto.com/3549910/1965127

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!