多线程调用存储过程

lifeifei2010 2013-12-04 05:03:42
<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;
}
}
---------------------------------



...全文
539 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
MiceRice 2013-12-05
  • 打赏
  • 举报
回复
问题是啥? 另外你貌似发错板块了。
lifeifei2010 2013-12-04
  • 打赏
  • 举报
回复
--------------------------------- create or replace procedure getTheseCubeV( rtnCursor out sys_refcursor, tuples0 in varchar) is --cursor rtnCursor is select * from tmp_getthesecubev; begin --cursor rtnCursor is select * from tmp_getthesecubev; insert into tmp_getthesecubev values('aa','333'); open rtnCursor for select * from tmp_getthesecubev; --for i in rtnCursor loop -- dbms_output.put_line(i.scalar); --end loop; --delete from tmp_getthesecubev; end; ------------------------------- -- Create table create global temporary table TMP_GETTHESECUBEV ( CUBTUPLE VARCHAR2(500), SCALAR VARCHAR2(100) ) on commit preserve rows;

25,980

社区成员

发帖
与我相关
我的任务
社区描述
高性能WEB开发
社区管理员
  • 高性能WEB开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧