一:pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tc.mf</groupId>
<artifactId>MyTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<snakeyaml.version>1.14</snakeyaml.version>
<asyncload.version>1.0.1-SNAPSHOT</asyncload.version>
<oro.version>2.0.8</oro.version>
<qdpProject.version>3.0.0-SNAPSHOT</qdpProject.version>
<tc-dw-agent-api-version>1.0.0-SNAPSHOT</tc-dw-agent-api-version>
<tc-var.version>1.0-SNAPSHOT</tc-var.version>
</properties>
<dependencies>
<dependency>
<groupId>com.alibaba.asyncload</groupId>
<artifactId>asyncload</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>oro</groupId>
<artifactId>oro</artifactId>
<version>${oro.version}</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2</version>
</dependency>
</dependencies>
</project>
二:spring-context.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/lang
http://www.springframework.org/schema/lang/spring-lang-4.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.0.xsd">
<aop:config proxy-target-class="true"></aop:config>
<!-- 异步并行加载线程池 -->
<!--
关于mode参数,目前支持REJECT和CALLSRUN。
REJECT:当异步提交的任务数超过了acceptCount后,直接返回Reject异常
CALLSRUN:当异步提交的任务数超过了acceptCount后,由当前提交的线程执行runnable任务。此时的线程模型就变为了poolSize+1线程数,你的提交线程也就成为了其中的一个工作线程。建议使用该参数
通过看源码后,其实默认值就是CALLSRUN
-->
<bean id="asyncLoadExecutor" class="com.alibaba.asyncload.AsyncLoadExecutor"
init-method="initital" destroy-method="destory">
<property name="poolSize" value="50" /><!-- 异步线程池大小 -->
<property name="acceptCount" value="-1" /><!-- 不限制提交任务队列大小 -->
<property name="mode" value="CALLERRUN" />
</bean>
<!-- 匹配信息配置 -->
<!--
defaultTimeout:指异步提交任务后,等待返回结果的超时时间,可以有效保护系统的健壮性(当外部系统不可用时)。如果不想进行超时控制,可设置为0。默认值为0
needThreadLocalSupport:指异步任务提交后,原先的正常业务处理线程A和异步任务的处理线程B,是否共享ThreadLocal变量,使用需慎用,一般不建议开启。默认值为false,不开启
needBarrierSupport:指当原先一个业务处理线程,被拆分为N多个异步任务并行处理后,可以通过设置栅栏,在某一代码处要求所有的异步结果均返回后才进行下一步操作。默认值为false,不开启
matches:匹配点定义,asyncload自带的匹配方式,如果使用spring拦截器处理可不配置该属性,使用spring pointcut定义匹配点。
-->
<bean id="asyncLoadConfig" class="com.alibaba.asyncload.AsyncLoadConfig">
<property name="defaultTimeout" value="3000" />
<property name="needThreadLocalSupport" value="false" />
<property name="needBarrierSupport" value="false" />
<property name="matches">
<map>
<entry key-ref="asyncLoadMethodMatch" value="2000" />
</map>
</property>
</bean>
<!--
patterns:代表满足该正则的匹配
excludedPatterns:代表需要被排除的匹配
excludeOveride:true/false
true: 优先执行excluded排除匹配
false:优先执行满足匹配,在满足匹配通过后,再执行排除匹配
-->
<bean id="asyncLoadMethodMatch"
class="com.alibaba.asyncload.impl.AsyncLoadPerl5RegexpMethodMatcher">
<property name="patterns">
<list>
<!-- <value>(.*)RemoteModel(.*)</value> -->
<value>.*sayHello*.*</value>
</list>
</property>
<property name="excludedPatterns">
<list>
<value>(.*)listRemoteModel(.*)</value>
</list>
</property>
<property name="excludeOveride" value="false" />
</bean>
<!-- 异步并行加载模板类基于模板模式,提供异步并行机制。可以编程方式指定进行异步并行加载的执行单元。 比如针对好几个service的调用合并为一次并行加载。 -->
<bean id="asyncLoadTemplate" class="com.alibaba.asyncload.impl.template.AsyncLoadTemplate">
<property name="executor" ref="asyncLoadExecutor" />
<property name="config" ref="asyncLoadConfig" />
</bean>
<!-- 并行加载拦截器,如果要实现一些特殊方法拦截可扩展这个方法 -->
<bean id="asyncLoadInterceptor" class="com.alibaba.asyncload.impl.spring.AsyncLoadInterceptor">
<property name="asyncLoadTemplate" ref="asyncLoadTemplate" />
</bean>
<!-- 按照bean名称创建异步并行服务代理 -->
<bean class="com.alibaba.asyncload.impl.spring.CompositeAutoProxyCreator">
<property name="optimize" value="false" />
<property name="proxyTargetClass" value="false" />
<!-- 指定具体需要做异步拦截的service -->
<property name="beanNames">
<list>
<!-- <value>asynCloadTask</value>
<value>analyzeService</value>
-->
<value>person</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>asyncLoadInterceptor</value>
</list>
</property>
</bean>
<bean id ="person" class="com.tc.Person"/>
</beans>
三:java类
public class BaseAsyncLoadNoRunTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"spring-context.xml"});
Person p1 = (Person)context.getBean("person", Person.class);
p1.sayHello("gege", 1);
p1.sayHello("gege1", 2);
p1.sayHello("gege2", 3);
p1.sayHello("gege3", 4);
//摧毁studentService实例对象
((ClassPathXmlApplicationContext) context).close();
}
}
public class Person {
public void sayHello(String name, int index) {
System.out.println(index + ",hello," + name);
try {
Thread.currentThread().sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
【】【】【】【】【】【】【】【】
<property name="beanNames">
<list>
<value>batchBlackListService</value>
</list>
</property>
<bean id="asyncLoadMethodMatch"
class="com.alibaba.asyncload.impl.AsyncLoadPerl5RegexpMethodMatcher">
<property name="patterns">
<list>
<value>.*SelfBlackList.*</value>
<value>.*WdBlackList.*</value>
<value>.*DeadBeatBlackList.*</value>
</list>
</property>
<property name="excludedPatterns">
<list>
<value>(.*)listModel(.*)</value>
</list>
</property>
<property name="excludeOveride" value="false" />
</bean>
Map<String, Object> selfHitMap = batchBlackListService.querySelfBlackList(blacklistJson);
//查询网贷黑名单命中结果
Map<String, Object> wdHitMap = batchBlackListService.queryWdBlackList(blacklistJson);
//查询老赖黑名单命中结果
Map<String, Object> deadbeatMap = batchBlackListService.queryDeadBeatBlackList(blacklistJson);
//三种黑名单 合并
String result = BlackListUtil.createResponseHitResult(selfHitMap, wdHitMap, deadbeatMap);
response.setData(result);