@Bean("自定义线程池名称")
public ThreadPoolTaskExecutor saveDataExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//核心线程数目
executor.setCorePoolSize(10);
//队列中最大的数目
executor.setQueueCapacity(200);
//指定最大线程数
executor.setMaxPoolSize(15);
//线程名称前缀
executor.setThreadNamePrefix("线程名字");
//rejection-policy:当pool已经达到max size的时候,如何处理新任务
//CALLER_RUNS:不在新线程中执行任务,而是由调用者所在的线程来执行
//对拒绝task的处理策略
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
//线程空闲后的最大存活时间(对核心线程不起作用)
executor.setKeepAliveSeconds(60);
//过期时间对核心线程起作用
//executor.setAllowCoreThreadTimeOut(true);
//加载
executor.initialize();
return executor;
}
@Async("自定义线程池名称")
public Future<Boolean> save(String functionName, String tableName, List<Object> list);
Oracle判断表是否存在
<select id="hasTableInDatabase" resultType="java.lang.Integer">
select count(*) from user_tables t where table_name= upper('${tableName}')
</select>
<update id="createTable">
create table ${tableName} as SELECT * FROM ${baseTableName} where 1=2
</update>
反射调用mapper所有方法
@Autowired
private SqlSession sqlSession;
@Override
public Future<Boolean> save(String functionName, String tableName, List<Object> list) {
try {
// 获取Mapper地址
Class<?> mapper = Class
.forName("xxxMapper");
Object instance = Proxy.newProxyInstance(mapper.getClassLoader(), new Class[] { mapper },
new DataCollectInvocationHandler(sqlSession.getMapper(mapper)));
// 各自的接口中需要定义好对应的方法才能调用
Method method = instance.getClass().getMethod(functionName, String.class, List.class);
method.invoke(instance, tableName, list);
return new AsyncResult<>(true);
} catch (Exception e) {
e.printStackTrace();
return new AsyncResult<>(false);
}
}