多线程调用存储过程
<resultMap id="TupleScalarVoMap" type="com.huawei.it.cube.TupleScalarVo">
<result property="tuple" column="cubtuple"/>
<result property="scalar" column="scalar" jdbcType="VARCHAR" javaType="java.lang.String"/>
</resultMap>
<parameterMap id="getTheseCubeVParamMap" type="map">
<parameter property="rtnCursor" javaType="java.sql.ResultSet" mode="OUT" jdbcType="CURSOR" resultMap="TupleScalarVoMap"/>
<parameter property="tuples0" javaType="java.lang.String" mode="IN" jdbcType="VARCHAR" />
</parameterMap>
<update id ="getTheseCubeV" parameterMap="getTheseCubeVParamMap" statementType="CALLABLE">
call getTheseCubeV(?,?)
</update>
---------------------------
package com.huawei.it.cube;
public class TupleScalarVo {
private String tuple;
private Object scalar;
public String getTuple() {
return tuple;
}
public void setTuple(String tuple) {
this.tuple = tuple;
}
public Object getScalar() {
return scalar;
}
public void setScalar(Object scalar) {
this.scalar = scalar;
}
}
-------------------------------
package com.huawei.it.cube;
import java.util.List;
import java.util.Map;
public interface ICubeQueryBatcher {
public Map<String, Object> queryBatch(List<String> batchTuples);
}
-------------------------------
package com.huawei.it.cube;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
public class CubeQueryBatcherImpl implements ICubeQueryBatcher {
ThreadPoolExecutor threadPoolExecutor;
Object dao;
Method method;
public CubeQueryBatcherImpl(ThreadPoolExecutor threadPoolExecutor, Object dao, String methodName) {
super();
this.threadPoolExecutor = threadPoolExecutor;
this.dao = dao;
Method[] methods = dao.getClass().getMethods();
for (Method m : methods) {
if (m.getName().equals(methodName)) {
this.method = m;
break;
}
}
}
@Override
public Map<String, Object> queryBatch(List<String> batchTuples) {
List<Future<?>> FutureList = new ArrayList<Future<?>>();
Map<String, Object> map = new HashMap<String, Object>();
GroupTask task = new GroupTask(dao, method, batchTuples, map);
Future<?> future = this.threadPoolExecutor.submit(task);
FutureList.add(future);
for (Future<?> futureItem : FutureList) {
try {
futureItem.get();
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
}
return map;
}
static class GroupTask implements Runnable {
private Object dao;
private Method method;
private Map<String, Object> map;
private List<String> batchTuples;
public GroupTask(Object dao, Method method, List<String> batchTuples, Map<String, Object> map) {
super();
this.dao = dao;
this.method = method;
this.batchTuples = batchTuples;
this.map = map;
}
@Override
public void run() {
System.out.println("我是线程start---------");
Map<String, Object> params = new HashMap<String, Object>();
params.put("tuples0", "dog1");
try {
this.method.invoke(dao, params);
List<TupleScalarVo> rtnList = (List<TupleScalarVo>) params.get("rtnCursor");
synchronized (this.map) {
for (TupleScalarVo rtn : rtnList) {
map.put(rtn.getTuple(), rtn.getScalar());
}
}
} catch (Exception e) {
throw new RuntimeException("本皮查询错误", e);
}
}
}
}
--------------------------------
List<String> batchTuples = new ArrayList<String>();
Map<String, Object> queryBatch = CubeBatcherHelper.createQueryBatcher(this.exportDemoDao, "getTheseCubeV").queryBatch(batchTuples);
for (String key : queryBatch.keySet()) {
System.out.println("key:" + key + ";value:" + queryBatch.get(key));
}
-------------------------------
package com.huawei.it.cube;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class CubeBatcherHelper {
public CubeBatcherHelper() {
}
private static ThreadPoolExecutor threadPoolExecutor;
static {
LinkedBlockingQueue<Runnable> linkedBlocking = new LinkedBlockingQueue<Runnable>();
threadPoolExecutor = new ThreadPoolExecutor(50, 100, 30, TimeUnit.SECONDS, linkedBlocking);
}
public static ICubeQueryBatcher createQueryBatcher(Object dao, String methodName) {
CubeQueryBatcherImpl cubeQueryBatcher = new CubeQueryBatcherImpl(threadPoolExecutor, dao, methodName);
return cubeQueryBatcher;
}
}
---------------------------------