Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
S
sentinel
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
曲欣亮
sentinel
Commits
55f1f662
Commit
55f1f662
authored
Jul 30, 2019
by
Quxl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
x
parent
bce81833
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
46 additions
and
34 deletions
+46
-34
SqlConcurrentController.java
.../java/com/egolm/sentinel/web/SqlConcurrentController.java
+46
-34
No files found.
src/main/java/com/egolm/sentinel/web/SqlConcurrentController.java
View file @
55f1f662
...
@@ -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
();
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment