Commit 55f1f662 authored by Quxl's avatar Quxl

x

parent bce81833
...@@ -28,6 +28,7 @@ import com.alibaba.druid.pool.DruidDataSource; ...@@ -28,6 +28,7 @@ import com.alibaba.druid.pool.DruidDataSource;
import com.egolm.common.StringUtil; import com.egolm.common.StringUtil;
import com.egolm.common.bean.Rjx; import com.egolm.common.bean.Rjx;
import com.egolm.common.jdbc.JUtil; import com.egolm.common.jdbc.JUtil;
import com.egolm.sentinel.config.ExceptionHandler;
import com.egolm.sentinel.config.XRException; import com.egolm.sentinel.config.XRException;
@RestController @RestController
...@@ -35,16 +36,17 @@ public class SqlConcurrentController { ...@@ -35,16 +36,17 @@ public class SqlConcurrentController {
@PostMapping("concurrentSql") @PostMapping("concurrentSql")
public Rjx doExecute(String jdbcUrl, String username, String password, String sql, String parameters, Integer threadCount, Integer executeCount, Boolean isOpenTx) throws InterruptedException, IOException, SQLException { public Rjx doExecute(String jdbcUrl, String username, String password, String sql, String parameters, Integer threadCount, Integer executeCount, Boolean isOpenTx) {
List<Object> list = new ArrayList<Object>(); List<Object> list = new ArrayList<Object>();
List<String> stack = new ArrayList<String>();
Long start = System.currentTimeMillis();
try {
threadCount = threadCount == null ? 1 : threadCount; threadCount = threadCount == null ? 1 : threadCount;
executeCount = executeCount == null ? 1 : executeCount; executeCount = executeCount == null ? 1 : executeCount;
isOpenTx = isOpenTx == null ? false : isOpenTx; isOpenTx = isOpenTx == null ? false : isOpenTx;
ThreadPoolExecutor executor = new ThreadPoolExecutor(threadCount, threadCount, 5, TimeUnit.MINUTES, new LinkedBlockingQueue<Runnable>(executeCount)); ThreadPoolExecutor executor = new ThreadPoolExecutor(threadCount, threadCount, 5, TimeUnit.MINUTES, new LinkedBlockingQueue<Runnable>(executeCount));
CountDownLatch latch = new CountDownLatch(executeCount); CountDownLatch latch = new CountDownLatch(executeCount);
DruidDataSource dataSource = null; DruidDataSource dataSource = null;
Long start = System.currentTimeMillis();
try { try {
dataSource = this.getDataSource(jdbcUrl, username, password); dataSource = this.getDataSource(jdbcUrl, username, password);
TransactionTemplate txTemplate = null; TransactionTemplate txTemplate = null;
...@@ -54,18 +56,22 @@ public class SqlConcurrentController { ...@@ -54,18 +56,22 @@ public class SqlConcurrentController {
} }
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
for(int i = 0; i < executeCount; i++) { for(int i = 0; i < executeCount; i++) {
executor.execute(new Runner(jdbcTemplate, txTemplate, latch, sql, list)); executor.execute(new Runner(jdbcTemplate, txTemplate, latch, sql, list, stack));
} }
latch.await(); latch.await();
} finally { } finally {
dataSource.close(); dataSource.close();
} }
} catch (Exception e) {
stack.add(ExceptionHandler.toStackString(e).toString());
}
Long end = System.currentTimeMillis(); Long end = System.currentTimeMillis();
return Rjx.jsonOk().setData(list).set("执行时间", (end-start)).set("执行次数", executeCount).set("并发线程", threadCount); return Rjx.jsonOk().setData(list).set("执行时间", (end-start)).set("执行次数", executeCount).set("并发线程", threadCount);
} }
public static class Runner implements Runnable { public static class Runner implements Runnable {
private List<String> stack;
private List<Object> list; private List<Object> list;
private TransactionTemplate txTemplate; private TransactionTemplate txTemplate;
private JdbcTemplate jdbcTemplate; private JdbcTemplate jdbcTemplate;
...@@ -73,31 +79,35 @@ public class SqlConcurrentController { ...@@ -73,31 +79,35 @@ public class SqlConcurrentController {
private String sql; private String sql;
private String parameters; private String parameters;
public Runner(JdbcTemplate jdbcTemplate, TransactionTemplate txTemplate, CountDownLatch latch, String sql, List<Object> list) { public Runner(JdbcTemplate jdbcTemplate, TransactionTemplate txTemplate, CountDownLatch latch, String sql, List<Object> list, List<String> stack) {
super(); super();
this.txTemplate = txTemplate; this.txTemplate = txTemplate;
this.jdbcTemplate = jdbcTemplate; this.jdbcTemplate = jdbcTemplate;
this.latch = latch; this.latch = latch;
this.sql = sql; this.sql = sql;
this.list = list; this.list = list;
this.stack = stack;
} }
@Override @Override
public void run() { public void run() {
List<Object> result; try {
if(txTemplate != null) { if(txTemplate != null) {
result = txTemplate.execute(new TransactionCallback<List<Object>>() { txTemplate.execute(new TransactionCallback<List<Object>>() {
public List<Object> doInTransaction(TransactionStatus status) { public List<Object> doInTransaction(TransactionStatus status) {
return executeSql(); executeSql();
return null;
} }
}); });
} else { } else {
result = executeSql(); executeSql();
}
} catch (Exception e) {
stack.add(ExceptionHandler.toStackString(e).toString());
} }
list.add(result);
} }
public List<Object> executeSql() { public void executeSql() {
try { try {
List<Object> argList = new ArrayList<Object>(); List<Object> argList = new ArrayList<Object>();
if(StringUtil.isNotBlank(parameters)) { if(StringUtil.isNotBlank(parameters)) {
...@@ -119,7 +129,7 @@ public class SqlConcurrentController { ...@@ -119,7 +129,7 @@ public class SqlConcurrentController {
} }
Object[] args = argList.toArray(new Object[argList.size()]); Object[] args = argList.toArray(new Object[argList.size()]);
return jdbcTemplate.execute(new CallableStatementCreator() { list.add(jdbcTemplate.execute(new CallableStatementCreator() {
public CallableStatement createCallableStatement(final Connection con) throws SQLException { public CallableStatement createCallableStatement(final Connection con) throws SQLException {
final CallableStatement cs = con.prepareCall(sql); final CallableStatement cs = con.prepareCall(sql);
for(int i = 0; i < args.length; i++) { for(int i = 0; i < args.length; i++) {
...@@ -147,7 +157,9 @@ public class SqlConcurrentController { ...@@ -147,7 +157,9 @@ public class SqlConcurrentController {
} }
return datas; return datas;
} }
}); }));
} catch (Exception e) {
stack.add(ExceptionHandler.toStackString(e).toString());
} finally { } finally {
latch.countDown(); latch.countDown();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment