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



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章